首先,在开始写程序之前先添加design包,导包步骤如下:
1.第一步
2.第二步
3.第三步
经过以上的三步,你就添加了design的包了,(添加其他的包也可以用这个方法来添加,简单快捷)
下面开始两个RecyclerView嵌套的使用,直接上代码
父布局的代码
<?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"
>
<TextView
android:id="@+id/tv_name_parent"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>
子布局的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_name_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:padding="10dp" />
</LinearLayout>
父布局Adapter
public class ParentRecyclerAdapter extends RecyclerView.Adapter<ParentViewHolder> {
private Context context;
private OnLayoutListenter listenter;
private List<String> list_name_parent;
public ParentRecyclerAdapter(Context context,
List<String> list_name_parent) {
this.context = context;
this.list_name_parent = list_name_parent;
}
@Override
public ParentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ParentViewHolder(LayoutInflater.from(context)
.inflate(R.layout.layout_recycelr_parent, parent, false));
}
@Override
public void onBindViewHolder(ParentViewHolder holder, int position) {
holder.tv_name_parent.setText(list_name_parent.get(position));
getListenter().layoutChild(holder, position);
}
@Override
public int getItemCount() {
return list_name_parent.size();
}
public OnLayoutListenter getListenter() {
return listenter;
}
public void setListenter(OnLayoutListenter listenter) {
this.listenter = listenter;
}
}
子布局Adapter
public class ChildRecyclerAdapter extends RecyclerView.Adapter<ChildViewHolder> {
private Context context;
private List<String> list_nam_child;
public ChildRecyclerAdapter(Context context,
List<String> list_nam_child) {
this.context = context;
this.list_nam_child = list_nam_child;
}
@Override
public ChildViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ChildViewHolder(LayoutInflater.from(context)
.inflate(R.layout.layout_recycelr_child, parent, false));
}
@Override
public void onBindViewHolder(ChildViewHolder holder, int position) {
holder.tv_name_child.setText(list_nam_child.get(position));
}
@Override
public int getItemCount() {
return list_nam_child.size();
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView rv_parent;
private ParentRecyclerAdapter adapterParent;
private ChildRecyclerAdapter adapterChild;
private List<String> list_name_parent;
private List<String> list_name_child;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
rv_parent = (RecyclerView) findViewById(R.id.rv_parent);
rv_parent.setLayoutManager(new LinearLayoutManager(this));
rv_parent.addItemDecoration(
new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
adapterParent = new ParentRecyclerAdapter(MainActivity.this, list_name_parent);
adapterParent.setListenter(new OnLayoutListenter() {
@Override
public void layoutChild(ParentViewHolder holder, int position) {
holder.rv_child.setLayoutManager(new GridLayoutManager(MainActivity.this, 4,
LinearLayoutManager.VERTICAL, false));
holder.rv_child.addItemDecoration(
new DividerItemDecoration(MainActivity.this,
LinearLayoutManager.HORIZONTAL));
adapterChild = new ChildRecyclerAdapter(MainActivity.this, list_name_child);
holder.rv_child.setAdapter(adapterChild);
}
});
rv_parent.setAdapter(adapterParent);
}
private void initData() {
list_name_parent = new ArrayList<>();
list_name_child = new ArrayList<>();
for (int i = 0; i < 10; i++) {
list_name_parent.add("我是父布局:" + i);
}
for (int j = 0; j < 3; j++) {
list_name_child.add("我是子布局:" + j);
}
}
}