简体   繁体   English

为什么我的RecyclerView没有在此片段中显示任何内容?

[英]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. 我使用ViewPager设置了一个非常简单的模板应用程序,以玩转并熟悉Fragments和ViewPager本身。 I have a TabbedActivity class that contains the following fragment class: 我有一个TabbedActivity类,其中包含以下片段类:

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. 我的MainActivity是另一个非常基本的文件,用于在另一个RecyclerView中显示从Retrofit返回的一些JSON数据,因此我在MainActivity中构建了一个公共静态方法,以便可以在整个过程中共享一个Retrofit实例。 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. 在我的MainActivity中,JSON可以很好地加载并显示在RecylerView中,但是由于某些原因,当我导航到ViewPager和LinearFragment部分时,什么也没发生。

getRestAdapter() in my MainActivity looks like this: 我的MainActivity中的getRestAdapter()如下所示:

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: 父活动(TabbedActivity)和Fragment的布局文件如下:

activity_tabbed.xml 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 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. 例如,在回收站初始化期间的onCreateView()方法中进行设置。 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); 犯了一个愚蠢的错误,并称为return inflater.inflate(R.layout.fragment_linear, container, false); at the end of my onCreateView() method in the fragment instead of return rootView; 在片段中的onCreateView()方法的末尾,而不是return rootView; The rootView object was already created at the beginning of the method. rootView对象已在方法开始时创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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