垂直回收者视图内的水平回收者视图的自定义适配器

时间:2018-06-05 05:42:56

标签: android android-recyclerview

我有一个父级回收站视图,其中包含一个水平回收站视图作为其项目。我在其中显示类别明智的视频。 当我开始滚动水平回收视图时,应用程序崩溃。

错误是:

FATAL EXCEPTION: main
Process: earn.ads.earningvideos, PID: 11192
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid
view holder adapter positionViewHolder{f5a3493 position=3 id=-1,
oldPos=-1, pLpos:-1 no parent}
android.support.v7.widget.RecyclerView{16875cc VFED..... ........
0,80-686,394 #7f08014e app:id/vid_rv},
adapter:earn.ads.earningvideos.classes.Category_thumbnail@c2e47ce,
layout:android.support.v7.widget.LinearLayoutManager@e986ef,
context:earn.ads.earningvideos.Activities.Home_page@9308787
at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5416)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5598)
atandroid.support.v7.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:285)
at android.support.v7.widget.GapWorker.flushTaskWithDeadline(GapWorker.java:342)
at android.support.v7.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:358)
at android.support.v7.widget.GapWorker.prefetch(GapWorker.java:365)
at android.support.v7.widget.GapWorker.run(GapWorker.java:396)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) at
com.android.internal.os.ZygoteInit.main  (ZygoteInit.java:924) 

我的代码是 类别

public void getCat(){
    Cursor cur = null;
    cat_list.clear();
    String item_query = " SELECT tcategory.*,(select count(VideoPlaylist.videoId) from VideoPlaylist where tcategory.CategoryId=VideoPlaylist.catId) as totalVideos FROM "+ database.Table.Name.TABLE_CATEGORY +" order by totalVideos asc";
    cur = db.findCursor(item_query, null);


    if (cur != null && cur.moveToNext()) {

        for(int i=0;i<cur.getCount();i++)
        {
            if(Integer.parseInt(cur.getString(cur.getColumnIndex("totalVideos")).toString())>0) {
                HashMap<String, String> map = new HashMap<>();

                map.put("vcategory", cur.getString(cur.getColumnIndex(database.Table.Name.CAT_NAME.toString())));
                map.put("vcategoryid", cur.getString(cur.getColumnIndex(database.Table.Name.CAT_ID.toString())));

                cat_list.add(map);
            }
            cur.moveToNext();
        }
    }
    cur.close();
        recyclerView.setLayoutManager(linearLayoutManager);
        CategoryAdapter categoryAdapter = new CategoryAdapter(getContext(), cat_list);
        recyclerView.setAdapter(categoryAdapter);
        recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
}

垂直适配器

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.MyViewHolder> {

    View view;
    Context context;
    database db;
    Typeface typeface;
    LinearLayoutManager llm;
    String catid;
    public static ArrayList<HashMap<String, String>> cat_list;
    ArrayList<HashMap<String,String>> videoData= new ArrayList<>();


    public CategoryAdapter(Context context,ArrayList<HashMap<String, String>> cat_list) {
        this.context = context;
        this.cat_list = cat_list;
        typeface = Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Regular.ttf");
    }

    @Override
    public CategoryAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_custom, parent, false);
        MyViewHolder vh = new MyViewHolder(v); // pass the view to View Holder
        return vh;
    }

    @Override
    public void onBindViewHolder(CategoryAdapter.MyViewHolder holder, int position) {

        llm = new 
LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false);

        db = new database(context);
        holder.cat.setText(cat_list.get(position).get("vcategory"));
        catid = cat_list.get(position).get("vcategoryid");
        getData(Integer.parseInt(catid),holder.rv);


        holder.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ((Home_page)context).load_fragment(new More_category());
            }
        });
    }

    @Override
    public int getItemCount() {
        return cat_list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        // init the item view's
        RecyclerView rv;
        TextView more,cat;


        public MyViewHolder(View itemView) {
            super(itemView);

            // get the reference of item view's
            rv = (RecyclerView)itemView.findViewById(R.id.vid_rv);
            more = (TextView)itemView.findViewById(R.id.more);
            cat = (TextView)itemView.findViewById(R.id.cat);

        }
    }

    public void getData(int cid, RecyclerView rv){
        Cursor cur = null;
        videoData.clear();
        String item_query = "SELECT * FROM "+ database.Table.Name.TABLE_NAME +" WHERE catId = "+ cid + " order by videoId desc" ;
        cur = db.findCursor(item_query, null);

        if (cur != null && cur.moveToNext()) {

            for(int i=0;i<cur.getCount();i++)
            {
                HashMap<String,String> map= new HashMap<>();

                map.put("vtitle", cur.getString(cur.getColumnIndex(database.Table.Name.VIDEOTITLE.toString())));
                map.put("vtime", cur.getString(cur.getColumnIndex(database.Table.Name.VIDEO_TIME.toString())));
                map.put("vviews", cur.getString(cur.getColumnIndex(database.Table.Name.VIDEO_VIEW.toString())));
                map.put("vamount", cur.getString(cur.getColumnIndex(database.Table.Name.VIDEO_AMOUNT.toString())));
                map.put("vimage", cur.getString(cur.getColumnIndex(database.Table.Name.VIDEO_THUMB.toString())));
                map.put("vid", cur.getString(cur.getColumnIndex(database.Table.Name.VIDEO_ID.toString())));
                map.put("vkey", cur.getString(cur.getColumnIndex(database.Table.Name.YOUTUBE_KEY.toString())));
                map.put("vlike", cur.getString(cur.getColumnIndex(database.Table.Name.LIKE.toString())));
                map.put("vlike_status", cur.getString(cur.getColumnIndex(database.Table.Name.LIKE_STATUS.toString())));
                map.put("vcomment", cur.getString(cur.getColumnIndex(database.Table.Name.COMMENT.toString())));
                map.put("vtype", cur.getString(cur.getColumnIndex(database.Table.Name.TYPE.toString())));
                map.put("vseen_status", cur.getString(cur.getColumnIndex(database.Table.Name.STATUS.toString())));
                map.put("vdesc", cur.getString(cur.getColumnIndex(database.Table.Name.VIDEO_DETAILS.toString())));
                map.put("vsubscription", cur.getString(cur.getColumnIndex(database.Table.Name.SUBSCRIPTION.toString())));
                map.put("vsubscriber", cur.getString(cur.getColumnIndex(database.Table.Name.SUBSCRIBER.toString())));
                map.put("vcompany", cur.getString(cur.getColumnIndex(database.Table.Name.COMPANY.toString())));
                map.put("vcompanyid", cur.getString(cur.getColumnIndex(database.Table.Name.COMPANY_ID.toString())));

                videoData.add(map);

                cur.moveToNext();
            }
        }
        cur.close();
        rv .setLayoutManager(llm);
        Category_thumbnail category_thumbnail = new Category_thumbnail(context,videoData);
        rv.setAdapter(category_thumbnail);

    }

}

横向适配器

public class Category_thumbnail extends RecyclerView.Adapter<Category_thumbnail.MyViewHolder> {
    View view;
    Context context;
    Typeface typeface;
    public static ArrayList<HashMap<String, String>> vidList;

    public Category_thumbnail(Context context,ArrayList<HashMap<String, String>> vidList) {
        this.context = context;
        this.vidList = vidList;
        typeface = Typeface.createFromAsset(context.getAssets(), "fonts/OpenSans-Regular.ttf");
    }

    @Override
    public Category_thumbnail.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.categoryvidlist, parent, false);
        MyViewHolder vh = new MyViewHolder(v); // pass the view to View Holder
        return vh;
    }

    @Override
    public void onBindViewHolder(final Category_thumbnail.MyViewHolder holder, int position) {

        holder.loader_small.setVisibility(View.VISIBLE);
        holder.vtitle.setText(vidList.get(position).get("vtitle"));
        holder.views.setText(vidList.get(position).get("vviews"));
        holder.duration.setText(vidList.get(position).get("vtime"));


        Picasso.with(context).load(vidList.get(position).get("vimage").toString()).into(holder.thumbnail, new Callback() {
            @Override
            public void onSuccess() {
                holder.loader_small.setVisibility(View.GONE);
            }

            @Override
            public void onError() {
            }
        });
    }

    @Override
    public int getItemCount() {

        return vidList.size();
    }


    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        // init the item view's
         TextView vtitle,views,duration;
         ImageView thumbnail;
         ProgressBar loader_small;


        public MyViewHolder(View itemView) {
            super(itemView);

            // get the reference of item view's
            vtitle = (TextView)itemView.findViewById(R.id.vtitle);
            views = (TextView)itemView.findViewById(R.id.views);
            duration = (TextView)itemView.findViewById(R.id.duration);
            thumbnail = (ImageView)itemView.findViewById(R.id.vidThumb);
            loader_small =(ProgressBar)itemView.findViewById(R.id.smallloader);


        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果不能正常工作,您可以尝试以下水平适配器,尝试垂直适配器:

@Override
    public void onBindViewHolder(final Category_thumbnail.MyViewHolder holder, int position) {

        //check this line
        if (position < 0 || position >= vidList.size()) return;

        holder.loader_small.setVisibility(View.VISIBLE);
        holder.vtitle.setText(vidList.get(position).get("vtitle"));
        holder.views.setText(vidList.get(position).get("vviews"));
        holder.duration.setText(vidList.get(position).get("vtime"));


        Picasso.with(context).load(vidList.get(position).get("vimage").toString()).into(holder.thumbnail, new Callback() {
            @Override
            public void onSuccess() {
                holder.loader_small.setVisibility(View.GONE);
            }

            @Override
            public void onError() {
            }
        });
    }

    @Override
    public int getItemCount() {

        //check this line
        return (vidList != null) ? vidList.size() : 0;
    }