本文共 6311 字,大约阅读时间需要 21 分钟。
package zhangphil.test;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.support.v7.widget.StaggeredGridLayoutManager;import android.view.LayoutInflater;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import java.util.ArrayList;public class RVActivity extends AppCompatActivity { private RecyclerView mRecyclerView1; private RecyclerView mRecyclerView2; private ArrayList相应布局文件:mItems; /** * 返回true,表示拦截事件。 * 返回false,表示不做任何处理,交给子View处理。 * * @param event * @return */ @Override public boolean dispatchTouchEvent(MotionEvent event) { /** * 如果用户的手指同时放在屏幕上滑动,不要触发滚动事件。 * */ if (event.getPointerCount() >= 2) { return true; } /** * 如果左侧的RecyclerView1在滚动中,但是此时用户又在RecyclerView2中触发滚动事件,则停止所有滚动,等待新一轮滚动。 * */ if (mRecyclerView1.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) { if (touchEventInView(mRecyclerView2, event.getX(), event.getY())) { mRecyclerView1.stopScroll(); mRecyclerView2.stopScroll(); return true; } } /** * 如果右侧的RecyclerView2在滚动中,但是此时用户又在RecyclerView1中触发滚动事件,则停止所有滚动,等待新一轮滚动。 * */ if (mRecyclerView2.getScrollState() != RecyclerView.SCROLL_STATE_IDLE) { if (touchEventInView(mRecyclerView1, event.getX(), event.getY())) { mRecyclerView2.stopScroll(); mRecyclerView1.stopScroll(); return true; } } return super.dispatchTouchEvent(event); } private boolean touchEventInView(View view, float x, float y) { if (view == null) { return false; } int[] location = new int[2]; view.getLocationOnScreen(location); int left = location[0]; int top = location[1]; int right = left + view.getMeasuredWidth(); int bottom = top + view.getMeasuredHeight(); if (y >= top && y <= bottom && x >= left && x <= right) { return true; } return false; } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.rv_activity); mItems = new ArrayList<>(); for (int i = 0; i < 10000; 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); StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(12, StaggeredGridLayoutManager.VERTICAL); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); mRecyclerView2.setLayoutManager(layoutManager); RecyclerViewAdapter mAdapter = new RecyclerViewAdapter(2); mRecyclerView2.setAdapter(mAdapter); } private class RecyclerViewAdapter extends RecyclerView.Adapter { 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(R.layout.rv_item, 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(R.id.text1); text1.setTextColor(Color.WHITE); text2 = itemView.findViewById(R.id.text2); text2.setTextColor(Color.DKGRAY); } }}
附录: 1,《Android若干条并排RecyclerView滑动实时联动》链接:https://blog.csdn.net/zhangphil/article/details/79925803 2,《 Android dispatchTouchEvent检测多点触摸事件是否落入在某一个View区域内》链接:https://blog.csdn.net/zhangphil/article/details/80067150