在Android 6.0 解决recyclerview 在 scrollview 中不能全部显示,你可以看到只显示一个条目,解决方法如下:
- scrollView只嵌套一个recyclerView:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.RecyclerView
android:id="@+id/info_detail_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white" />
</RelativeLayout>
在recycleview的父布局加一个属性:
android:descendantFocusability="blocksDescendants"
该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。
属性的值有三种:
beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
当然你可以直接在recycleview加一个RelativeLayout父布局
scrollView嵌套recyclerView 再嵌套recyclerView
这是你需要在最外层的recyclerView 执行上面的操作就可以了。
- 如果这个时候你发现recycleview上下滑动的时候有点卡顿的话,可以使用下面的ScrollView
package tsou.com.equipmentonline.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.ScrollView;
/**
* 自定义ScrollView嵌套recycleview
*/
public class MyScrollView extends ScrollView {
private int downX;
private int downY;
private int mTouchSlop;
public MyScrollView(Context context) {
super(context);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
int action = e.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
downX = (int) e.getRawX();
downY = (int) e.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int moveY = (int) e.getRawY();
if (Math.abs(moveY - downY) > mTouchSlop) {
return true;
}
}
return super.onInterceptTouchEvent(e);
}
}