运用MVP实现二级联动

在这里插入图片描述

具体的代码详见
https://github.com/YangJun1208/liandong

总体的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<RelativeLayout
    android:id="@+id/layout_top"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorPrimary">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:text="购物车"
        android:textColor="#ffffff"
        android:textSize="16sp" />

</RelativeLayout>

<!--左边的列表-->
<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview_shop_type"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_below="@+id/layout_top"
    android:background="#eeeeee" />

<View
    android:id="@+id/view_line"
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:layout_below="@+id/layout_top"
    android:layout_toRightOf="@+id/recyclerview_shop_type"
    android:background="#333333" />

<!--右边的列表-->
<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview_shop"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignRight="@+id/layout_top"
     android:layout_below="@+id/layout_top"
        android:layout_toRightOf="@+id/view_line" />

</RelativeLayout>

MainActivity

public class MainActivity extends AppCompatActivity implements IView {
private IPersenterImpl iPersenter;
private ShopTypeAdapter mShopTypeAdapter;
private ShopTypeProductAdapter mShopTypeProductAdapter;
private RecyclerView mRecyclerViewShopType, mRecyclerViewShop;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    iPersenter=new IPersenterImpl(this);
    initShopTypeView();
    initShopTypeProductView();
    getTypeData();
}

private void getTypeData() {
    Map<String, String> map = new HashMap<>();
    iPersenter.getRequest(Apis.URL_PRODUCT_GET_CATAGORY, map, ShopTypeBean.class);
}

private void initShopTypeProductView() {

    mRecyclerViewShop = findViewById(R.id.recyclerview_shop_type);
    LinearLayoutManager linearLayoutManagerLeft = new LinearLayoutManager(this);
    linearLayoutManagerLeft.setOrientation(LinearLayoutManager.VERTICAL);
    mRecyclerViewShop.setLayoutManager(linearLayoutManagerLeft);
    mRecyclerViewShop.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    mShopTypeAdapter = new ShopTypeAdapter(this);
    mRecyclerViewShop.setAdapter(mShopTypeAdapter);

    //添加接口回调,用来接收左侧recyclerView的cid
    mShopTypeAdapter.setOnClickListener(new ShopTypeAdapter.OnClickListener() {
        @Override
        public void onClick(int position, String cid) {
            //拿到cid之后,通过接口获得对应的数据,展示在右侧列表中
            getShopData(cid);
        }
    });
}

private void initShopTypeView() {

    mRecyclerViewShopType = findViewById(R.id.recyclerview_shop);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    mRecyclerViewShopType.setLayoutManager(linearLayoutManager);

    mRecyclerViewShopType.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

    mShopTypeProductAdapter = new ShopTypeProductAdapter(this);
    mRecyclerViewShopType.setAdapter(mShopTypeProductAdapter);

}

private void getShopData(String cid) {
    Map<String, String> map = new HashMap<>();
    map.put("cid", cid);
    iPersenter.getRequest(Apis.URL_PRODUCT_GET_PRODUCT_CATAGORY, map, ShopTypeProductBean.class);
}

@Override
public void onSuccess(Object data) {

    if (data instanceof ShopTypeBean) {
        //获取数据后,展示左侧列表
        ShopTypeBean shopTypeBean = (ShopTypeBean) data;
        mShopTypeAdapter.setData(shopTypeBean.getData());
    } else if (data instanceof ShopTypeProductBean) {
        //获取数据后,展示右侧列表
        ShopTypeProductBean shopTypeProductBean = (ShopTypeProductBean) data;
        mShopTypeProductAdapter.setData(shopTypeProductBean.getData());

        //将右侧列表滚到顶部(这行不加也无所谓)
        mRecyclerViewShopType.scrollToPosition(0);
    }
}

右边的Adapter的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<TextView
    android:id="@+id/tv_shop_type_product_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:padding="2dp"
    android:textColor="#ffffff"
    android:textSize="16sp" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

      <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerview_shop_product"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </RelativeLayout>
</LinearLayout>

左边的Adapter

public class ShopTypeAdapter extends RecyclerView.Adapter<ShopTypeAdapter.MyViewHolder>{
    private Context mContext;
    private List<ShopTypeBean.Data> mList = new ArrayList<>();
public ShopTypeAdapter(Context context) {
    this.mContext = context;
}

public void setData(List<ShopTypeBean.Data> list){
    this.mList = list;
    notifyDataSetChanged();
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = View.inflate(mContext, R.layout.shop_type_adapter, null);
    MyViewHolder myViewHolder = new MyViewHolder(view);
    return myViewHolder;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {
    myViewHolder.mName.setText(mList.get(i).getName());
    myViewHolder.mLinearLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mOnClickListener != null){
                mOnClickListener.onClick(i, mList.get(i).getCid());
            }
        }
    });
}

@Override
public int getItemCount() {
    return mList.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
    LinearLayout mLinearLayout;
    TextView mName;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        mName = (TextView) itemView.findViewById(R.id.tv_shop_type_name);
        mLinearLayout = itemView.findViewById(R.id.ll_shop_type);
    }
}

private OnClickListener mOnClickListener;

public void setOnClickListener(OnClickListener listener) {
    this.mOnClickListener = listener;
}

public interface OnClickListener {
    void onClick(int position, String cid);
}

右边的总体的Adapter

public ShopTypeProductAdapter(Context context) {
    this.mContext = context;
}

public void setData(List<ShopTypeProductBean.Data> list) {
    this.mList = list;
    notifyDataSetChanged();
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = View.inflate(mContext, R.layout.shop_type_product_adapter, null);
    MyViewHolder myViewHolder = new MyViewHolder(view);
    return myViewHolder;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {
    myViewHolder.mName.setText(mList.get(i).getName());

    //右侧使用RecyclerView嵌套展示效果(这里根据真正的需求自行修改)
    final ShopTypeProductLinearAdapter shopTypeProductLinearAdapter = new ShopTypeProductLinearAdapter(mContext);
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
    myViewHolder.mRecyclerView.setLayoutManager(linearLayoutManager);
    myViewHolder.mRecyclerView.setAdapter(shopTypeProductLinearAdapter);

    myViewHolder.mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));

    shopTypeProductLinearAdapter.setData(mList.get(i).getList());
}

@Override
public int getItemCount() {
    return mList.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
    TextView mName;
    RecyclerView mRecyclerView;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        mName = (TextView) itemView.findViewById(R.id.tv_shop_type_product_name);
        mRecyclerView = itemView.findViewById(R.id.recyclerview_shop_product);
    }
}

右边的子条目Adapter

public class ShopTypeProductLinearAdapter extends RecyclerView.Adapter<ShopTypeProductLinearAdapter.MyViewHolder>{
    private Context mContext;
    private List<ShopTypeProductBean.Data.ProductData> mList = new ArrayList<>();

public ShopTypeProductLinearAdapter(Context context) {
    this.mContext = context;
}

public void setData(List<ShopTypeProductBean.Data.ProductData> list){
    this.mList = list;
    notifyDataSetChanged();
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = View.inflate(mContext, R.layout.shop_type_product_linear_adapter, null);
    MyViewHolder myViewHolder = new MyViewHolder(view);
    return myViewHolder;
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {
    Glide.with(mContext).load(mList.get(i).getIcon()).into(myViewHolder.mImage);
    myViewHolder.mName.setText(mList.get(i).getName());
}

@Override
public int getItemCount() {
    return mList.size();
}

public class MyViewHolder extends RecyclerView.ViewHolder {
    TextView mName;
    ImageView mImage;

    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        mName = itemView.findViewById(R.id.tv_shop_type_product_linear);
        mImage = itemView.findViewById(R.id.iv_shop_type_product_linear);
    }
}

右边自条目的Adapter的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:padding="4dp"
    android:orientation="horizontal">

<ImageView
    android:id="@+id/iv_shop_type_product_linear"
    android:layout_width="40dp"
    android:layout_height="40dp" />

    <TextView
        android:id="@+id/tv_shop_type_product_linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:layout_marginLeft="10dp" />
</LinearLayout>

左边的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_shop_type"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center"
    android:padding="10dp">

    <TextView
        android:id="@+id/tv_shop_type_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

猜你喜欢

转载自blog.csdn.net/yjjhtl/article/details/85107428