详情搜索

网络请求

public class RetrofitUtils {
    /**
     * 单例模式  懒汉
     */
    private static RetrofitUtils retrofitUtils;

    public RetrofitUtils(){

    }

    public static RetrofitUtils getInstance(){
        if (retrofitUtils==null){
            synchronized (RetrofitUtils.class){
                if (retrofitUtils==null){
                   retrofitUtils= new RetrofitUtils();
                }
            }
        }
        return retrofitUtils;
    }

    /**
     * 获取OkHttp
     */
    private static OkHttpClient okHttpClient;
    private static synchronized OkHttpClient getOkHttpClient(){
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.i("lj","log:"+message);
            }
        });

        okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(interceptor.setLevel(HttpLoggingInterceptor.Level.BODY))
                .connectTimeout(3000,TimeUnit.SECONDS)
                .readTimeout(3000,TimeUnit.SECONDS)
                .build();
        return RetrofitUtils.okHttpClient;
    }

    //获取retrofit的方法

    private static Retrofit retrofit;

    public static synchronized Retrofit getRetrofit(String url){
         retrofit = new Retrofit.Builder()
                 .baseUrl(url)
                 .addConverterFactory(GsonConverterFactory.create())
                 .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                 .client(getOkHttpClient())
                .build();


        return retrofit;
    }

    //get方法
    public <T> T daGet(String url,Class<T> apiService){
        Retrofit retrofit = getRetrofit(url);
        T t = retrofit.create(apiService);
        return t;
    }

}

接口

public class Api {
 //http://172.17.8.100/small/commodity/v1/findCommodityByKeyword?keyword=%E5%A5%B3%E8%A3%85&page=1&count=20
    public static final String ListUrl="http://172.17.8.100/small/commodity/v1/";
//http://172.17.8.100/small/commodity/v1/findCommodityDetailsById?commodityId=83
    public static final String DataUrl="http://172.17.8.100/small/commodity/v1/";
}

public interface ApiService {
    //http://172.17.8.100/small/commodity/v1/findCommodityByKeyword?keyword=%E5%A5%B3%E8%A3%85&page=1&count=20

    @GET("findCommodityByKeyword")
    Call<ListBean> getLits(@Query("keyword") String keyword, @Query("page") int page, @Query("count") int count);

    //http://172.17.8.100/small/commodity/v1/findCommodityDetailsById?commodityId=83
    @GET("findCommodityDetailsById")
    Call<DataBean> getData(@Query("commodityId") String commodityId);

}

MyApp

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}

P抽基类

/**

  • 内存泄漏

  • @param
    */
    public abstract class BasePresenter {
    private Reference tReference;

    public void attachView(T t){
    tReference = new WeakReference<>(t);
    }

    public void deatchView(){
    if (tReference!=null){
    tReference.clear();
    tReference=null;
    }
    }

    public abstract void onListP(String keyword,int page,int count);

}


展示的M

public class ListModel {

    public void getHttpData(String keyword,int page,int count){
        ApiService apiService = RetrofitUtils.getInstance().daGet(Api.ListUrl, ApiService.class);
        apiService.getLits(keyword, page, count).enqueue(new Callback<ListBean>() {
            @Override
            public void onResponse(Call<ListBean> call, Response<ListBean> response) {

                List<ListBean.ListResult> result = response.body().getResult();
                if (listListener!=null){
                    listListener.onResult(result);
                }
            }

            @Override
            public void onFailure(Call<ListBean> call, Throwable t) {

            }
        });

    }

    /**
     * 定义接口
     */
    public interface onListListener{
        void onResult(List<ListBean.ListResult> data);
    }

    public onListListener listListener;

    public void setListListener(onListListener listListener){
        this.listListener=listListener;
    }

}

P

public class DataPresenter extends BasePresenter<DataView>{
    private DataModel dataModel;
    private DataView dataView;

    public DataPresenter(DataView view){
        dataView=view;
        dataModel = new DataModel();
    }

    @Override
    public void onListP(String keyword, int page, int count) {

    }

    public void onRelated(final String commodityId){
        dataModel.getHttpData(commodityId);

        dataModel.setDetailsListener(new DataModel.onDetailsListener() {
            @Override
            public void onResult(DataBean.DataResult result) {
                dataView.getDataViewData(result);
            }
        });
    }
}

v

public interface DataView {
    void getDataViewData(DataBean.DataResult result);
}

``
然后自动以一个Myview

public class MyView extends LinearLayout {

private Button but_myview;
private EditText name_myview;

public MyView(Context context) {
    super(context);
}

public MyView(final Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    LayoutInflater.from(context).inflate(R.layout.myview_layout,this,true);
    name_myview=findViewById(R.id.name_myview);
    but_myview=findViewById(R.id.but_myview);
    but_myview.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            String name = name_myview.getText().toString();
            if (name.isEmpty()){
                Toast.makeText(context,"请输入你要搜索的内容",Toast.LENGTH_SHORT).show();
            }else {
                getName.ongetName(name);
            }

        }
    });
}

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

/**
 * 接口回调
 */
public interface onGetName{
    void ongetName(String s);
}

onGetName getName;

public void setGetName(onGetName getName){
    this.getName=getName;
}

}
布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/name_myview"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="8"
            android:gravity="center"
            android:hint="请输入你要查询的商品"/>
        <Button
            android:id="@+id/but_myview"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent"
            android:text="搜索"/>
    </LinearLayout>

</LinearLayout>

适配器

public class MyAdapter extends RecyclerView.Adapter {
private Context context;
private List<ListBean.ListResult> list;

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

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(context).inflate(R.layout.item_layout, viewGroup, false);
    ListViewHolder listViewHolder = new ListViewHolder(view);
    return listViewHolder;
}

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
    ListViewHolder listViewHolder= (ListViewHolder) viewHolder;
    listViewHolder.price.setText(list.get(i).commodityName);
    AbstractDraweeController build = Fresco.newDraweeControllerBuilder()
            .setUri(list.get(i).getMasterPic())
            .setTapToRetryEnabled(true)
            .build();
    listViewHolder.img.setController(build);
    /**
     * 点击EvenBus回传值
     */
    listViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String commodityId = list.get(i).getCommodityId();
            EventBus.getDefault().post(commodityId);
        }
    });
}

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

/**
 * 展示列表的viewholder
 */
private class ListViewHolder extends RecyclerView.ViewHolder {
    private final TextView price;
    private final SimpleDraweeView img;

    public ListViewHolder(@NonNull View itemView) {
        super(itemView);
        img=itemView.findViewById(R.id.img_item);
        price=itemView.findViewById(R.id.price_item);
    }
}

}

activity抽基类

public abstract class BaseActivity extends AppCompatActivity {
private T presenter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(getLayout());

    initView();

    presenter=getPresenter();
    presenter.attachView(presenter);
    initData();

}

protected abstract void initData();

protected abstract void initView();

protected abstract T getPresenter();

protected abstract int getLayout();

@Override
protected void onDestroy() {
    super.onDestroy();
    presenter.deatchView();
    EventBus.getDefault().unregister(this);

}

}

展示列表

public class MainActivity extends BaseActivity implements ListView {

@BindView(R.id.myview)
MyView myview;
@BindView(R.id.rec)
RecyclerView rec;
private ListPresenter listPresenter;
// String keyword, int page, int count;
private String keyword="女装";
private int page = 1;
private int count = 20;
private MyAdapter myAdapter;




@Override
protected void initData() {


}

@Override
protected void initView() {
    ButterKnife.bind(this);
    EventBus.getDefault().register(this);
    GridLayoutManager linearLayoutManager = new GridLayoutManager(this,2);
    rec.setLayoutManager(linearLayoutManager);
    myview.setGetName(new MyView.onGetName() {
        @Override
        public void ongetName(String s) {
            listPresenter.onListP(s, page, count);
        }
    });

}

@Override
protected ListPresenter getPresenter() {
    //实例化P
    listPresenter = new ListPresenter(this);
    listPresenter.onListP(keyword, page, count);
    return listPresenter;
}

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

@Override
public void getListViewData(List<ListBean.ListResult> data) {

   myAdapter = new MyAdapter(MainActivity.this, data);
    Log.e("xxx",data.toString());
    rec.setAdapter(myAdapter);

}



/**
 * 获取EvenBus
 */
@Subscribe(threadMode = ThreadMode.MAIN)
public void getEventData(String commodityId){
    if (!TextUtils.isEmpty(commodityId)){
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        intent.putExtra("commodityId",commodityId);
        startActivity(intent);
    }
}

}

详情的M

public class ListModel {

public void getHttpData(String keyword,int page,int count){
    ApiService apiService = RetrofitUtils.getInstance().daGet(Api.ListUrl, ApiService.class);
    apiService.getLits(keyword, page, count).enqueue(new Callback<ListBean>() {
        @Override
        public void onResponse(Call<ListBean> call, Response<ListBean> response) {

            List<ListBean.ListResult> result = response.body().getResult();
            if (listListener!=null){
                listListener.onResult(result);
            }
        }

        @Override
        public void onFailure(Call<ListBean> call, Throwable t) {

        }
    });

}

/**
 * 定义接口
 */
public interface onListListener{
    void onResult(List<ListBean.ListResult> data);
}

public onListListener listListener;

public void setListListener(onListListener listListener){
    this.listListener=listListener;
}

}

v

public interface ListView {
void getListViewData(List<ListBean.ListResult> data);
}

p

public class ListPresenter extends BasePresenter{
private ListView listView;
private ListModel listModel;

public ListPresenter(ListView view){
    listView=view;
    listModel=new ListModel();
}

@Override
public void onListP(String keyword, int page, int count) {
    //调用M层的接口回调监听 获取数据给View
    listModel.getHttpData(keyword, page, count);

    listModel.setListListener(new ListModel.onListListener() {
        @Override
        public void onResult(List<ListBean.ListResult> data) {
            listView.getListViewData(data);
        }
    });
}

}

然后展示详情

public class Main2Activity extends BaseActivity implements DataView {

@BindView(R.id.return_btu)
TextView returnBtu;
@BindView(R.id.banner)
com.stx.xhb.xbanner.XBanner banner;
@BindView(R.id.price_details)
TextView priceDetails;
@BindView(R.id.num_details)
TextView numDetails;
@BindView(R.id.l)
RelativeLayout l;
@BindView(R.id.spxq)
TextView spxq;
@BindView(R.id.webview)
WebView webview;
private DataPresenter presenter;
private String commodityId;
String mimeType = "text/html";
String enCoding = "utf-8";


@Override
protected void initData() {
    presenter.onRelated(commodityId);
}

@Override
protected void initView() {
    ButterKnife.bind(this);
    Intent intent = getIntent();
    commodityId = intent.getStringExtra("commodityId");
}

@Override
protected DataPresenter getPresenter() {
    presenter = new DataPresenter(this);
    return presenter;
}

@Override
protected int getLayout() {

    return R.layout.activity_main2;
}

@Override
public void getDataViewData(DataBean.DataResult result) {
    Log.e("result_xxx",result.toString());
    /**
     * 轮播
     */
    final ArrayList<String> list = new ArrayList<>();


    String picture = result.getPicture();
    String[] split = picture.split(",");
    for (int i=0;i<split.length;i++){
        list.add(split[1]);
        //Log.i("getDetailData", "getDetailData: "+split[i]);
    }
    banner.setData(list,null);
    banner.loadImage(new XBanner.XBannerAdapter() {
        @Override
        public void loadBanner(XBanner banner, Object model, View view, int position) {
            Glide.with(Main2Activity.this).load(list.get(position)).into((ImageView) view);

        }
    });
    /**
     * 其它赋值
     */
    priceDetails.setText("¥"+result.getPrice());
    numDetails.setText("已售"+result.getCommentNum() + "件");
    webview.loadDataWithBaseURL(null,result.getDetails(),mimeType,enCoding,null);


}

@OnClick(R.id.return_btu)
public void onViewClicked() {
    //返回上一页
    finish();
}

}

展示

    <com.example.xl.view.MyView
        android:id="@+id/myview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></com.example.xl.view.MyView>

    <android.support.v4.widget.SwipeRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9">


                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/rec"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                    </android.support.v7.widget.RecyclerView>


    </android.support.v4.widget.SwipeRefreshLayout>
```

详情

<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=".Main2Activity"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/return_btu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="<"
            android:textSize="30sp" />

        <com.stx.xhb.xbanner.XBanner
            android:id="@+id/banner"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_below="@id/return_btu" />

        <RelativeLayout
            android:background="#fff"
            android:id="@+id/l"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/banner"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/price_details"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_margin="10dp"
                android:text="159"
                android:textColor="#f00"
                android:textSize="30sp" />

            <TextView
                android:id="@+id/num_details"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_margin="10dp"
                android:text="已售"
                android:textSize="25sp" />
        </RelativeLayout>

        <TextView
            android:id="@+id/spxq"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/l"
            android:layout_margin="10dp"
            android:text="商品详情"
            android:textSize="25sp" />

        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/spxq"></WebView>
    </RelativeLayout>



</LinearLayout>

条目

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#fff">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:layout_margin="5dp"
        android:orientation="vertical">
        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/img_item"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_marginLeft="60dp"/>
        <TextView
            android:id="@+id/price_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:textColor="#f52"
            android:layout_marginLeft="20dp"
            android:text="$:222"/>
    </LinearLayout>


</LinearLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值