简体   繁体   中英

Why isn't my RecyclerView showing anything in this fragment?

I set up a very simple template app with a ViewPager just to play around and get familiar with Fragments and the ViewPager itself. I have a TabbedActivity class that contains the following fragment class:

public static class LinearFragment extends Fragment {

    RecyclerView recyclerView;
    public ArrayList<AndroidVersion> data;
    public DataAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.fragment_linear, container, false);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.card_recyler_view_pager);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(layoutManager);
        loadJSON();

        return inflater.inflate(R.layout.fragment_linear, container, false);
    }

    private void loadJSON() {
        Retrofit retrofit = MainActivity.getRestAdapter();
        RequestInterface request = retrofit.create(RequestInterface.class);
        Call<JSONResponse> call = request.getJSON();
        call.enqueue(new Callback<JSONResponse>() {
            @Override
            public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
                JSONResponse jsonResponse = response.body();
                data = new ArrayList<>(Arrays.asList(jsonResponse.getAndroid()));
                adapter = new DataAdapter(data);
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<JSONResponse> call, Throwable t) {
                Log.d("Error", t.getMessage());
            }
        });
    }

}

My MainActivity is another very basic file to display some JSON data returned from Retrofit in another RecyclerView, so I built a public static method in MainActivity so that I can share a single Retrofit instance throughout. In my MainActivity the JSON is loaded and displayed in the RecylerView just fine, but for some reason when I navigate to the ViewPager and to the LinearFragment portion nothing happens.

getRestAdapter() in my MainActivity looks like this:

public static Retrofit getRestAdapter(){
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://api.learn2crack.com")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    return retrofit;
}

The layout files for the parent activity (TabbedActivity) and Fragment are as follows:

activity_tabbed.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.inta.anthony.recylerjsonparsing.TabbedActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay">

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />


</android.support.design.widget.CoordinatorLayout>

fragment_linear.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.inta.anthony.recylerjsonparsing.MainActivity">


    <android.support.v7.widget.RecyclerView
        android:id="@+id/card_recyler_view_pager"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/view_pager_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/mainActivityButton"/>

</LinearLayout>

Thanks!

  1. Don't create the adapter every time when you try to update data. Set up it in onCreateView() method within recycler initialization, for example. To update data you may use this in your adapter:

     public void updateItems(ArrayList<AndroidVersion> newItems) { if(this.items != null) { this.items.clear() this.items.addAll(newItems) notifyDataSetChanged() } } 
  2. Try to set static height for the recycler view in the layout for test

Figured it out. Made a dumb mistake and called return inflater.inflate(R.layout.fragment_linear, container, false); at the end of my onCreateView() method in the fragment instead of return rootView; The rootView object was already created at the beginning of the method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM