Android若干条并排RecyclerView滑动实时联动

Android若干条并排RecyclerView滑动实时联动


以水平方向并排排列的两条RecyclerView为例,实现一个简单的功能:这两个RecyclerView要实时联合滚动,即其中任意一个RecyclerView,将触发其余所有RecyclerView同时滚动相同。
写布局:
<?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="horizontal">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view_1"
        android:layout_width="180dp"
        android:layout_height="match_parent" />

    <View
        android:layout_width="1px"
        android:layout_height="match_parent"
        android:background="#e0e0e0" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view_2"
        android:layout_width="180dp"
        android:layout_height="match_parent" />
</LinearLayout>



代码:
package zhangphil.recyclerview;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

public class RecyclerViewActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView1;
    private RecyclerView mRecyclerView2;

    private ArrayList<Integer> mItems;

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

        mItems = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            mItems.add(i);
        }

        initRecyclerView1();
        initRecyclerView2();

        mRecyclerView1.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
                    mRecyclerView2.scrollBy(dx, dy);
                }
            }
        });

        mRecyclerView2.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (recyclerView.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) {
                    mRecyclerView1.scrollBy(dx, dy);
                }
            }
        });
    }

    private void initRecyclerView1() {
        mRecyclerView1 = findViewById(R.id.recycler_view_1);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView1.setLayoutManager(layoutManager);

        RecyclerViewAdapter mAdapter = new RecyclerViewAdapter(1);
        mRecyclerView1.setAdapter(mAdapter);
    }

    private void initRecyclerView2() {
        mRecyclerView2 = findViewById(R.id.recycler_view_2);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView2.setLayoutManager(layoutManager);

        RecyclerViewAdapter mAdapter = new RecyclerViewAdapter(2);
        mRecyclerView2.setAdapter(mAdapter);
    }

    private class RecyclerViewAdapter extends RecyclerView.Adapter<MyVH> {
        private int id;

        public RecyclerViewAdapter(int id) {
            this.id = id;
        }

        @NonNull
        @Override
        public MyVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getApplicationContext()).inflate(android.R.layout.simple_list_item_2, parent, false);
            return new MyVH(view);
        }

        @Override
        public void onBindViewHolder(@NonNull MyVH holder, int position) {
            holder.text1.setText("RecyclerView:" + id);
            holder.text2.setText(mItems.get(position) + "");

            switch (id) {
                case 1:
                    holder.text1.setBackgroundColor(Color.RED);
                    break;

                case 2:
                    holder.text1.setBackgroundColor(Color.BLUE);
                    break;
            }
        }

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

    private class MyVH extends RecyclerView.ViewHolder {
        public TextView text1;
        public TextView text2;

        public MyVH(View itemView) {
            super(itemView);
            text1 = itemView.findViewById(android.R.id.text1);
            text1.setTextColor(Color.WHITE);
            text2 = itemView.findViewById(android.R.id.text2);
            text2.setTextColor(Color.DKGRAY);
        }
    }
}





同时滑动其中一条,然后第二条也实时联合滚动

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android Studio中实现并排按钮,您可以使用LinearLayout或ConstraintLayout布局来放置两个按钮。这里有两种常见的方法: 方法一:使用LinearLayout布局 您可以使用一个水平的LinearLayout来容纳两个按钮,并将它们并排放置。以下是一个示例代码片段: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button 1" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button 2" /> </LinearLayout> ``` 方法二:使用ConstraintLayout布局 您也可以使用ConstraintLayout来放置两个按钮。以下是一个示例代码片段: ```xml <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toStartOf="@+id/button2" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" app:layout_constraintStart_toEndOf="@+id/button1" app:layout_constraintEnd_toEndOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> ``` 这两种方法都可以实现并排的按钮布局。您可以根据需要选择其中一种布局方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值