GridViewAdapter在getItemCount()中返回数据大小为0

时间:2016-08-23 07:32:12

标签: android android-fragments android-viewpager android-recyclerview recycler-adapter

我正面临recyclerview adapter这个奇怪的问题。我在Fragment中使用FragleStatePagerAdapter中的GridLayoutManager。我有四个碎片。当我刷回Recyclerview中的第一个片段数据时会丢失。见this screen cast

这是片段中的recyclerview初始化部分。

RecyclerView.LayoutManager layoutManager = new GridLayoutManager(mCon, 3);
recyclerView.setLayoutManager(layoutManager);

mGalleryModelList.clear();
mGalleryModelList = db.getInventory(strPopular.split(","));
Log.i("ReqFragmet","initialising adapter");

adapter = new GridRecyAdapterForReq(mCon, mGalleryModelList, Glide.with(this));
Log.i("ReqFragmet","calling setAdapter");
recyclerView.setAdapter(adapter);

这是适配器的代码

public class GridRecyAdapterForReq extends RecyclerView.Adapter<GridRecyAdapterForReq.MainViewHolder> {

    private List<MediaModel> data = new ArrayList<>();

    private Context mCOn;

    public static final String TAG = "GridRecyclerViewAdapter";

    private RequestManager glide;


    public GridRecyAdapterForReq(Context context, List<MediaModel> list, RequestManager glide) {
        Log.i(TAG,"inside constructor");
        Log.i(TAG,"initial data size of adaper list : "+ data.size());

        this.mCOn=context;
        this.data=list;
        this.glide=glide;

        Log.i(TAG,"final data size of adaper list : "+ data.size());
        Log.i(TAG,"leaving constructor");
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        Log.i(TAG,"inside onCreateViewHolder() ");

        return new MyViewHolder(LayoutInflater.from(mCOn).inflate(R.layout.row_gridview_item_request, parent, false));
    }

    @Override
    public void onBindViewHolder(MainViewHolder mHolder, int position) {

            Log.i(TAG,"inside onBindViewHolder() ");

            MyViewHolder holder = (MyViewHolder) mHolder;

            MediaModel model = data.get(position);

            holder.res_name.setText(model.name);

            glide.load(model.url).placeholder(R.drawable.inventory_placeholder).diskCacheStrategy(DiskCacheStrategy.RESULT).crossFade().into(holder.res_pic);


    }
    @Override
    public int getItemCount() {

        Log.i(TAG,"inside getItemCount() ");
        Log.i(TAG,"data size list count : "+ data.size());

        return data.size();

    }

    public class MainViewHolder extends RecyclerView.ViewHolder {
        public MainViewHolder(View v) {
            super(v);
        }
    }

    private class MyViewHolder extends MainViewHolder{

        ImageView res_pic;
        TextView res_name;

        public MyViewHolder(View itemView) {
            super(itemView);
            res_name = (TextView)itemView.findViewById(R.id.text);
            res_pic=(ImageView)itemView.findViewById(R.id.picture);
        }
    }

}

我已在此处记录了适配器的行为。查看数据项大小。在适配器初始化时它的6和getItemCount()被称为零。因为recyclerview中没有显示哪些数据。 任何人都可以指出我的错误在哪里?

08-23 02:28:27.980 4971-4971/com.skcsllp.mutterfly.debug E/RecyclerView: No adapter attached; skipping layout
08-23 02:28:28.876 4971-4971/com.skcsllp.mutterfly.debug I/ReqFragmet: initialising adapter
08-23 02:28:28.876 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: inside constructor
08-23 02:28:28.876 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: initial data size of adaper list : 0
08-23 02:28:28.876 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: final data size of adaper list : 6
08-23 02:28:28.876 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: leaving constructor
08-23 02:28:28.876 4971-4971/com.skcsllp.mutterfly.debug I/ReqFragmet: calling setAdapter
08-23 02:28:28.876 4971-4971/com.skcsllp.mutterfly.debug W/FragmentManager: moveToState: Fragment state for RequestFragment{527e7e18 #4 id=0x7f0f00f3} not updated inline; expected state 3 found 2
08-23 02:28:28.880 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: inside getItemCount() 
08-23 02:28:28.880 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: data size list count : 0
08-23 02:28:28.880 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: inside getItemCount() 
08-23 02:28:28.880 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: data size list count : 0
08-23 02:28:28.884 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: inside getItemCount() 
08-23 02:28:28.884 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: data size list count : 0
08-23 02:28:28.884 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: inside getItemCount() 
08-23 02:28:28.884 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: data size list count : 0
08-23 02:28:28.888 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: inside getItemCount() 
08-23 02:28:28.888 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: data size list count : 0
08-23 02:28:28.888 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: inside getItemCount() 
08-23 02:28:28.888 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: data size list count : 0
08-23 02:28:28.892 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: inside getItemCount() 
08-23 02:28:28.892 4971-4971/com.skcsllp.mutterfly.debug I/GridRecyclerViewAdapter: data size list count : 0

Initial view after swipe

2 个答案:

答案 0 :(得分:0)

在我之前提到的所有更改之后,我再次尝试了您的代码,现在它已完全正常运行。请检查

<强> MainActivity.java

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    GridRecyAdapterForReq adapter;
    private List<MediaModel> mGalleryModelList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 3);
        recyclerView.setLayoutManager(layoutManager);

        mGalleryModelList.clear();

        for (int i = 0; i < 10; i++) {
            MediaModel mediaModel = new MediaModel();
            mediaModel.name = "image " + (i + 1);
            mediaModel.url = "https://s-media-cache-ak0.pinimg.com/236x/12/64/da/1264da4a3f18207dc22592102abae40d.jpg";
            mGalleryModelList.add(mediaModel);
        }

        Log.i("ReqFragmet", "initialising adapter");

        adapter = new GridRecyAdapterForReq(getApplicationContext(), mGalleryModelList);
        Log.i("ReqFragmet", "calling setAdapter");
        recyclerView.setAdapter(adapter);
    }

    private class GridRecyAdapterForReq extends RecyclerView.Adapter<GridRecyAdapterForReq.MainViewHolder> {

        public static final String TAG = "GridRecyclerViewAdapter";
        LayoutInflater layoutInflater;
        private List<MediaModel> data = new ArrayList<>();
        private Context mCOn;

        public GridRecyAdapterForReq(Context context, List<MediaModel> list) {
            Log.i(TAG, "inside constructor");
            Log.i(TAG, "initial data size of adaper list : " + data.size());

            this.mCOn = context;
            this.data = list;
            layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);

            Log.i(TAG, "final data size of adaper list : " + data.size());
            Log.i(TAG, "leaving constructor");
        }

        @Override
        public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            Log.i(TAG, "inside onCreateViewHolder() ");

            return new MainViewHolder(LayoutInflater.from(mCOn).inflate(R.layout.row_gridview_item_request, parent, false));
        }

        @Override
        public void onBindViewHolder(MainViewHolder mHolder, int position) {

            Log.i(TAG, "inside onBindViewHolder() ");

            MediaModel model = data.get(position);

            mHolder.res_name.setText(model.name);

            Glide.with(getApplicationContext())
                    .load(model.url)
                    .placeholder(R.mipmap.ic_launcher)
                    .diskCacheStrategy(DiskCacheStrategy.RESULT)
                    .crossFade()
                    .into(mHolder.res_pic);
        }

        @Override
        public int getItemCount() {

            Log.i(TAG, "inside getItemCount() ");
            Log.i(TAG, "data size list count : " + data.size());

            return data.size();

        }

        public class MainViewHolder extends RecyclerView.ViewHolder {
            ImageView res_pic;
            TextView res_name;

            public MainViewHolder(View itemView) {
                super(itemView);
                res_name = (TextView) itemView.findViewById(R.id.text);
                res_pic = (ImageView) itemView.findViewById(R.id.picture);
            }
        }
    }

    class MediaModel {
        String name;
        String url;
    }
}

<强> activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

<强> row_gridview_item_request.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/picture"
        android:layout_width="50dp"
        android:layout_height="50dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"/>

</LinearLayout>

<强>截图

here is a screenshot for my code

答案 1 :(得分:0)

初始化viewpager后,使用方法viewpager.setOffscreenPageLimit(int),一次保留片段数。还要重新检查FragmentStatePagerAdapter类的getItem()中的片段初始化代码。