1.嵌套GridView
RecyclerView非常方便使用,每个Item可以嵌套不同的布局,通过
public int getItemViewType(int position)
可以获取到每个Item的布局类型。想在其中的一个Item就是GridView控件。大家知道,多个含有滑动效果的控件嵌套在一起,很容易出现滑动冲突的问题。一般的解决办法:把内层的控件布局重新计算后再绘制出来。也就是把GridView需要显示的内容全部显示出来,而不用滑动事件。只需要自定义各一个GridView即可。
/**
* Author: aaa
* Date: 2017/1/16 17:15.
*/
public class MyGridView extends GridView {
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
布局文件
<MyGridView
android:id="@+id/recyclerview_product"
android:numColumns="2"
android:layout_marginTop="7dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dip"
android:layout_marginRight="12dp"
android:layout_marginLeft="12dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
每行只显示两个Item,如果有四个项目,那么就显示两行;如果有八个项目,就显示四行。而且不如外部的RecyclerView的滑动相冲突。
2.RecyclerView复用
如果不想RecyclerView中的Item复用,而是每次都重新显示,只需加上以下这句话:
itemViewHolder.setIsRecyclable(false);//false - 禁止复用 true-可以复用
可以解决RecyclerView由于复用而造成的显示错乱问题