1. 程式人生 > >recycleview購物車

recycleview購物車

1.mainactivity

public class MainActivity extends BaseActivity implements IView {

private String mUrl="http://www.wanandroid.com/tools/mockapi/6523/restaurant-list";
private RecyclerView mLeftRecy;
private RecyclerView mRightRecy;
private ArrayList<MyData.DataBean> mList=new ArrayList<>();
private ArrayList<MyData.DataBean.SpusBean> mRightList=new ArrayList<>();
private LinearLayoutManager manager;
private LeftAdapter mLeftAdapter;
private PresenterImpl presenter;
private RightAdapter mRightAdapter;
private LinearLayoutManager manager1;

@Override
protected int getLayout() {
    return R.layout.activity_main;
}

@Override
protected void initView() {
    mLeftRecy = findViewById(R.id.Left_Recy);
    mRightRecy = findViewById(R.id.Right_Recy);

    //左邊的介面卡
    mLeftAdapter = new LeftAdapter(mList,this);
    mLeftRecy.setAdapter(mLeftAdapter);
    //左邊管理類
    manager = new LinearLayoutManager(this);
    mLeftRecy.setLayoutManager(manager);

    //右邊的介面卡
    mRightAdapter = new RightAdapter(mRightList,this);
    mRightRecy.setAdapter(mRightAdapter);
    //右邊管理類
    manager1 = new LinearLayoutManager(this);
    mRightRecy.setLayoutManager(manager1);

    mLeftAdapter.setOnClickListener(new LeftAdapter.ItemClick() {
        @Override
        public void setOnclick(View view, int position) {
            Toast.makeText(MainActivity.this,mList.get(position).getName(), Toast.LENGTH_SHORT).show();
            //點選切換 先清除集合
            mRightList.clear();
            mRightList.addAll(mList.get(position).getSpus());
            mRightAdapter.notifyDataSetChanged();
        }
    });
}

@Override
protected void setOnClick() {

}

@Override
protected void progressLogic() {
    presenter = new PresenterImpl(this);
    presenter.startRequest(mUrl,null);
}

@Override
public void success(Object data) {
    MyData myData= (MyData) data;
    mList.addAll(myData.getData());
    mLeftAdapter.notifyDataSetChanged();
    
	//進去就展示
    mRightList.addAll(mList.get(0).getSpus());
    mRightAdapter.notifyDataSetChanged();

}

@Override
public void error(Object error) {

}

}

2.主佈局
<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/Left_Recy"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/Right_Recy"
        android:layout_width="0dp"
        android:layout_weight="6"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</LinearLayout>

/RelativeLayout>

3.leftAdapter
public class LeftAdapter extends RecyclerView.Adapter<LeftAdapter.ViewHolder> implements View.OnClickListener {

private ArrayList<MyData.DataBean> mList;
private Context mContext;

public LeftAdapter(ArrayList<MyData.DataBean> mList, Context mContext) {
    this.mList = mList;
    this.mContext = mContext;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = View.inflate(mContext, R.layout.left_item, null);
    ViewHolder holder = new ViewHolder(view);
    view.setOnClickListener(this);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
    viewHolder.tv.setText(mList.get(i).getName());
    viewHolder.itemView.setTag(i);
}

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



public class ViewHolder extends RecyclerView.ViewHolder {
    private TextView tv;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        tv = itemView.findViewById(R.id.Left_Text);
    }
}

//介面回撥
public interface ItemClick {
    void setOnclick(View view, int position);
}

private ItemClick mItemClick;

public void setOnClickListener(ItemClick itemClick) {
    this.mItemClick = itemClick;
}

@Override
public void onClick(View v) {
    if(mItemClick!=null){
        mItemClick.setOnclick(v,(int)v.getTag());
    }
}

}

4.rightAdapter
public class RightAdapter extends RecyclerView.Adapter<RightAdapter.ViewHolder> {

private ArrayList<MyData.DataBean.SpusBean> mList;
private Context mContext;

public RightAdapter(ArrayList<MyData.DataBean.SpusBean> mList, Context mContext) {
    this.mList = mList;
    this.mContext = mContext;
}

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

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
    viewHolder.name.setText(mList.get(i).getName()+"");
    viewHolder.price.setText(mList.get(i).getSkus().get(0).getPrice());
    Glide.with(mContext).load(mList.get(i).getPic_url()).into(viewHolder.img);
}

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

public class ViewHolder extends RecyclerView.ViewHolder {
    private ImageView img;
    private TextView name;
    private TextView price;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        img=itemView.findViewById(R.id.Right_Image);
        name=itemView.findViewById(R.id.Right_Title);
        price=itemView.findViewById(R.id.Right_Price);
    }
}

}

左邊佈局

<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:text="哈哈哈"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

/LinearLayout>

右邊 佈局
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android

android:layout_width="match_parent"
android:layout_height="120dp"
android:gravity="center_vertical"
android:orientation="horizontal">

<ImageView

    android:id="@+id/Right_Image"
    android:layout_width="80dp"
    android:layout_height="80dp" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical">

    <TextView
        android:id="@+id/Right_Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="哈哈哈" />

    <TextView
        android:id="@+id/Right_Price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="哈哈哈" />
</LinearLayout>

<com.bawei.wangyaxiao.yuekaomoni2.weight.JiaJianView

    android:id="@+id/JiaJian_View"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></com.bawei.wangyaxiao.yuekaomoni2.weight.JiaJianView>