在Vertical RecyclerView内部的Horizo​​ntal RecyclerView中滚动项目,将垂直滚动视图滚动到顶部

时间:2018-09-28 16:33:18

标签: android android-recyclerview recycler-adapter

我在vertical recyclerview内部有水平recyclerview,如果我在水平recyclerview内部有两个以上项目,则在水平RV内部滚动项目,导致垂直滚动转到顶部或项目0。

如果水平滚动视图有两个项目,那么一切正常,但是如果我添加两个以上的项目,则垂直滚动会重置或滚动到顶部。

我的适配器垂直滚动适配器是

    public class RecyclerViewVerticalListAdapter extends RecyclerView.Adapter<RecyclerViewVerticalListAdapter.ItemViewHolder>{
    private List<Object> horizontalList;
    Context context;
    RecyclerViewHorizontalListAdapter rvAdapter;
    public RecyclerViewVerticalListAdapter(List<Object> horizontalList, Context context){
        this.horizontalList= horizontalList;
        this.context = context;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View groceryProductView = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_home_layout, parent, false);
        ItemViewHolder gvh = new ItemViewHolder(groceryProductView);
        return gvh;
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, final int position) {

        if(position==0){
            holder.txtHeader.setText("Exteriors");
        }
        else if(position==1){
            holder.txtHeader.setText("Interiors");
        }
        else if(position==2){
            holder.txtHeader.setText("Dev plans");
        }

        holder.rv_horizontal.addItemDecoration(new DividerItemDecoration(context, LinearLayoutManager.VERTICAL) {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

            }
        });

        RecyclerViewHorizontalListAdapter recyclerViewHorizontal1ListAdapter = new RecyclerViewHorizontalListAdapter(
                (List<ItemBean>) horizontalList.get(position),context);

        LinearLayoutManager horizontalLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        SnapHelper snapHelper = new PagerSnapHelper();
        holder.rv_horizontal.setLayoutManager(horizontalLayoutManager);
        snapHelper.attachToRecyclerView(holder.rv_horizontal);
        holder.rv_horizontal.setAdapter(recyclerViewHorizontal1ListAdapter);

        holder.txtSeeAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,ImageViewActivity.class);
                intent.putExtra("section",position);
                intent.putExtra("isSeeAll",true);
                context.startActivity(intent);
            }
        });

    }

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


    public class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView txtHeader,txtSeeAll;
        RecyclerView rv_horizontal;
        public ItemViewHolder(View view) {
            super(view);
            txtHeader=view.findViewById(R.id.tv_header);
            txtSeeAll=view.findViewById(R.id.tv_see_all);
            rv_horizontal = view.findViewById(R.id.rv_horizontal);
        }

    }
}

我的卧式RV适配器:

    public class RecyclerViewFloorPlanlListAdapter extends RecyclerView.Adapter<RecyclerViewFloorPlanlListAdapter.ItemViewHolder>{
    private List<ItemBean> horizontalList;
    Context context;

    public RecyclerViewFloorPlanlListAdapter(List<ItemBean> horizontalList, Context context){
        this.horizontalList= horizontalList;
        this.context = context;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //inflate the layout file
        View groceryProductView = LayoutInflater.from(parent.getContext()).inflate(R.layout.floor_plan_layout, parent, false);
        ItemViewHolder gvh = new ItemViewHolder(groceryProductView);
        return gvh;
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, final int position) {

        Glide.with(context).load(horizontalList.get(position).getItemImage())
                .apply(new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .into(holder.imageView);

        holder.txtHeader.setText(horizontalList.get(position).getItemName());
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                String productName = horizontalGrocderyList.get(position).getProductName().toString();
//                Toast.makeText(context, productName + " is selected", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context,ImageViewActivity.class);
                intent.putExtra("section",horizontalList.get(position).getSection());
                intent.putExtra("isSeeAll",false);
                context.startActivity(intent);

            }
        });
    }


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


    public class ItemViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView txtHeader,tv01,tv02;

        public ItemViewHolder(View view){
            super(view);

            imageView = view.findViewById(R.id.roundedImageView);
            txtHeader = view.findViewById(R.id.tv_item_name);
            tv01 = view.findViewById(R.id.tv_01);
            tv02 = view.findViewById(R.id.tv_02);
            int width = (int) (UserActivity.mDisplayMatix.widthPixels*0.85f);
            itemView.setLayoutParams(new RecyclerView.LayoutParams(width, RecyclerView.LayoutParams.WRAP_CONTENT));
            itemView.setTag(itemView);
        }

//        @Override
//        public void onClick(View view) {
//            if (clickListener != null) clickListener.onClick(view, getAdapterPosition());
//        }
    }
}

0 个答案:

没有答案