How to sync scrolling first-positions of 2 RecyclerViews?

后端 未结 4 1943
情话喂你
情话喂你 2021-01-01 21:21

Background

I have 2 RecyclerView instances. One is horizontal, and the second is vertical.

They both show the same data and have the same amount of items,

4条回答
  •  -上瘾入骨i
    2021-01-01 22:06

    Another simple solution working fine in my device

    Variable

    RecyclerView horizontalRecyclerView, verticalRecyclerView;
    LinearLayoutManager horizontalLayoutManager, verticalLayoutManager;
    
    ArrayList arrayList = new ArrayList<>();
    ArrayList arrayList2 = new ArrayList<>();
    

    RecyclerView code

    horizontalRecyclerView = findViewById(R.id.horizontalRc);
    verticalRecyclerView = findViewById(R.id.verticalRc);
    
    horizontalRecyclerView.setHasFixedSize(true);
    verticalRecyclerView.setHasFixedSize(true);
    
    horizontalLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    verticalLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    
    horizontalRecyclerView.setLayoutManager(horizontalLayoutManager);
    verticalRecyclerView.setLayoutManager(verticalLayoutManager);
    
    for (int i = 0; i < 50; i++) {
         arrayList.add("" + i);
         arrayList2.add("" + i);
     }
    
     MyDataAdapter horizontalAdapter = new MyDataAdapter(this, arrayList);
     MyDataAdapter verticalAdapter = new MyDataAdapter(this, arrayList2);
    
     horizontalRecyclerView.setAdapter(horizontalAdapter);
     verticalRecyclerView.setAdapter(verticalAdapter);
    

    logic inside RecyclerView.addOnScrollListener

     horizontalRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
    
                    int pos = horizontalLayoutManager.findFirstCompletelyVisibleItemPosition();
                    verticalLayoutManager.scrollToPositionWithOffset(pos, 20);
    
                }
    
            });
    
    
            verticalRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
    
                    int pos = verticalLayoutManager.findFirstCompletelyVisibleItemPosition();
                    horizontalLayoutManager.scrollToPositionWithOffset(pos, 20);
    
    
                }
            });
    

    Hope it help some one

    WHOLE Code

    public class Main4Activity extends AppCompatActivity {
    
        RecyclerView horizontalRecyclerView, verticalRecyclerView;
        LinearLayoutManager horizontalLayoutManager, verticalLayoutManager;
    
        ArrayList arrayList = new ArrayList<>();
        ArrayList arrayList2 = new ArrayList<>();
    
        boolean isVertical = true, isHorizontal = true;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main4);
    
    
            horizontalRecyclerView = findViewById(R.id.horizontalRc);
            verticalRecyclerView = findViewById(R.id.verticalRc);
    
            horizontalRecyclerView.setHasFixedSize(true);
            verticalRecyclerView.setHasFixedSize(true);
    
            horizontalLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
            verticalLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    
            horizontalRecyclerView.setLayoutManager(horizontalLayoutManager);
            verticalRecyclerView.setLayoutManager(verticalLayoutManager);
    
            for (int i = 0; i < 50; i++) {
                arrayList.add("" + i);
                arrayList2.add("" + i);
            }
    
            MyDataAdapter horizontalAdapter = new MyDataAdapter(this, arrayList);
            MyDataAdapter verticalAdapter = new MyDataAdapter(this, arrayList2);
    
            horizontalRecyclerView.setAdapter(horizontalAdapter);
            verticalRecyclerView.setAdapter(verticalAdapter);
    
            horizontalRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
    
                    int pos = horizontalLayoutManager.findFirstCompletelyVisibleItemPosition();
                    verticalLayoutManager.scrollToPositionWithOffset(pos, 20);
    
    
                }
    
            });
    
    
            verticalRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
    
                    int pos = verticalLayoutManager.findFirstCompletelyVisibleItemPosition();
                    horizontalLayoutManager.scrollToPositionWithOffset(pos, 20);
    
    
                }
            });
    
    
    
           /* horizontalRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
    
                    int pos = horizontalLayoutManager.findFirstCompletelyVisibleItemPosition();
                    verticalLayoutManager.scrollToPositionWithOffset(pos, 20);
    
                    *//*if (isHorizontal) {
                        int pos = horizontalLayoutManager.findFirstCompletelyVisibleItemPosition();
                        verticalLayoutManager.scrollToPositionWithOffset(pos, 20);
                        Log.e("isHorizontal", "TRUE");
                        isVertical = false;
                    } else {
                        isHorizontal = true;
                    }*//*
    
                }
    
               *//* @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    isVertical = true;
                }*//*
            });
    
    
            verticalRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    
                *//* @Override
                 public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                     super.onScrollStateChanged(recyclerView, newState);
                     isHorizontal = true;
                 }
     *//*
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
    
                    int pos = verticalLayoutManager.findFirstCompletelyVisibleItemPosition();
                    horizontalLayoutManager.scrollToPositionWithOffset(pos, 20);
    
                   *//* if (isVertical) {
                        int pos = verticalLayoutManager.findFirstCompletelyVisibleItemPosition();
                        horizontalLayoutManager.scrollToPositionWithOffset(pos, 20);
                        Log.e("isVertical", "TRUE");
                        isHorizontal = false;
                    } else {
                        isVertical = true;
                    }*//*
    
    
                }
            });*/
    
    
        }
    }
    

    Adapter code

    public class MyDataAdapter extends RecyclerView.Adapter {
    
        Context context;
        ArrayList arrayList;
    
    
        public MyDataAdapter(Context context, ArrayList arrayList) {
            this.context = context;
            this.arrayList = arrayList;
        }
    
        @Override
        public MyDataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.temp, parent, false);
            return new ViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(MyDataAdapter.ViewHolder holder, int position) {
    
            if (position % 2 == 0) {
                holder.tvNumber.setBackgroundResource(R.color.colorGreen);
            } else {
                holder.tvNumber.setBackgroundResource(R.color.colorRed);
            }
            holder.tvNumber.setText(arrayList.get(position));
        }
    
        @Override
        public int getItemCount() {
            return arrayList.size();
        }
    
        public class ViewHolder extends RecyclerView.ViewHolder {
    
            TextView tvNumber;
    
            public ViewHolder(View itemView) {
                super(itemView);
                tvNumber = itemView.findViewById(R.id.tvNumber);
            }
        }
    }
    

    Activity layout

    
    
    
    
        
    
        
    
    
    
        
    

    temp Layout

    
    
    
        
    
    
    
    

    COLOR

    #307832
    #ff4c4c
    

提交回复
热议问题