重写子RecycleView
代码如下:
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import androidx.recyclerview.widget.RecyclerView;
import com.xiaoniu.hulumusic.utils.Apputils;
public class NestChildRecyclerView extends RecyclerView {
public NestChildRecyclerView(Context context) {
this(context, null);
}
public NestChildRecyclerView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public NestChildRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
getParent().requestDisallowInterceptTouchEvent(true);
Log.d("test","---onInterceptTouchEvent---ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.d("test","---onInterceptTouchEvent---ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
Log.d("test","---onInterceptTouchEvent---ACTION_CANCEL");
getParent().requestDisallowInterceptTouchEvent(false);
break;
}
return super.onInterceptTouchEvent(ev);
}
}
布局如下:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingHorizontal="@dimen/dp_15">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:lines="1"
android:ellipsize="end"
android:textColor="@color/dark_text"
tools:text="test"
android:textSize="18dp" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBaseline_toBaselineOf="@id/tv_search_history"
android:text="demo"/>
<com.demo.NestChildRecyclerView
android:id="@+id/recycler_view"
android:layout_marginTop="10dp"
app:layout_constraintTop_toBottomOf="@id/tv_search_history"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.constraintlayout.widget.ConstraintLayout>
注意:
处于父RecyclerView下的子RecyclerView的父布局高一定要设置为match_parent;不然无法滑动