1.recyclerview嵌入recyclerview根据数据源实现多级级联选择

2。布局文件

main_avtivity.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5     <include
 6         android:id="@+id/show_title"
 7         layout="@layout/new_title_project"/>
 8     <!-- 最外层-->
 9 <LinearLayout
10     android:layout_marginTop="15dp"
11     android:layout_below="@+id/show_title"
12     android:orientation="vertical"
13     android:background="@drawable/stroke"
14     android:layout_width="match_parent"
15     android:layout_height="wrap_content">
16     <android.support.v7.widget.RecyclerView
17         android:id="@+id/recycle_item"
18         android:layout_width="match_parent"
19         android:layout_height="wrap_content">
20         
21     </android.support.v7.widget.RecyclerView>
22   
23 </LinearLayout>
24 </RelativeLayout>
View Code

recyclerview_activity.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="wrap_content">
 5        <RelativeLayout
 6        android:gravity="center_vertical"
 7        android:background="@color/new_light_blue"
 8        android:id="@+id/withholding_content"
 9        android:layout_width="match_parent"
10        android:layout_height="50dp"
11        android:orientation="horizontal"
12        android:paddingLeft="20dp"
13        android:paddingRight="10dp">
14 
15        <TextView
16            android:id="@+id/mentioned_about_single"
17        android:gravity="center_vertical"
18            android:layout_width="wrap_content"
19            android:layout_height="match_parent"
20           android:textSize="20dp"
21            android:textColor="#fff"
22            android:text="关于提单" />
23       
24    </RelativeLayout>
25     <android.support.v7.widget.RecyclerView
26         android:visibility="gone"
27         android:id="@+id/problems_items"
28         android:layout_width="match_parent"
29         android:layout_height="wrap_content">
30         
31     </android.support.v7.widget.RecyclerView>
32    
33  
34 </LinearLayout>
View Code

recyclerview_activity_item.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="wrap_content">
 5     <LinearLayout
 6         android:id="@+id/lading_all_button"
 7         android:padding="10dp"
 8         android:background="#fff"
 9         android:orientation="vertical"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content">
12         <LinearLayout
13             android:layout_margin="10dp"
14             android:orientation="horizontal"
15             android:layout_width="match_parent"
16             android:layout_height="wrap_content">
17             <TextView
18                 android:id="@+id/the_sequence"
19                 android:textColor="@color/new_sky_blue_name"
20                 android:layout_width="wrap_content"
21                 android:layout_height="wrap_content"
22                 android:text="1."
23                 />
24             <TextView
25                 android:id="@+id/howdo_lading"
26                 android:layout_width="match_parent"
27                 android:layout_height="wrap_content"
28                 android:textColor="@color/new_sky_blue_name"
29                 android:text="怎么提单"
30                 />
31         </LinearLayout>
32         <View
33             android:layout_marginLeft="20dp"
34             android:layout_marginRight="20dp"
35             android:background="@color/new_wb_split"
36             android:layout_width="match_parent"
37             android:layout_height="1dp"/>
38         <TextView
39             android:id="@+id/lading_content"
40             android:layout_marginTop="10dp"
41             android:layout_marginLeft="20dp"
42             android:layout_width="match_parent"
43             android:layout_height="wrap_content"
44             android:textSize="15dp"
45             android:textColor="@color/new_ui_item_content"
46             android:text="每一秒钟,都有用户通过简书创作出新的精彩,并与相同兴趣的人们在社区内进行互动。简书提供了最优雅的界面和最好的分享体验,包罗万象的专题,使得任何用户都可以在这个社区内创作,与同好交流。"/>
47     </LinearLayout>
48 </LinearLayout>
View Code

3.主main以及两个adapter适配器

1.mianActiviry.java

public class MainActivity extends AppCompatActivity {
    RecyclerView recycleItem
    List list;//数据源
OtherProblemsAdapter problemsAdapter ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recycleItem= (RecyclerView)findViewById(R.id.recycle_item);//最原始的id查找
      list = new ArrayList();
        SetRecycleItem();
}
private void SetRecycleItem() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleItem.setLayoutManager(manager);
        recycleItem.addItemDecoration(new RecycleViewDivider(this, RecyclerView.HORIZONTAL, DensityUtil.dip2px(this, 5), getResources().getColor(R.color.gray_bg)));
        problemsAdapter = new OtherProblemsAdapter(this, list);
        recycleItem.setAdapter(problemsAdapter);

    }
    
}
View Code

2.OtherProblemsAdapter.java

public class OtherProblemsAdapter extends RecyclerView.Adapter<OtherProblemsAdapter.MyViewHolder> {
    private boolean isShowGroupItem = false;
    private List list;
    private Context context;

    public OtherProblemsAdapter(Context context, List list) {
        this.list = list;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        DBLog.i(TAG,"这是第一个适配器");
        View view = LayoutInflater.from(context).inflate(R.layout.recyclerview_activity, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
    //list有数据的情况下获取数据设置到相应id填充内容
        //创建默认的线性LayoutManager
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(context);
        //显示底部位置
        mLayoutManager.setStackFromEnd(true);
        holder.problemsItems.setLayoutManager(mLayoutManager);
        holder.problemsItems.setHasFixedSize(true);
         AdapterItems adapterItems = new AdapterItems(list);//Recyclerview加入adapterItems子布局
        holder.problemsItems.addItemDecoration(new DividerItemDecoration(context,DividerItemDecoration.HORIZONTAL));
        holder.problemsItems.setAdapter(adapterItems);
        holder.problemsItems.setVisibility(View.GONE);
        isShowGroupItem = false;
        adapterItems.addOnItemClickListener(new AdapterItems.OnRecyclerItemClickListener() {
            @Override
            public void onItemClick(View view, List data, int position) {
                Toast.makeText(context, "Item " + position + " Click!", Toast.LENGTH_LONG).show();
            }
        });
         holder.withholdingContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DBLog.i(TAG,"列表显示隐藏按钮");
                if (!isShowGroupItem) {
                    holder.problemsItems.setVisibility(View.VISIBLE);
                    isShowGroupItem = true;
                } else {
                    holder.problemsItems.setVisibility(View.GONE);
                    isShowGroupItem = false;
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return 3;//布局文件的条数或者使用mainactivit类传入过来的listlist.size()
    }
    

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.mentioned_about_single)
        TextView mentionedAboutSingle;
        @BindView(R.id.withholding_content)
        RelativeLayout withholdingContent;
        @BindView(R.id.problems_items)
        RecyclerView problemsItems;
        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);//butterknife库 快捷使用自动生成id工具
            
        }
    }

    /**
     * 清除数据
     */
    public void clear() {
        if (null != list && list.size() > 0) {
            this.list.clear();
            this.notifyDataSetChanged();
        }
    }

    /**
     * 添加数据
     */
    public void addData(List listAll) {
        if (null != listAll && listAll.size() > 0) {
            this.list.addAll(listAll);
            this.notifyDataSetChanged();
        }
    }

}
View Code

3.AdapterItems.java

public class AdapterItems extends RecyclerView.Adapter<AdapterItems.ViewHolder> implements View.OnClickListener {
  
    private List list;
    private OnRecyclerItemClickListener mOnItemClickListener;

    public AdapterItems(List list) {
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        DBLog.i(TAG,"这是第二个适配器列表");
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_activity_item, parent, false);
        view.setOnClickListener(this);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

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


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

    @Override
    public void onClick(View view) {
        //使用getTag方法获取数据
        if (mOnItemClickListener != null){
          
        }
          
    }

    /**
     * item点击事件
     *
     * @param listener
     */
    public void addOnItemClickListener(OnRecyclerItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.the_sequence)
        TextView theSequence;
        @BindView(R.id.howdo_lading)
        TextView howdoLading;
        @BindView(R.id.lading_content)
        TextView ladingContent;
        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnRecyclerItemClickListener {
        void onItemClick(View view, List data, int position);
    }
}
View Code