首先我这里用到的布局文件
用到的是RecyclerView和ScrollView组合
<LinearLayout 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=".ui.fragment.BlankFragment2"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="3"
>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="国际名牌"
android:gravity="center_horizontal"
android:textSize="20sp"
android:textColor="#000"
android:layout_weight="1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:text="66666"
android:textColor="#000"
android:layout_weight="0.02"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/LeftRecyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="15"
android:layout_marginTop="2dp"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
<ScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
>
<LinearLayout
android:id="@+id/mLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
android:orientation="vertical"
></LinearLayout>
</ScrollView>
<LinearLayout 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=".ui.fragment.BlankFragment2"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="3"
>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="国际名牌"
android:gravity="center_horizontal"
android:textSize="20sp"
android:textColor="#000"
android:layout_weight="1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:text="66666"
android:textColor="#000"
android:layout_weight="0.02"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/LeftRecyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="15"
android:layout_marginTop="2dp"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
<ScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
>
<LinearLayout
android:id="@+id/mLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
android:orientation="vertical"
></LinearLayout>
</ScrollView>
获取RecyclerView和ScrollView内部的LinearLayout的控件的id
leftRecyclerView = inflate.findViewById(R.id.LeftRecyclerView);
mLinearLayout = inflate.findViewById(R.id.mLinearLayout);
在左侧的RecyclerView通过自定义接口回调设置点击事件将两个接口对应的id值传过来
Adapter中的或左侧id
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
leftAdapterListener.success(list.get(i).getCid()+"");
}
});
获取左侧的地址自定义一个变量获取传回来的id值
leftAdapter.setLeftAdapterListener(new LeftAdapter.LeftAdapterListener() {
@Override
public void success(String data) {
cid = data;
initRight();
}
});
右侧的数据获取完成后进行右侧的展示
//请空LinearLayout中的子空间
mLinearLayout.removeAllViews();
//判断集合是否为空
if(data1 != null){
//遍历集合
for (int i = 0 ; i < data1.size() ; i++){
//动态创建一个textView
TextView textView = new TextView(getContext());
textView.setTextSize(20);
textView.setTextColor(R.color.hui);
//给每个组的textview的标题赋值
textView.setText(data1.get(i).getName());
//动态创建 RecyclerView
RecyclerView recyclerView = new RecyclerView(getContext());
//设置布局管理器
recyclerView.setLayoutManager(new GridLayoutManager(getContext(),3));
RightAdapter rightAdapter = new RightAdapter(data1,getActivity());
//设置适配器
recyclerView.setAdapter(rightAdapter);
//因为每次切换都要重新加载布局中的数据.所以要刷新适配器
rightAdapter.notifyDataSetChanged();
//添加动态生成的控件
mLinearLayout.addView(textView);
mLinearLayout.addView(recyclerView);
}
}