列表跳转详情

activity_main

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bwie.test.moniapp.MainActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

activity_details

<?xml version="1.0" encoding="utf-8"?>
<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="com.bwie.test.moniapp.DetailsActivity">
    <ImageView
        android:id="@+id/image_tu"
        android:layout_width="match_parent"
        android:layout_height="200dp" />
    <TextView
        android:id="@+id/xiangqing_title"
        android:layout_below="@id/image_tu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/xiangqing_price"
        android:layout_below="@id/xiangqing_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ff0000"/>
    <TextView
        android:id="@+id/xiangqing_dizhi"
        android:layout_marginTop="20dp"
        android:layout_below="@id/xiangqing_price"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="送至"/>

</RelativeLayout>

item_recycler

<?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="100dp">
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="100dp"
        android:layout_height="100dp" />
    <TextView
        android:id="@+id/text_title"
        android:layout_toRightOf="@id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="111"/>
    <TextView
        android:id="@+id/text_price"
        android:layout_toRightOf="@id/image_view"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ff0000"
        android:text="111"/>

</RelativeLayout>

activity_gaode

MainActivity

package com.bwie.test.moniapp;

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

import com.bwie.test.adapter.MyAdapter;
import com.bwie.test.bean.LieBiaoBean;
import com.bwie.test.bean.XiangQingBean;
import com.bwie.test.presenter.Presenter;
import com.bwie.test.util.ApiUrl;
import com.bwie.test.view.MyView;
import com.bwie.test.view.OnClicklist;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements MyView{
    private RecyclerView recycler_view;
    private Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //隐藏原有标题
        //getSupportActionBar().hide();
        //查找控件
        recycler_view = (RecyclerView) findViewById(R.id.recycler_view);
        presenter = new Presenter(this);
        presenter.attachView(this);

        Map<String, String> map = new HashMap<>();
        map.put("keywords","笔记本");
        map.put("page","1");
        presenter.getUrl(ApiUrl.liebiao,map);
    }

    @Override
    public void getLieBiaoBean(final LieBiaoBean lieBiaoBean) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                final List<LieBiaoBean.DataBean> list = lieBiaoBean.getData();
                recycler_view.setLayoutManager(new LinearLayoutManager(MainActivity
                        .this,LinearLayoutManager.VERTICAL,false));
                MyAdapter adapter = new MyAdapter(MainActivity.this, list);
                recycler_view.setAdapter(adapter);
                adapter.setOnClicklist(new OnClicklist() {
                    @Override
                    public void setOnClick(int position) {
                        Intent intent = new Intent(MainActivity
                                .this, DetailsActivity.class);
                        int pid = list.get(position).getPid();
                        intent.putExtra("pid",pid+"");
                        startActivity(intent);
                    }
                });
            }
        });
    }

    @Override
    public void getXiangQingBean(XiangQingBean xiangQingBean) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter!=null){
            presenter.detachView();
        }
    }
}

DetailsActivity

package com.bwie.test.moniapp;

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

import com.bumptech.glide.Glide;
import com.bwie.test.bean.LieBiaoBean;
import com.bwie.test.bean.XiangQingBean;
import com.bwie.test.presenter.Presenter;
import com.bwie.test.util.ApiUrl;
import com.bwie.test.view.MyView;

import java.util.HashMap;

/**
 * 详情页
 */
public class DetailsActivity extends AppCompatActivity implements MyView{
    private Presenter presenter;
    private ImageView image_tu;
    private TextView xiangqing_title;
    private TextView xiangqing_price;
    private TextView xiangqing_dizhi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);
        //隐藏原有标题
        getSupportActionBar().hide();
        //查找控件
        image_tu = (ImageView) findViewById(R.id.image_tu);
        xiangqing_title = (TextView) findViewById(R.id.xiangqing_title);
        xiangqing_price = (TextView) findViewById(R.id.xiangqing_price);
        xiangqing_dizhi = (TextView) findViewById(R.id.xiangqing_dizhi);

        String pid = getIntent().getStringExtra("pid");

        presenter = new Presenter(this);
        presenter.attachView(this);
        HashMap<String, String> map = new HashMap<>();
        map.put("pid",pid);
        presenter.getxiangUrl(ApiUrl.xiangqing,map);
    }

    @Override
    public void getLieBiaoBean(LieBiaoBean lieBiaoBean) {

    }

    @Override
    public void getXiangQingBean(final XiangQingBean xiangQingBean) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                XiangQingBean.DataBean data = xiangQingBean.getData();
                String[] split = data.getImages().split("\\|");
                Glide.with(DetailsActivity.this).load(split[0])
                        .into(image_tu);
                xiangqing_title.setText(data.getTitle());
                xiangqing_price.setText("¥"+data.getPrice());
                xiangqing_dizhi.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(DetailsActivity
                                .this, GaoDeActivity.class);
                        startActivity(intent);
                    }
                });
            }
        });
    }
}

GaoDeActivity

model文件夹Model

package com.bwie.test.model;

import com.bwie.test.bean.LieBiaoBean;
import com.bwie.test.bean.XiangQingBean;
import com.bwie.test.presenter.PresenterPort;
import com.bwie.test.util.OkHttpUtil;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Model {
    private PresenterPort presenterPort;

    public Model(PresenterPort presenterPort) {

        this.presenterPort = presenterPort;
    }

    public void getUrl(String liebiao, Map<String, String> map) {
        OkHttpUtil.doPost(liebiao, map, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()){
                    //Log.d("+++++++",response.body().string()+"");
                    LieBiaoBean lieBiaoBean = new Gson()
                            .fromJson(response.body().string(), LieBiaoBean.class);
                    presenterPort.getLieBiaoBean(lieBiaoBean);
                }
            }
        });
    }

    public void getxiangUrl(String xiangqing, HashMap<String, String> map) {
        OkHttpUtil.doPost(xiangqing, map, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()){
                    //Log.d("+++++++",response.body().string()+"");
                    XiangQingBean xiangQingBean = new Gson().fromJson(response
                            .body().string(), XiangQingBean.class);
                    presenterPort.getXiangQingBean(xiangQingBean);
                }
            }
        });
    }
}

presenter文件夹BasePresenter

public abstract class BasePresenter<T> {
    protected Reference<T> mViewRef;//View接口类型弱引用

    public void attachView(T view) {
        mViewRef = new WeakReference<T>(view); //建立关联
    }

    protected T getView() {
        return mViewRef.get();//获取View
    }

    public boolean isViewAttached() {//判断是否与View建立了关联
        return mViewRef != null && mViewRef.get() != null;
    }

    public void detachView() {//解除关联
        if (mViewRef != null) {
            mViewRef.clear();
            mViewRef = null;
        }
    }
}

Presenter

public class Presenter extends BasePresenter implements PresenterPort{
    private MyView myView;
    private final Model model;

    public Presenter(MyView myView) {
        model = new Model(this);
        this.myView = myView;
    }

    @Override
    public void getLieBiaoBean(LieBiaoBean lieBiaoBean) {
        myView.getLieBiaoBean(lieBiaoBean);
    }

    @Override
    public void getXiangQingBean(XiangQingBean xiangQingBean) {
        myView.getXiangQingBean(xiangQingBean);
    }

    public void getUrl(String liebiao, Map<String, String> map) {
        model.getUrl(liebiao,map);
    }

    public void getxiangUrl(String xiangqing, HashMap<String, String> map) {
        model.getxiangUrl(xiangqing,map);
    }
}

PresenterPort

public interface PresenterPort {
    void getLieBiaoBean(LieBiaoBean lieBiaoBean);
    void getXiangQingBean(XiangQingBean xiangQingBean);
}

view文件夹MyView

public interface MyView {
    void getLieBiaoBean(LieBiaoBean lieBiaoBean);
    void getXiangQingBean(XiangQingBean xiangQingBean);
}

OnClicklist

public interface OnClicklist {
    void setOnClick(int position);
}

adapter文件夹MyAdapter

public class MyAdapter extends RecyclerView.Adapter<MyHolder> {
    private final Context context;
    private final List<LieBiaoBean.DataBean> list;
    private OnClicklist onClicklist;

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

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recycler,
 parent, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, final int position) {
        LieBiaoBean.DataBean dataBean = list.get(position);
        String[] split = dataBean.getImages().split("\\|");
        Glide.with(context).load(split[0]).into(holder.image_view);
        holder.text_title.setText(dataBean.getTitle());
        holder.text_price.setText("¥"+dataBean.getPrice());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClicklist.setOnClick(position);
            }
        });
    }

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

    public void setOnClicklist(OnClicklist onClicklist){
        this.onClicklist = onClicklist;
    }
}

MyHolder

public class MyHolder extends RecyclerView.ViewHolder {
    public ImageView image_view;
    public TextView text_title;
    public TextView text_price;

    public MyHolder(View itemView) {
        super(itemView);
        image_view = itemView.findViewById(R.id.image_view);
        text_title = itemView.findViewById(R.id.text_title);
        text_price = itemView.findViewById(R.id.text_price);
    }
}

util文件夹ApiUrl

public class ApiUrl {
    //?keywords=笔记本&page=1
    public static String liebiao="https://www.zhaoapi.cn/product/searchProducts";
    public static String xiangqing="https://www.zhaoapi.cn/product/getProductDetail";
}

OkHttpUtil

public class OkHttpUtil {
    private static OkHttpClient okHttpClient=null;

    private OkHttpUtil(){

    }
    public static OkHttpClient getinfice(){

        if (okHttpClient==null){
            synchronized (OkHttpUtil.class){
                if (okHttpClient==null){
                    File sdcache = new File(Environment.getExternalStorageDirectory(), "cache");
                    //指定缓存大小
                    int cacheSize = 10 * 1024 * 1024;

                    okHttpClient = new OkHttpClient.Builder()//构建器
                            .connectTimeout(15, TimeUnit.SECONDS)//连接超时
                            .writeTimeout(20, TimeUnit.SECONDS)//写入超时
                            .readTimeout(20, TimeUnit.SECONDS)//读取超时

                            //.addInterceptor(new CommonParamsInterceptor())//添加的是应用拦截器...公共参数
                            //.addNetworkInterceptor(new CacheInterceptor())//添加的网络拦截器

                            .cache(new Cache(sdcache.getAbsoluteFile(), cacheSize))//设置缓存
                            .build();
                }
            }
        }
        return okHttpClient;
    }

    public static void doPost(String url, Map<String,String> map, Callback callback){
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("source","android");
        OkHttpClient okHttpClient = getinfice();
        FormBody.Builder builder = new FormBody.Builder();
        for (String key:map.keySet()){
            builder.add(key,map.get(key));
        }

        for (Map.Entry<String,String> entry:hashMap.entrySet()){
            builder.add(entry.getKey(),entry.getValue());
        }
        Request request = new Request.Builder().url(url).post(builder.build())
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(callback);
    }
}

需要的依赖

<!-- 允许程序打开网络套接字 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- 允许程序设置内置sd卡的写权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 允许程序获取网络状态 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 允许程序访问WiFi网络信息 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- 允许程序读写手机状态和身份 -->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- 允许程序访问CellIDWiFi热点来获取粗略的位置 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

需要的架包

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    ndk {
        //设置支持的SO库架构(开发者可以根据需要,选择一个或多个平台的so        abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "x86","arm64-v8a","x86_64"
    }
}
compile files('libs/universal-image-loader-1.9.3.jar')
compile 'com.android.support.test.espresso:espresso-core:2.2.2'
compile 'com.android.support.test:runner:0.5'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.4.1'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.github.bumptech.glide:glide:3.7.0'
//3D地图sojar
compile 'com.amap.api:3dmap:latest.integration'
//定位功能
compile 'com.amap.api:location:latest.integration'
//搜索功能
compile 'com.amap.api:search:latest.integration'

bean文件夹LieBiaoBean

public class LieBiaoBean {
    private String msg;
    private String code;
    private String page;
    private List<DataBean> data;

    public static class DataBean {
        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;

        
    }
}

XiangQingBean

public class XiangQingBean {
    
    private String code;
    private DataBean data;
    private String msg;
    private SellerBean seller;
   
    public static class DataBean {
        
        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 static class SellerBean {
       
        private String description;
        private String icon;
        private String name;
        private int productNums;
        private double score;
        private int sellerid;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值