RecycleView嵌套与多级列表

首先这并不是多级列表实现的最优方式,多级列表从表现形式上来看无非就是数据+缩进(或者颜色,字体大小等),通过读个RecycleView且套可以实现但是如果列表层级过多则会比较累赘,前面说到的缩进等表现形式完全可以在一个RecycleView中来实现具体可以参考以下链接
[1]: https://www.jianshu.com/p/b76572fb4e60

布局文件

主界面布局




    


主RecycleView的Item的布局




  

  

      
  


子RecycleView的Item的布局




    

这玩意就不多说了,主要讲嵌套中会出现的问题

主RecycleView的适配器

 public class CustomAdapter extends RecyclerView.Adapter {
     private Context context;
     private List data;

     CustomAdapter(Context context, List 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 getData() {
        ArrayList strings = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            strings.add("i=" + i);
        }
        return strings;
    }

    List getData2Inner() {
        ArrayList 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就不贴了,没有什么需要注意的地方改怎么写就怎么写

你可能感兴趣的:(RecycleView嵌套与多级列表)