RecyclerViewでのCardViewの展開と重ね合わせ

4435 ワード

title:RecyclerViewにおけるCardViewの展開とオーバーラップtype:categories date:2016-11-23 16:22:03 categories:Android
tags: [recyclerView, CardView]
RecyclerViewでのCardViewの展開とオーバーラップ効果.
効果図
インプリメンテーション
本論文DEMOhttps://github.com/zhangzhaopds/FlexbleCell.git
プライマリコード
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements View.OnClickListener {

    @Override
    public int getItemCount() {
        return 20;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View cell = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_main_recyclerview, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(cell);

        //     cell      
        cell.setOnClickListener(this);

        //   viewHolder     (   )
        myViewHolder.setIsRecyclable(false);

        return myViewHolder;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        MyViewHolder(View view) {
            super(view);
        }
    }

    @Override
    public void onClick(View v) {

        int index;
        //                  
        LinearLayout linearLayout = (LinearLayout)v.findViewById(R.id.cell_main_rootLayout);
        View subView = LayoutInflater.from(v.getContext()).inflate(R.layout.cell_main_recyclerview_add, (ViewGroup)v, false);

        //   cell   Tag    cell      ,           ,cell            ,Tag       。
        //        ,                     ,                。
        if (v.findViewById(R.id.cell_main_rootLayout).getTag() == null) {
            index = 1;
        } else {
            index = (int)v.findViewById(R.id.cell_main_rootLayout).getTag();
        }

        // close  :     
        if (index == 1) {
            linearLayout.addView(subView);
            subView.setTag(1000);
            v.findViewById(R.id.cell_main_rootLayout).setTag(2);
        } else {
            // open  :     
            linearLayout.removeView(v.findViewWithTag(1000));
            v.findViewById(R.id.cell_main_rootLayout).setTag(1);
        }
    }
}