首先这并不是多级列表实现的最优方式,多级列表从表现形式上来看无非就是数据+缩进(或者颜色,字体大小等),通过读个RecycleView且套可以实现但是如果列表层级过多则会比较累赘,前面说到的缩进等表现形式完全可以在一个RecycleView中来实现具体可以参考以下链接
[1]: https://www.jianshu.com/p/b76572fb4e60
布局文件
主界面布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="rar.uchannel.com.recyclelviewandrecycleview.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
主RecycleView的Item的布局
<?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="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="30dp"
android:background="@color/colorPrimary"
android:gravity="center" />
<LinearLayout
android:id="@+id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycleView_son"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
子RecycleView的Item的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_other"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_other"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
这玩意就不多说了,主要讲嵌套中会出现的问题
主RecycleView的适配器
public class CustomAdapter extends RecyclerView.Adapter<CustomViewHolder> {
private Context context;
private List<String> data;
CustomAdapter(Context context, List<String> data) {
this.context = context;
this.data = data;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new CustomViewHolder(LayoutInflater.from(context).inflate(R.layout.item_for_recycleview, parent, false));
}
@Override
public void onBindViewHolder(final CustomViewHolder holder, final int position) {
holder.textView.setText(data.get(position));
if (onItemClickListener != null) {
holder.textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("xxx", holder.isRecyclable() + "");
int pos = holder.getLayoutPosition();
onItemClickListener.onItemClick(holder, pos);
holder.setIsRecyclable(false);//view复用造成一处点击多处响应的问题 设置为被点击的item不可以被复用,
Log.e("xxx", holder.isRecyclable() + "");
}
});
}
}
@Override
public int getItemCount() {
return data.size();
}
void setOnItemClickListener(OnItemClickListener itemClickListener) {
this.onItemClickListener = itemClickListener;
}
private OnItemClickListener onItemClickListener;
interface OnItemClickListener {
void onItemClick(CustomViewHolder holder, int position);
}
}
问题1:点击RecycleView中的某个Item会出现多个Item响应的问题,可能有规律。
解决:RecycleView中的点击事件不要直接在Adapter中实现,通过回调传入在外部实现,回调接口中参数为ViewHolder是应为某些情况下点击事件有一个控件响应之后去修改其他控件的内容。可以修改为对应View
问题2:点击一个RecycleView中的Item展开其次级列表时后面的Item也会复用当前已经展开的Item(这个跟上面有点类似)
解决: 这里解决方案比较多设置Tag之类的都是可行的,究其原理无非是展开的Item不进行复用,这里采用的方式是直接设置Item不可复用holder.setIsRecyclable(false);
这里仅仅对单个已经点击的item有效,而且当该item画出屏幕外再次滑动回到屏幕内时其复用状态会重新更新为可以复用,可以看到代码中的两个Log,当Item出现在屏幕内首次点击之前该Item都是可以复用的,对性能影响较小。
这里滑动冲突不需要进行处理,应为RecycleView布局使用的是
android:layout_height="wrap_content"
如果各位在且套的时候要求内部子RecycleView长度固定且可以滑动就需要通过setScrollEnabled
处理滑动冲突,(额,直说吧这里我根本没尝试...)
MainActivity代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
List<String> getData() {
ArrayList<String> strings = new ArrayList<>();
for (int i = 0; i < 100; i++) {
strings.add("i=" + i);
}
return strings;
}
List<String> getData2Inner() {
ArrayList<String> strings = new ArrayList<>();
for (int i = 0; i < 5; i++) {
strings.add("i=" + i);
}
return strings;
}
void initView() {
RecyclerView recyclerView = findViewById(R.id.recycleView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
final CustomAdapter adapter = new CustomAdapter(this, getData());
adapter.setOnItemClickListener(new CustomAdapter.OnItemClickListener() {
@Override
public void onItemClick(CustomViewHolder holder, int position) {
LinearLayoutManager llm = new LinearLayoutManager(MainActivity.this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
InnerAdapter innerAdapter = new InnerAdapter(MainActivity.this, getData2Inner());
holder.recyclerView.setLayoutManager(llm);
holder.recyclerView.setAdapter(innerAdapter);
ViewGroup.LayoutParams layoutParams = holder.linearLayout.getLayoutParams();
layoutParams.height = 0;
holder.linearLayout.setLayoutParams(layoutParams);
startAnimation(holder.linearLayout);
}
});
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(linearLayoutManager);
}
public void startAnimation(final View view) {
ValueAnimator animator;
if (view.getHeight() == 0) {
animator = ValueAnimator.ofInt(0, getMeasureHeight(view));
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.height = (int) animation.getAnimatedValue();
view.setLayoutParams(layoutParams);
}
});
} else {//if (view.getHeight() == getMeasureHeight(view))
animator = ValueAnimator.ofInt(view.getHeight(), 0);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.height = (int) animation.getAnimatedValue();
view.setLayoutParams(layoutParams);
}
});
}
animator.setDuration(1000);
animator.start();
}
/**
* 测量控件的显示的时候的真实高度
*
* @param view target view
* @return target view real height
*/
public int getMeasureHeight(View view) {
int w = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
int h = View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED);
view.measure(w, h);
return view.getMeasuredHeight();
}
}
其他ViewHolder和子Adapter就不贴了,没有什么需要注意的地方改怎么写就怎么写