android的MVP展示+详情页面+高德定位+显示蓝点

1.首先:添加依赖

  // xRecelrview依赖
  compile 'com.jcodecraeer:xrecyclerview:1.3.2'
  //    Glide依赖
  compile 'com.github.bumptech.glide:glide:3.7.0'
  //    okhttp依赖
  compile 'com.squareup.okhttp3:okhttp:3.3.0'

2.添加权限

(1)、平时用到的权限:网络权限、可读、可写

 <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" />

(2)、高德地图的权限

<!-- 用于进行网络定位 -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <!-- 用于访问GPS定位 -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <!-- 用于获取运营商信息,用于支持提供运营商信息相关的接口 -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <!-- 用于获取wifi的获取权限,wifi信息会用来进行网络定位 -->
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <!-- 用于访问网络,网络定位需要上网 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- 用于读取手机当前的状态 -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <!-- 用于写入缓存数据到扩展存储卡 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- 用于申请调用A-GPS模块 -->
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <!-- 用于申请获取蓝牙信息进行室内定位 -->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

3、添加高德解压包的离线依赖

 //gson解析
 compile files('libs/gson-2.2.4.jar')
 //高德定位
 compile files('libs/AMap_Location_V3.8.0_20180201.jar')
 //高德显示地图
 compile files('libs/Amap_2DMap_V5.2.0_20170627.jar')

4.还要在application里面添加 自己创建应用的 appkey 和 service

代码如下:

注意:value的值就是 自己的appkey

<service android:name="com.amap.api.location.APSService" />

<meta-data
android:name="com.amap.api.v2.apikey"
android:value="b8709b716b236306437c30ba711c7d56"></meta-data>

具体操作如下:

这里写图片描述

接下来正式开始写代码:

一、需要用到的所有布局

1.MainActivity的布局:(展示商品列表)

<?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"
    tools:context="com.bwei.lenovo.day0402_yuekao1.MainActivity">

   <com.jcodecraeer.xrecyclerview.XRecyclerView
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/xrecylerview"></com.jcodecraeer.xrecyclerview.XRecyclerView>

</LinearLayout>

2.展示商品列表的item布局

<?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="horizontal">

    <ImageView
        android:id="@+id/item_img"
        android:layout_width="100dp"
        android:layout_height="100dp" />

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

    <TextView
        android:id="@+id/item_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="标题"
        android:textSize="25sp" />

    <TextView
        android:layout_marginTop="5dp"
        android:id="@+id/item_price"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="价格" />

    </LinearLayout>
</LinearLayout>

3.详情页面的布局 (detaile)

<?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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.bwei.lenovo.day0402_yuekao1.activity.DetailActivity">

    <ImageView
        android:id="@+id/detail_img"
        android:layout_width="match_parent"
        android:layout_height="500dp"></ImageView>

    <TextView
        android:text="标题"
        android:textSize="25sp"
        android:id="@+id/detail_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:text="价格"
        android:textColor="#f00"
        android:id="@+id/detail_price"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:text="地址"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/detail_adress"/>
</LinearLayout>

4.显示地图的布局 2D(高德)

<?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"
    tools:context="com.bwei.lenovo.day0402_yuekao1.activity.MyAddressActivity">

    <com.amap.api.maps2d.MapView

        android:id="@+id/map"

        android:layout_width="match_parent"

        android:layout_height="match_parent"/>

</LinearLayout>

二、http包

1.HttpConfig包下->接口路径

package com.bwei.lenovo.day0402_yuekao1.http;

public class HttpConfig {
    //展示列表
    public static String base_url="https://www.zhaoapi.cn/product/getProducts";
}

2.okHttpUtils工具类 (OKHttpUtils )

package com.bwei.lenovo.day0402_yuekao1.http;

import android.os.Handler;
import android.os.Message;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OKHttpUtils {

    private static OKHttpUtils okHttpUtils = null;
    private MyViewHolder myViewHolder = new MyViewHolder();
    private OkLoadLinstener okLoadLinstener;

    public static OKHttpUtils getInstance() {
        if (okHttpUtils == null) {
            okHttpUtils = new OKHttpUtils();
        }
        return okHttpUtils;
    }

    public void okGet(String url) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Message message = myViewHolder.obtainMessage();
                message.what = 1;
                message.obj = e.getMessage();
                myViewHolder.sendMessage(message);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Message message = myViewHolder.obtainMessage();
                message.what = 0;
                message.obj = response.body().string();
                myViewHolder.sendMessage(message);
            }
        });
    }

    //拦截器、‘
    class MyIntercepter implements Interceptor{

        @Override
        public Response intercept(Chain chain) throws IOException {

            Request request = chain.request();
            String method = request.method();
            //判断请求网络的方法是get的话
            if (method.equals("GET")){
                String url= request.url().toString();
                url=url+"&source=android";
                Request newRequest= request.newBuilder().url(url).build();
                Response response = chain.proceed(newRequest);
                return response;

            }else if (method.equals("POST")){
                RequestBody body = request.body();
                if (body instanceof FormBody){
                    FormBody.Builder builder = new FormBody.Builder();
                    for (int i=0;i<((FormBody) body).size();i++){
                        String name = ((FormBody) body).name(i);
                        String value = ((FormBody) body).value(i);
                        builder.add(name,value);
                    }
                    builder.add("source","android");
                    FormBody formBody = builder.build();
                    Request request1 = request.newBuilder().post(formBody).build();
                    Response response = chain.proceed(request1);
                    return response;
                }

            }
            return null;
        }
    }


   //handler
    class MyViewHolder extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                //成功
                case 0:
                    String json = (String) msg.obj;
                    okLoadLinstener.okLoadSuccess(json);
                    break;
                //失败
                case 1:
                   String error= (String) msg.obj;
                    okLoadLinstener.okLoadError(error);
                    break;
            }
        }
    }

    //定义一个供外部调用的方法
    public void SetOnLoadLinstener(OkLoadLinstener okLoadLinstener) {
        this.okLoadLinstener = okLoadLinstener;
    }
}

3.okHttpUtils工具类用到的网络 接口回调 (OkLoadLinstener )

package com.bwei.lenovo.day0402_yuekao1.http;

public interface OkLoadLinstener {
    //成功
    void okLoadSuccess(String jaon);

    //失败
    void okLoadError(String error);
}

三、view包

1.view包下的 IView接口

package com.bwei.lenovo.day0402_yuekao1.view;

import com.bwei.lenovo.day0402_yuekao1.bean.GoodsBean;

import java.util.List;

public interface IView {
    //展示商品列表
    void showGoodsList(List<GoodsBean.DataBean> list);
}

四、model包

1.model包下的 监听事件 ( GetGoodsListLinstener )

package com.bwei.lenovo.day0402_yuekao1.model;

/**
 *     //监听事件
 */
public interface GetGoodsListLinstener {
    //成功
    void getGoodsListSuccess(String json);
    //失败
    void getGoodsListError(String error);
}

}

2.model包下的 ( IModel )接口

package com.bwei.lenovo.day0402_yuekao1.model;

public interface IModel {

void getGoodsList(String url,GetGoodsListLinstener getGoodsListLinstener);

}

3.model包下的 IModel 的实现类(IModelmpI )

package com.bwei.lenovo.day0402_yuekao1.model;

import com.bwei.lenovo.day0402_yuekao1.http.OKHttpUtils;
import com.bwei.lenovo.day0402_yuekao1.http.OkLoadLinstener;

/**
 * 请求网络
 */

public class IModelmpI implements IModel {
    @Override
    public void getGoodsList(String url, final GetGoodsListLinstener getGoodsListLinstener) {
        OKHttpUtils instance = OKHttpUtils.getInstance();
        //get请求
        instance.okGet(url);
        instance.SetOnLoadLinstener(new OkLoadLinstener() {
            @Override
            public void okLoadSuccess(String jaon) {
                //成功
                getGoodsListLinstener.getGoodsListSuccess(jaon);
            }

            @Override
            public void okLoadError(String error) {
                //失败
                getGoodsListLinstener.getGoodsListError(error);
            }
        });
    }
}

五、presenter层

1.p层的接口 (IPresenter )

package com.bwei.lenovo.day0402_yuekao1.presenter;

import com.bwei.lenovo.day0402_yuekao1.model.IModel;
import com.bwei.lenovo.day0402_yuekao1.view.IView;


public interface IPresenter {

    void getGoodsView(IModel iModel, IView iView);
}

2.P层接口的实现类 (IPresenterImpI )

package com.bwei.lenovo.day0402_yuekao1.presenter;

import android.util.Log;

import com.bwei.lenovo.day0402_yuekao1.bean.GoodsBean;
import com.bwei.lenovo.day0402_yuekao1.http.HttpConfig;
import com.bwei.lenovo.day0402_yuekao1.model.GetGoodsListLinstener;
import com.bwei.lenovo.day0402_yuekao1.model.IModel;
import com.bwei.lenovo.day0402_yuekao1.view.IView;
import com.google.gson.Gson;

import java.util.List;

/**
 * 逻辑操作
 */

public class IPresenterImpI implements IPresenter {
    @Override
    public void getGoodsView(IModel iModel, final IView iView) {
        //拼接路径
        String url = HttpConfig.base_url + "?pscid=1";
        iModel.getGoodsList(url, new GetGoodsListLinstener() {
            @Override
            public void getGoodsListSuccess(String json) {
                Log.d("IPresenterImpI", "-P层获取到集合---------"+json);
                //解析数据
                Gson gson=new Gson();
                GoodsBean goodsBean = gson.fromJson(json, GoodsBean.class);
                List<GoodsBean.DataBean> list = goodsBean.getData();
                iView.showGoodsList(list);
            }

            @Override
            public void getGoodsListError(String error) {
                Log.d("IPresenterImpI", "p层获取的异常-----------"+error);
            }
        });
    }
}

六、需要用的Activity

1.展示数据的 MainActivity

package com.bwei.lenovo.day0402_yuekao1;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.bwei.lenovo.day0402_yuekao1.activity.DetailActivity;
import com.bwei.lenovo.day0402_yuekao1.adpter.MyAdapter;
import com.bwei.lenovo.day0402_yuekao1.bean.GoodsBean;
import com.bwei.lenovo.day0402_yuekao1.model.IModelmpI;
import com.bwei.lenovo.day0402_yuekao1.presenter.IPresenterImpI;
import com.bwei.lenovo.day0402_yuekao1.view.IView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

public class MainActivity extends AppCompatActivity implements IView {

    private XRecyclerView xRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化数据
        initviews();
    }

    private void initviews() {
        xRecyclerView = (XRecyclerView) findViewById(R.id.xrecylerview);
        //设置布局管理器
        xRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        IPresenterImpI iPresenterImpI = new IPresenterImpI();
        iPresenterImpI.getGoodsView(new IModelmpI(), this);
    }

    //---------------------------重写的IView方法-------------------------------------------
    @Override
    public void showGoodsList(final List<GoodsBean.DataBean> list) {
        Log.d("MainActivity", "MainAcvity获取到集合------" + list);
        MyAdapter myAdapter = new MyAdapter(this, list);
        xRecyclerView.setAdapter(myAdapter);

        myAdapter.setOnMyitemClick(new MyAdapter.MyItemClickLinstener() {
            @Override
            public void onMyItemClick(View view, int poistion, int pscid) {
                //跳转详情页面
                GoodsBean.DataBean dataBean = list.get(poistion);
                Intent intent = new Intent(MainActivity.this, DetailActivity.class);
                intent.putExtra("goodsbean", dataBean);
                startActivity(intent);
            }
        });
    }
}

2.详情页面的Activity (DetailActivity )

里面有高德定位 和 详情展示

package com.bwei.lenovo.day0402_yuekao1.activity;

import android.content.Intent;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.bumptech.glide.Glide;
import com.bwei.lenovo.day0402_yuekao1.R;
import com.bwei.lenovo.day0402_yuekao1.bean.GoodsBean;

import java.io.Serializable;

public class DetailActivity extends AppCompatActivity {

    private ImageView detail_img;
    private TextView detail_title;
    private TextView detail_price;
    private TextView detail_adress;
    public AMapLocationClient mLocationClient = null;

    //声明定位回调监听器
     //    public AMapLocationListener mLocationListener = new AMapLocationListener();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        detail_img = (ImageView) findViewById(R.id.detail_img);
        detail_title = (TextView) findViewById(R.id.detail_title);
        detail_price = (TextView) findViewById(R.id.detail_price);
        detail_adress = (TextView) findViewById(R.id.detail_adress);
        detail_adress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(DetailActivity.this, MyAddressActivity.class);
                startActivity(intent);
            }
        });
        //详情页面的数据
        initDatas();
        //高德定位
        initApi();
    }

    private void initApi() {
        //声明AMapLocationClient类对象

         //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(mAMapLocationListener);

        //声明AMapLocationClientOption对象
        AMapLocationClientOption mLocationOption = null;
        //初始化AMapLocationClientOption对象
        mLocationOption = new AMapLocationClientOption();
        AMapLocationClientOption option = new AMapLocationClientOption();
        /**
         * 设置定位场景,目前支持三种场景(签到、出行、运动,默认无场景)
         */
        option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
        if(null != mLocationClient){
            mLocationClient.setLocationOption(option);
            //设置场景模式后最好调用一次stop,再调用start以保证场景模式生效
            mLocationClient.stopLocation();
            mLocationClient.startLocation();
        }
        //设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。
        mLocationOption.setInterval(1000);

         //设置是否返回地址信息(默认返回地址信息)
        mLocationOption.setNeedAddress(true);

       //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
       //启动定位
        mLocationClient.startLocation();
    }

    AMapLocationListener mAMapLocationListener = new AMapLocationListener() {
        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation != null) {
                if (amapLocation.getErrorCode() == 0) {
                //可在其中解析amapLocation获取相应内容。
                    Log.d("DetailActivity", "----定位成功----------");
                    String street = amapLocation.getStreet();
                    String address = amapLocation.getAddress();
                    Log.d("DetailActivity", "-----------"+street);
                    Log.d("DetailActivity", "-----------"+address);
                    //将地址显示出来
                    detail_adress.setText(address);

                }else {
                    //定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
                    Log.e("AmapError","location Error, ErrCode:"
                            + amapLocation.getErrorCode() + ", errInfo:"
                            + amapLocation.getErrorInfo());
                }
            }
        }
    };


    //详情页的数据
    private void initDatas() {
        Intent intent = getIntent();
        GoodsBean.DataBean dataBean = (GoodsBean.DataBean) intent.getSerializableExtra("goodsbean");
        detail_title.setText(dataBean.getTitle());
        detail_price.setText(dataBean.getPrice() + "");
        detail_price.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        String detail_url = dataBean.getImages().split("\\|")[0];
        Glide.with(DetailActivity.this).load(detail_url).into(detail_img);
    }


}

3.显示地图 定位小蓝点 (MyAddressActivity )

package com.bwei.lenovo.day0402_yuekao1.activity;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.MyLocationStyle;
import com.bwei.lenovo.day0402_yuekao1.R;


public class MyAddressActivity extends AppCompatActivity {

    private MapView mMapView;
    AMap aMap = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_address);

        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.map);
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图
        mMapView.onCreate(savedInstanceState);
        if (aMap == null) {
            aMap = mMapView.getMap();
        }
        //定位蓝点的方法
        initApi();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy(),销毁地图
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume (),重新绘制加载地图
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause (),暂停地图的绘制
        mMapView.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
        //初始化地图控制器对象


    }

    private void initApi() {
        MyLocationStyle myLocationStyle;
        myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。
        myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。
        aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
        //aMap.getUiSettings().setMyLocationButtonEnabled(true);设置默认定位按钮是否显示,非必需设置。
        aMap.setMyLocationEnabled(true);// 设置为true表示启动显示定位蓝点,false表示隐藏定位蓝点并不进行定位,默认是false。

        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW);//连续定位、且将视角移动到地图中心点,定位蓝点跟随设备移动。(1秒1次定位)
        //设置显示定位蓝点
        myLocationStyle.showMyLocation(true);///设置是否显示定位小蓝点,用于满足只想使用定位,不想使用定位小蓝点的场景,设置false以后图面上不再有定位蓝点的概念,但是会持续回调位置信息。

        myLocationStyle.strokeColor(Color.BLUE);//设置定位蓝点精度圆圈的边框颜色的方法。
        myLocationStyle.radiusFillColor(Color.BLUE);//
        myLocationStyle.strokeWidth(100);//设置定位蓝点精度圈的边框宽度的方法。

    }
}

七、展示商品列表的Adapter (MyAdapter )

package com.bwei.lenovo.day0402_yuekao1.adpter;

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.bumptech.glide.Glide;
import com.bwei.lenovo.day0402_yuekao1.R;
import com.bwei.lenovo.day0402_yuekao1.bean.GoodsBean;
import com.bwei.lenovo.day0402_yuekao1.model.IModel;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    Context context;
    List<GoodsBean.DataBean> list;
    private MyItemClickLinstener myItemClickLinstener;

    public MyAdapter(Context context, List<GoodsBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = View.inflate(context, R.layout.layout_item, null);
        MyViewHolder myViewHolder = new MyViewHolder(inflate);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.getItem_title().setText(list.get(position).getTitle());
        holder.getItem_price().setText(list.get(position).getPrice() + "");
        String image_url = list.get(position).getImages().split("\\|")[0];
        Glide.with(context).load(image_url).into(holder.getItem_img());

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myItemClickLinstener.onMyItemClick(view,position,list.get(position).getPscid());
            }
        });
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {

        private final ImageView item_img;
        private final TextView item_title;
        private final TextView item_price;

        public MyViewHolder(View itemView) {
            super(itemView);
            item_img = itemView.findViewById(R.id.item_img);
            item_title = itemView.findViewById(R.id.item_title);
            item_price = itemView.findViewById(R.id.item_price);
        }

        public MyViewHolder(View itemView, ImageView item_img, TextView item_title, TextView item_price) {
            super(itemView);
            this.item_img = item_img;
            this.item_title = item_title;
            this.item_price = item_price;
        }

        public ImageView getItem_img() {
            return item_img;
        }

        public TextView getItem_title() {
            return item_title;
        }

        public TextView getItem_price() {
            return item_price;
        }
    }

    //定义自己的item事件
    public interface MyItemClickLinstener {
        void onMyItemClick(View view,int poistion,int pscid);
    }

    //供外部调用的方法
    public void setOnMyitemClick(MyItemClickLinstener myItemClickLinstener) {
        this.myItemClickLinstener = myItemClickLinstener;
    }
}

八、bean类

1.把bean类贴出来的目的,是怕大家忘了实现序列化,因为详情页传的是对象,不要忘记哦

package com.bwei.lenovo.day0402_yuekao1.bean;

import java.io.Serializable;
import java.util.List;

public class GoodsBean implements Serializable{

    private String msg;
    private String code;
    private String page;
    private List<DataBean> data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean implements Serializable{

        private double bargainPrice;
        private String createtime;
        private String detailUrl;
        private String images;
        private int itemtype;
        private int pid;
        private double price;
        private int pscid;
        private int salenum;
        private int sellerid;
        private String subhead;
        private String title;

        public double getBargainPrice() {
            return bargainPrice;
        }

        public void setBargainPrice(double bargainPrice) {
            this.bargainPrice = bargainPrice;
        }

        public String getCreatetime() {
            return createtime;
        }

        public void setCreatetime(String createtime) {
            this.createtime = createtime;
        }

        public String getDetailUrl() {
            return detailUrl;
        }

        public void setDetailUrl(String detailUrl) {
            this.detailUrl = detailUrl;
        }

        public String getImages() {
            return images;
        }

        public void setImages(String images) {
            this.images = images;
        }

        public int getItemtype() {
            return itemtype;
        }

        public void setItemtype(int itemtype) {
            this.itemtype = itemtype;
        }

        public int getPid() {
            return pid;
        }

        public void setPid(int pid) {
            this.pid = pid;
        }

        public double getPrice() {
            return price;
        }

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

        public int getPscid() {
            return pscid;
        }

        public void setPscid(int pscid) {
            this.pscid = pscid;
        }

        public int getSalenum() {
            return salenum;
        }

        public void setSalenum(int salenum) {
            this.salenum = salenum;
        }

        public int getSellerid() {
            return sellerid;
        }

        public void setSellerid(int sellerid) {
            this.sellerid = sellerid;
        }

        public String getSubhead() {
            return subhead;
        }

        public void setSubhead(String subhead) {
            this.subhead = subhead;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值