首先写MVP框架 跟上一篇一样 所以直接看上一篇
两个布局文件 左边一个 右边一个
这个是左边的
recycler_left_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/left_text" android:layout_width="100dp" android:layout_height="50dp" android:textSize="16sp" android:gravity="center" android:text="aa" /> </LinearLayout>
这个是右边的
recycler_right_item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:orientation="horizontal"> <ImageView android:id="@+id/image" android:layout_width="100dp" android:layout_height="wrap_content" android:adjustViewBounds="true" android:minHeight="50dp" android:layout_alignParentLeft="true" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/text" android:layout_toRightOf="@+id/image" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="aa" android:padding="10dp"/> <TextView android:id="@+id/text_price" android:layout_toRightOf="@+id/image" android:layout_below="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="价格" android:padding="10dp"/> <com.example.yuekao8.util.view.AddSubLayout android:id="@+id/add_sub_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_marginRight="20dp" android:layout_marginBottom="20dp"> </com.example.yuekao8.util.view.AddSubLayout> </RelativeLayout>
适配器也是一样
LeftAdapter
public class LeftAdapter extends RecyclerView.Adapter<LeftAdapter.MyHolder> { private List<Shop> mList = new ArrayList<>(); public void addAll(List<Shop> list){ mList.addAll(list); } @NonNull @Override public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = View.inflate(viewGroup.getContext(), R.layout.recycler_left_item,null); MyHolder myHolder = new MyHolder(view); return myHolder; } @Override public void onBindViewHolder(@NonNull final MyHolder myHolder, int i) { final Shop shop = mList.get(i); myHolder.text.setText(shop.getSellerName()); myHolder.text.setBackgroundResource(shop.getBackground()); myHolder.text.setTextColor(shop.getTextColor()); myHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { for (int j = 0; j <mList.size() ; j++) { mList.get(j).setTextColor(0xffffffff); mList.get(j).setBackground(R.color.colorPrimary); } shop.setBackground(R.color.colorPrimary); shop.setTextColor(0xff000000); notifyDataSetChanged(); onItemClickListenter.onItemClick(shop);//切换右边的列表 } }); } @Override public int getItemCount() { return mList.size(); } public List<Shop> getList() { return mList; } class MyHolder extends RecyclerView.ViewHolder{ TextView text; public MyHolder(@NonNull View itemView) { super(itemView); text = itemView.findViewById(R.id.left_text); } } private OnItemClickListenter onItemClickListenter; public void setOnItemClickListenter(OnItemClickListenter onItemClickListenter) { this.onItemClickListenter = onItemClickListenter; } public interface OnItemClickListenter{ void onItemClick(Shop shop); } }
RightAdapter
public class RightAdapter extends RecyclerView.Adapter<RightAdapter.ChildHolder> { private List<Goods> mList = new ArrayList<>(); public void addAll(List<Goods> list) { mList.addAll(list); } @NonNull @Override public ChildHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) { View view = View.inflate(viewGroup.getContext(), R.layout.recycler_right_item, null); ChildHolder myHolder = new ChildHolder(view); return myHolder; } @Override public void onBindViewHolder(@NonNull ChildHolder childHolder, int position) { final Goods goods = mList.get(position); childHolder.text.setText(goods.getTitle()); childHolder.price.setText("单价:" + goods.getPrice());//单价 String imageurl = "https" + goods.getImages().split("https")[1]; Log.i("dt", "imageUrl: " + imageurl); imageurl = imageurl.substring(0, imageurl.lastIndexOf(".jpg") + ".jpg".length()); Glide.with(DTApplication.getInstance()).load(imageurl).into(childHolder.image);//加载图片 childHolder.addSub.setCount(goods.getNum());//设置商品数量 childHolder.addSub.setAddSubListener(new AddSubLayout.AddSubListener() { @Override public void addSub(int count) { goods.setNum(count); onNumListener.onNum();//计算价格 } }); } @Override public int getItemCount() { return mList.size(); } public void clearList() { mList.clear(); } class ChildHolder extends RecyclerView.ViewHolder { TextView text; TextView price; ImageView image; AddSubLayout addSub; public ChildHolder(@NonNull View itemView) { super(itemView); text = itemView.findViewById(R.id.text); price = itemView.findViewById(R.id.text_price); image = itemView.findViewById(R.id.image); addSub = itemView.findViewById(R.id.add_sub_layout); } } private OnNumListener onNumListener; public void setOnNumListener(OnNumListener onNumListener) { this.onNumListener = onNumListener; } public interface OnNumListener{ void onNum(); } }
到主页面了
布局
<?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="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="horizontal"> <android.support.v7.widget.RecyclerView android:id="@+id/left_recycler" android:layout_width="100dp" android:layout_height="match_parent" > </android.support.v7.widget.RecyclerView> <android.support.v7.widget.RecyclerView android:id="@+id/right_recycler" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="80dp"> <ImageView android:id="@+id/shop_car_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:src="@drawable/gouwuc_r" /> <TextView android:id="@+id/goods_sum_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="价格:" android:layout_marginLeft="20dp" android:layout_centerVertical="true"/> <TextView android:id="@+id/goods_number" android:layout_width="30dp" android:layout_height="30dp" android:textSize="10sp" android:gravity="center" android:layout_marginLeft="-10dp" android:layout_alignParentTop="true" android:text="7" /> </RelativeLayout> </LinearLayout>
主页面
public class ShopActivity2 extends AppCompatActivity implements DataCall<List<Shop>> { private TextView mSumPrice; private TextView mCount; private RecyclerView mLeftRecycler,mRightRecycler; private LeftAdapter mLeftAdapter; private RightAdapter mRightAdapter; private CartPresenter cartPresenter = new CartPresenter(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.shop2); mSumPrice = findViewById(R.id.goods_sum_price); mCount = findViewById(R.id.goods_number); mLeftRecycler = findViewById(R.id.left_recycler); mRightRecycler = findViewById(R.id.right_recycler); mLeftRecycler.setLayoutManager(new LinearLayoutManager(this)); mRightRecycler.setLayoutManager(new LinearLayoutManager(this)); mLeftAdapter = new LeftAdapter(); mLeftAdapter.setOnItemClickListenter(new LeftAdapter.OnItemClickListenter() { @Override public void onItemClick(Shop shop) { mRightAdapter.clearList();//清空数据 mRightAdapter.addAll(shop.getList()); mRightAdapter.notifyDataSetChanged(); } }); mLeftRecycler.setAdapter(mLeftAdapter); mRightAdapter = new RightAdapter(); mRightAdapter.setOnNumListener(new RightAdapter.OnNumListener() { @Override public void onNum() { calculatePrice(mLeftAdapter.getList()); } }); mRightRecycler.setAdapter(mRightAdapter); cartPresenter.requestData(); } @Override public void success(List<Shop> data) { calculatePrice(data);//计算价格和数量 mLeftAdapter.addAll(data);//左边的添加类型 //得到默认选中的shop,设置上颜色和背景 Shop shop = data.get(1); shop.setTextColor(0xff000000); shop.setBackground(R.color.colorPrimary); mRightAdapter.addAll(shop.getList()); mLeftAdapter.notifyDataSetChanged(); mRightAdapter.notifyDataSetChanged(); } @Override public void fail(Result result) { Toast.makeText(this, result.getCode() + " " + result.getMsg(), Toast.LENGTH_LONG).show(); } /** * @author dingtao * @date 2018/12/18 7:01 PM * 计算总价格 */ private void calculatePrice(List<Shop> shopList){ double totalPrice=0; int totalNum = 0; for (int i = 0; i < shopList.size(); i++) {//循环的商家 Shop shop = shopList.get(i); for (int j = 0; j < shop.getList().size(); j++) { Goods goods = shop.getList().get(j); //计算价格 totalPrice = totalPrice + goods.getNum() * goods.getPrice(); totalNum+=goods.getNum();//计数 } } mSumPrice.setText("价格:"+totalPrice); mCount.setText(""+totalNum); } }