1.生成两个bean类
2.展示的presenter层
public class ShowPresenter{
private ShowView showView;
public ShowPresenter(ShowView showView){
this.showView=showView;
}
public void LoadDataNet(){
new Thread(){
@Override
public void run() {
super.run();
HttpUtils.getInstance().doGet(Contacts.path1, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
showView.getShopData(string);
}
});
}
}.start();
}
public void Loadgoos(){
new Thread(){
@Override
public void run() {
super.run();
HttpUtils.getInstance().doGet(Contacts.path2, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
showView.getGoodsData(string);
}
});
}
}.start();
}
//内存泄漏
public void detachview(){
if (showView!=null){
showView=null;
}
}
}
3.activity页面
public class ShowActivity extends AppCompatActivity {
private RadioGroup radioGroup;
private FragmentManager fragmentManager;
private FragmentOne fragmentOne;
private FragmentTwo fragmentTwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
radioGroup = findViewById(R.id.radioGroup);
radioGroup.check(radioGroup.getChildAt(0).getId());
fragmentManager = getSupportFragmentManager();
fragmentOne = new FragmentOne();
fragmentTwo = new FragmentTwo();
fragmentManager.beginTransaction()
.add(R.id.frag, fragmentOne)
.add(R.id.frag, fragmentTwo)
.show(fragmentOne)
.hide(fragmentTwo)
.commit();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.radio0:
fragmentManager.beginTransaction()
.show(fragmentOne)
.hide(fragmentTwo)
.commit();
break;
case R.id.radio1:
fragmentManager.beginTransaction()
.show(fragmentTwo)
.hide(fragmentOne)
.commit();
break;
}
}
});
}
}
//布局
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/radio0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:text="首页"
android:gravity="center"
android:layout_marginLeft="32dp"
/>
<RadioButton
android:id="@+id/radio1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="购物车"
/>
</RadioGroup>
//左边的适配器 public class ShowAdapter extends RecyclerView.Adapter
}
//右边的适配器
public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.MyViewHolder>{
private Context context;
private List<GoodsBean.DataBean> list1;
public GoodsAdapter(Context context, List<GoodsBean.DataBean> list1) {
this.context = context;
this.list1 = list1;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.sitem1, null);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
myViewHolder.text.setText(list1.get(i).getName());
myViewHolder.rcv.setLayoutManager(new GridLayoutManager(context,3,LinearLayoutManager.VERTICAL,false));
ChildAdapter childAdapter = new ChildAdapter(context, list1.get(i).getList());
myViewHolder.rcv.setAdapter(childAdapter);
}
@Override
public int getItemCount() {
return list1.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
private final TextView text;
private final RecyclerView rcv;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
text = itemView.findViewById(R.id.textView);
rcv = itemView.findViewById(R.id.rcv);
}
}
}
///fragmentone页面
public class FragmentOne extends Fragment implements ShowView {
private RecyclerView recyclerView;
private ShowPresenter showPresenter;
private LinearLayoutManager linearLayoutManager;
private RecyclerView recycler_right;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag1, container, false);
recyclerView = view.findViewById(R.id.recyclerView);
recycler_right = view.findViewById(R.id.recycler_right);
showPresenter = new ShowPresenter(this);
//设置布局
linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayout.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
showPresenter.LoadDataNet();
LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(getActivity());
linearLayoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
recycler_right.setLayoutManager(linearLayoutManager1);
showPresenter.Loadgoos();
return view;
}
@Override
public void getShopData(String s) {
Message message = new Message();
message.what = 0;
message.obj = s;
handler.sendMessage(message);
}
@Override
public void getGoodsData(String s) {
Message message = new Message();
message.what =1;
message.obj = s;
handler.sendMessage(message);
}
@Override
public void onDestroy() {
super.onDestroy();
showPresenter.detachview();
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0) {
String s = (String) msg.obj;
Gson gson = new Gson();
ShopBean shopBean = gson.fromJson(s, ShopBean.class);
List<ShopBean.DataBean> list = shopBean.getData();
getDate(list);
}else if (msg.what==1){
String s= (String) msg.obj;
Gson gson = new Gson();
GoodsBean goodsBean = gson.fromJson(s, GoodsBean.class);
List<GoodsBean.DataBean> list1 = goodsBean.getData();
getDate1(list1);
}
}
};
private void getDate1(List<GoodsBean.DataBean> list1) {
GoodsAdapter goodsAdapter = new GoodsAdapter(getActivity(), list1);
recycler_right.setAdapter(goodsAdapter);
}
private void getDate(List<ShopBean.DataBean> list) {
ShowAdapter showAdapter = new ShowAdapter(getActivity(), list);
recyclerView.setAdapter(showAdapter);
}
}
//接口
public interface ShowView {
void getShopData(String s);
void getGoodsData(String s);
}
//show.xml
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/radio0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:text="首页"
android:gravity="center"
android:layout_marginLeft="32dp"
/>
<RadioButton
android:id="@+id/radio1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:gravity="center"
android:text="购物车"
/>
</RadioGroup>
//条目布局适配器1
<android.support.v7.widget.RecyclerView
android:id="@+id/rcv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
//条目布局适配器2
//frag1布局
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width=“150dp”
android:layout_height=“match_parent”>
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_right"
android:layout_width=“match_parent”
android:layout_height=“match_parent”></android.support.v7.widget.RecyclerView>