Android两条并排RecyclerView实时联动滑动增强
在附录1中,我初步实现了两条垂直摆放的RecyclerView的实时联动滚动,但是附录文章1的实现存在一定问题,比如当用户的手指同时在屏幕的两条RecyclerView区域内滑动,将发生异常。还有就是,比如在RecyclerView1正在滚动时候,用户又在RecyclerView2的区域内手指滑动,这样将触发循环嵌套滚动事件,引发错误,因此本文在附录1的基础上改进,通过拦截处理系统的触摸事件解决造成RecyclerView滚动引发的问题。
代码:
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<Integer> 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<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(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);
}
}
}
相应布局文件:
<?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="100dp"
android:layout_height="match_parent" />
<View
android:layout_width="1px"
android:layout_height="match_parent"
android:background="#e0e0e0" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_2"
android:layout_width="1000dp"
android:layout_height="match_parent" />
</HorizontalScrollView>
</LinearLayout>
附录:
1,《Android若干条并排RecyclerView滑动实时联动》链接:https://blog.csdn.net/zhangphil/article/details/79925803
2,《 Android dispatchTouchEvent检测多点触摸事件是否落入在某一个View区域内》链接:https://blog.csdn.net/zhangphil/article/details/80067150