Android——MVP模式拦截器加载网络数据,点击跳转购物车

本文介绍了在Android应用中使用MVP模式,通过自定义接口回调和HttpUtils进行网络数据加载。同时,详细讲解了如何设置LoggingInterceptor以便调试,并在点击事件中实现从当前界面跳转到购物车页面的功能。涉及到的关键组件包括RecyclerView用于展示数据,以及activity_car.xml布局文件的设计。
摘要由CSDN通过智能技术生成


android:name=".MyApplication"

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


adapter——CarAdapter

package com.example.theyuekao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.example.theyuekao.CustomView;
import com.example.theyuekao.R;
import com.example.theyuekao.bean.CarBean;

import java.util.List;

public class CarAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<CarBean> list;

    public CarAdapter(Context context, List<CarBean> list) {
        this.context = context;
        this.list = list;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.car_item,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof ViewHolder){
            ViewHolder viewHolder = (ViewHolder)holder;
            if(list.get(position).isB()){
                viewHolder.item_ck.setChecked(true);
            }else{
                viewHolder.item_ck.setChecked(false);
            }
            viewHolder.item_name.setText(list.get(position).getName());
            viewHolder.item_price.setText("单价:"+list.get(position).getPrice()+" ¥");
            viewHolder.item_des.setText("共计"+list.get(position).getCount()+"件,总价"+list.get(position).getCount()*100+"元");
            viewHolder.item_ck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isChecked = list.get(position).isB();
                    list.get(position).setB(!isChecked);
                    notifyDataSetChanged();
                    if(listener != null){
                        listener.check(!isChecked,position);
                    }
                }
            });
            //监听Edtext
            viewHolder.item_custom.setListener(new CustomView.ChangeListener() {
                @Override
                public void onChange(long count) {
                    list.get(position).setCount(count);
                    notifyDataSetChanged();
                    if(listener != null){
                        listener.check(list.get(position).isB(),position);
                    }
                }
            });

            viewHolder.del.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (list.get(position).isB()){
                        Toast.makeText(context, "不能删除", Toast.LENGTH_SHORT).show();
                    }else {
                        list.remove(position);
                        notifyDataSetChanged();
                        if(listener != null){
                            listener.check(true,position);
                        }
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    //为check写一个接口
    public CheckListener listener;
    public void setCheckListener(CheckListener listener){
        this.listener = listener;
    }

    public interface CheckListener{
        public void check(boolean check,int position);
    }
    
    class ViewHolder extends RecyclerView.ViewHolder{
        private final CustomView item_custom;
        private final TextView item_name;
        private final TextView item_price;
        private final TextView item_des;
        private final CheckBox item_ck;
        private final Button del;

        public ViewHolder(View itemView) {
            super(itemView);
            item_custom = (CustomView) itemView.findViewById(R.id.item_custom);
            item_name = (TextView) itemView.findViewById(R.id.item_name);
            item_ck = (CheckBox) itemView.findViewById(R.id.item_ck);
            item_price = itemView.findViewById(R.id.item_price);
            item_des = itemView.findViewById(R.id.item_des);
            del = itemView.findViewById(R.id.delete);
        }
    }
}

adapter——RVAdapter

package com.example.theyuekao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.theyuekao.MainActivity;
import com.example.theyuekao.MyApplication;
import com.example.theyuekao.R;
import com.example.theyuekao.bean.JavaBean;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;


public class RVAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener{

    private Context context;
    private List<JavaBean.SongListBean> list;

    public RVAdapter(Context context, List<JavaBean.SongListBean> list) {
        this.context = context;
        this.list = list;
    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.rv_item,null);
        MyHolder myHolder = new MyHolder(view);
        view.setOnClickListener(this);
        return myHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyHolder){
            MyHolder myHolder = (MyHolder) holder;
            ImageLoader.getInstance().displayImage(list.get(position).getPic_small(),myHolder.img, MyApplication.getOptons());
            myHolder.name.setText(list.get(position).getTitle());
            myHolder.author.setText(list.get(position).getAuthor()+"   "+list.get(position).getAlbum_title());
            myHolder.author.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onItemClick();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    @Override
    public void onClick(View view) {
        if (listener!=null){
            listener.onItemClick();
        }
    }

    class MyHolder extends RecyclerView.ViewHolder{

        private ImageView img;
        private TextView name;
        private TextView author;

        public MyHolder(View itemView) {
            super(itemView);
            //找控件
            img = itemView.findViewById(R.id.img);
            name = itemView.findViewById(R.id.name);
            author = itemView.findViewById(R.id.author);
        }
    }

    public interface OnClickListener{
        void onItemClick();
    }

    public OnClickListener listener;

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

}

bean——CarBean

package com.example.theyuekao.bean;



public class CarBean {
    private int pic;
    private String name;
    private int price;
    private boolean b=true;
    private long count;

    public CarBean(int pic, String name, int price, boolean isSelect, long count) {
        this.pic = pic;
        this.name = name;
        this.price = price;
        this.b = isSelect;
        this.count = count;
    }

    public int getPic() {
        return pic;
    }

    public void setPic(int pic) {
        this.pic = pic;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public boolean isB() {
        return b;
    }

    public void setB(boolean b) {
        this.b = b;
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }
}

bean——JavaBean

http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=20&offset=0

model——MyModel

package com.example.theyuekao.model;


import com.example.theyuekao.HttpUtls;

import java.io.IOException;

import okhttp3.Callback;
import okhttp3.Response;


public class MyModel {
    //网络请求
    public void login(String path,final MyModelListener listener) {

        HttpUtls.doGet(path, new Callback() {
            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                System.out.println("call = " + call);
            }

            @Override
            public void onResponse(okhttp3.Call call, Response response) throws IOException {
                listener.onSuccess(response.body().string());
            }
        });
    }
}

model——MyModelListener

package com.example.theyuekao.model;


public interface MyModelListener {
    void onSuccess(String s);
    void onFailed();
}

presenter——MyPresenter

package com.example.theyuekao.presenter;


import com.example.theyuekao.model.MyModel;
import com.example.theyuekao.model.MyModelListener;
import com.example.theyuekao.view.MyViewListener;



public class MyPresenter {

    private MyViewListener listener ;
    private MyModel model;
    //构造器
    public MyPresenter(MyViewListener loginActivityViewListener){

        this.listener = loginActivityViewListener;
        this.model = new MyModel();

    }

    public void login(final String path){

        // 空判断 合法性
        model.login(path, new MyModelListener() {
            @Override
            public void onSuccess(String s) {
                listener.onSuccess(s);
            }

            @Override
            public void onFailed() {
                listener.onFailed();
            }
        });


    }
}

view——MyViewListener

package com.example.theyuekao.view;



public interface MyViewListener {
    void onSuccess(String s);
    void onFailed();
}

CarActivity

package com.example.theyuekao;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.example.theyuekao.adapter.CarAdapter;
import com.example.theyuekao.bean.CarBean;

import java.util.ArrayList;
import java.util.List;

public class CarActivity extends AppCompatActivity {
    private CheckBox checkBox;
    private TextView all_price;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car);
        checkBox = (CheckBox) findViewById(R.id.ck_all);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rv_car);
        all_price = (TextView) findViewById(R.id.all_price);


        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        //添加死数据
        final List<CarBean> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            list.add(new CarBean(R.mipmap.ic_launcher_round,"商品" + i, 100, true, 1));
        }
        final CarAdapter adapter = new CarAdapter(this, list);
        recyclerView.setAdapter(adapter);


        adapter.setCheckListener(new CarAdapter.CheckListener() {
            @Override
            public void check(boolean check, int position) {
                //监听全选
                boolean all_check = true;
                //价格
                float price = 0;
                int count = 0;
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).isB()) {
                        price += list.get(i).getPrice() * list.get(i).getCount();
                        count += list.get(i).getCount();
                    }
                }
                all_price.setText("共计"+count+"件,总共"+price + "元");
                for (int i = 0; i < list.size(); i++) {
                    if (!list.get(i).isB()) {
                        all_check = false;
                        break;
                    }
                }

                if (all_check) {
                    checkBox.setChecked(true);
                } else {
                    checkBox.setChecked(false);
                }
            }
        });


        //全选和反选
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean check = checkBox.isChecked();
                float price = 0;
                int count = 0;
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).setB(check);
                    if (list.get(i).isB()) {
                        price += list.get(i).getPrice() * list.get(i).getCount();
                        count +=list.get(i).getCount();
                    }
                }
                adapter.notifyDataSetChanged();
                all_price.setText("共计"+count+"件,总共"+price + "元");
            }
        });
    }
}
//自定义view控件,实现加减器功能
CustomView

package com.example.theyuekao;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;


public class CustomView extends LinearLayout{
    public CustomView(Context context) {
        super(context);
    }

    public CustomView(final Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.custom_layout,null);
        addView(view);
        Button jian = (Button) view.findViewById(R.id.jian);
        Button jia = (Button) view.findViewById(R.id.jia);
        final EditText num = (EditText) view.findViewById(R.id.num);
        //减号
        jian.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String result = num.getText().toString().trim();
                int integerResult = Integer.valueOf(result);
                    //给做个限制
                    if(integerResult > 1){
                    integerResult = integerResult - 1;
                    num.setText(integerResult + "");
                }else{
                    Toast.makeText(context, "最小数量为1", Toast.LENGTH_SHORT).show();
                }
                if(listener != null){
                    listener.onChange(integerResult);
                }
            }
        });
        //加号
        jia.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String result = num.getText().toString().trim();
                int integerResult = Integer.valueOf(result);
                if(integerResult < 10000){
                    integerResult = integerResult + 1;
                    num.setText(integerResult +"");
                }else{
                    Toast.makeText(context, "已经超出最大值", Toast.LENGTH_SHORT).show();
                }
                if(listener != null ){
                    listener.onChange(integerResult);
                }
            }
        });

        num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.i("beforeTextChanged",s+"-----"+start+"-------"+count+"-----"+after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i("onTextChanged",s+"-----"+start+"-------"+before+"-----"+count);

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(listener != null ){
                    listener.onChange(Long.valueOf(s.toString()));
                }
            }
        });
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ChangeListener listener;
    public void setListener(ChangeListener listener){
        this.listener = listener;
    }
    public interface ChangeListener{
        public void onChange(long count);
    }
}

//网络框架

HttpUtls

package com.example.theyuekao;

import android.os.Environment;
import android.os.Handler;
import android.util.Log;

import java.io.File;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.logging.HttpLoggingInterceptor;

public class HttpUtls {
    private static OkHttpClient client = null;

    private HttpUtls() {
    }
    public static OkHttpClient getInstance() {
        if (client == null) {
            synchronized (HttpUtls.class) {
                if (client == null) {
                    //缓存目录
                    File sdcache = new File(Environment.getExternalStorageDirectory(), "okCache");
                    int cacheSize = 10 * 1024 * 1024;
                    //OkHttp3拦截器
                    HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                        @Override
                        public void log(String message) {
                            Log.i("xxx", message.toString());
                        }
                    });
                    //Okhttp3的拦截器日志分类 4种
                    httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
                    //添加拦截器
                    LoggingInterceptor interceptor = new LoggingInterceptor();

                    client = new OkHttpClient.Builder().connectTimeout(15, TimeUnit.SECONDS)
                            //添加OkHttp3的拦截器
                            .addInterceptor(httpLoggingInterceptor)
                            .addInterceptor(interceptor)
                            .writeTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS)
                            .cache(new Cache(sdcache.getAbsoluteFile(), cacheSize))
                            .build();
                }

            }
        }
        return client;
    }

    private static Handler mHandler = null;

    public synchronized static Handler getHandler() {
        if (mHandler == null) {
            mHandler = new Handler();
        }

        return mHandler;
    }

    /**
     * Get请求
     */
    public static void doGet(String url, Callback callback) {
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = getInstance().newCall(request);
        call.enqueue(callback);
    }

    /**
     * Post请求发送键值对数据
     */
    public static void doPost(String url, Map<String, String> mapParams, Callback callback) {
        FormBody.Builder builder = new FormBody.Builder();
        for (String key : mapParams.keySet()) {
            builder.add(key, mapParams.get(key));
        }
        Request request = new Request.Builder()
                .url(url)
                .post(builder.build())
                .build();
        Call call = getInstance().newCall(request);
        call.enqueue(callback);
    }

    /**
     * Post请求发送JSON数据
     */
    public static void doPost(String url, String jsonParams, Callback callback) {
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8")
                , jsonParams);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Call call = getInstance().newCall(request);
        call.enqueue(callback);
    }
}

//网络拦截器

LoggingInterceptor

package com.example.theyuekao;

import android.os.Build;
import java.io.IOException;
import java.util.logging.Logger;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 网络拦截器
 */

public class LoggingInterceptor implements Interceptor {

    private static final String UA = "User-Agent";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request()
                .newBuilder()
                .addHeader(UA, makeUA())
                .build();
        return chain.proceed(request);
    }
    private String makeUA() {
        String s = Build.BRAND + "/" + Build.MODEL + "/" + Build.VERSION.RELEASE;
        return Build.BRAND + "/" + Build.MODEL + "/" + Build.VERSION.RELEASE;
    }

}


//主视图

main

package com.example.theyuekao;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.theyuekao.adapter.RVAdapter;
import com.example.theyuekao.bean.JavaBean;
import com.example.theyuekao.presenter.MyPresenter;
import com.example.theyuekao.view.MyViewListener;
import com.google.gson.Gson;

import java.util.List;

public class MainActivity extends AppCompatActivity implements MyViewListener {

    private RecyclerView recyclerView;
    private MyPresenter presenter;
    private List<JavaBean.SongListBean> listBeen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找控件
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);

        //实例化presenter
        presenter = new MyPresenter(this);
        presenter.login("http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=20&offset=0");

    }

    @Override
    public void onSuccess(final String s) {
        if (s!=null){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("s = " + s.toString());
                    //解析gson串
                    Gson gson = new Gson();
                    JavaBean bean = gson.fromJson(s.toString(),JavaBean.class);
                    listBeen = bean.getSong_list();


                    //设置模式
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
                    recyclerView.setLayoutManager(linearLayoutManager);
                    //设置适配器
                    final RVAdapter adapter = new RVAdapter(MainActivity.this,listBeen);
                    recyclerView.setAdapter(adapter);

                    adapter.setOnItemClickListener(new RVAdapter.OnClickListener() {
                        @Override
                        public void onItemClick() {
                            Intent intent = new Intent(MainActivity.this,CarActivity.class);
                            startActivity(intent);
                        }
                    });
                }
            });
        }
    }

    @Override
    public void onFailed() {

    }
}

//图片框架

package com.example.theyuekao;

import android.app.Application;
import android.os.Environment;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.io.File;

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        String path = Environment.getExternalStorageDirectory()+"1507DAccess";
        File cacheDir = new File(path);

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
                .threadPriority(100)
                .threadPoolSize(3)
                .memoryCacheExtraOptions(200,200)
                .memoryCacheSize(2 * 1024 * 1024)
                .diskCache(new UnlimitedDiskCache(cacheDir))
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .diskCacheSize(50 * 1024 * 1024)
                .build();
        ImageLoader.getInstance().init(config);

    }



    public static DisplayImageOptions getOptons(){

        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                //空图片
                .showImageForEmptyUri(R.mipmap.ic_launcher_round)
                //加载失败时图片
                .showImageOnFail(R.mipmap.ic_launcher)
                //加载过程中图片
                .showImageOnLoading(R.mipmap.ic_launcher_round)
                .build();

        return options;


    }

}


activity_car.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    >
    <TextView
        android:text="购物车"
        android:textSize="28sp"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_car"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="30dp">
        <CheckBox
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="全选"
            android:checked="true"
            android:layout_gravity="center"
            android:id="@+id/ck_all"/>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="总计5件,总价500元"
            android:layout_marginLeft="10dp"
            android:textSize="10sp"
            android:gravity="center_vertical"
            android:id="@+id/all_price"
            />
    </LinearLayout>

</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.theyuekao.MainActivity">

    <ImageView
        android:id="@+id/aa"
        android:background="#f00"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:src="@mipmap/a"/>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_below="@id/aa"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

car_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="130dp">
    <CheckBox
        android:id="@+id/item_ck"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/car_img"
        android:layout_toRightOf="@id/item_ck"
        android:src="@mipmap/ic_launcher"
        android:layout_margin="5dp"
        android:layout_width="60dp"
        android:layout_centerVertical="true"
        android:layout_height="60dp" />
    <TextView
        android:id="@+id/item_name"
        android:layout_toRightOf="@id/car_img"
        android:layout_width="wrap_content"
        android:text="111"
        android:layout_marginTop="30dp"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/item_price"
        android:layout_toRightOf="@id/car_img"
        android:layout_width="wrap_content"
        android:text="111"
        android:layout_below="@id/item_name"
        android:layout_height="wrap_content" />
    <com.example.theyuekao.CustomView
        android:id="@+id/item_custom"
        android:layout_below="@id/item_price"
        android:layout_toRightOf="@id/car_img"
        android:layout_width="70dp"
        android:layout_height="70dp"/>
    <Button
        android:id="@+id/delete"
        android:layout_alignParentRight="true"
        android:layout_margin="5dp"
        android:text="删除"
        android:layout_centerVertical="true"
        android:layout_width="40dp"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/item_des"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@id/car_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

custom_layout.xml

<?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">
    <Button
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:text="-"
        android:background="#fff"
        android:id="@+id/jian"
        />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:text="1"
        android:id="@+id/num"/>
    <Button
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="#fff"
        android:text="+"
        android:id="@+id/jia"/>

</LinearLayout>

rv_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="50dp">
    <ImageView
        android:id="@+id/img"
        android:layout_margin="5dp"
        android:src="@mipmap/ic_launcher_round"
        android:layout_width="40dp"
        android:layout_height="40dp" />
    <TextView
        android:id="@+id/name"
        android:layout_toRightOf="@id/img"
        android:layout_marginTop="5dp"
        android:text="111"
        android:textSize="18sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/author"
        android:layout_toRightOf="@id/img"
        android:layout_below="@id/name"
        android:text="111"
        android:textSize="14sp"
        android:textColor="#ccc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</RelativeLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值