1.view
package com.example.week20.mvp;
import com.example.week20.bean.Bean;
import com.example.week20.bean.User;
import java.util.List;
public interface IView {
interface View{
void showData(List<Bean.DataBean> list);
void showinfo(List<User.DataBean> data);
}
interface Ipresenter<IView>{
void attData(IView iView);
void deleData(IView iView);
void infoData(String url);
void info(String url);
}
interface IModel{
interface oncallback{
void stringmag(List<Bean.DataBean> list);
}
interface Click{
void stringmmm(List<User.DataBean> data);
}
void requestData(String url,oncallback oncallback);
void request(String utl,Click click);
}
}
2.presenter
package com.example.week20.mvp;
import android.text.TextUtils;
import com.example.week20.bean.Bean;
import com.example.week20.bean.User;
import java.lang.ref.WeakReference;
import java.util.List;
public class IPresenter implements IView.Ipresenter<IView.View>{
IView.View view;
IModel model;
private WeakReference vweakReference;
private WeakReference mweakReference;
@Override
public void attData(IView.View view) {
this.view=view;
model=new IModel();
vweakReference = new WeakReference(view);
mweakReference = new WeakReference(model);
}
//内存释放
@Override
public void deleData(IView.View view) {
vweakReference.clear();
mweakReference.clear();
}
@Override
public void infoData(String url) {
if (!TextUtils.isEmpty(url)){
model.requestData(url, new IView.IModel.oncallback() {
//展示列表
@Override
public void stringmag(List<Bean.DataBean> list) {
view.showData(list);
}
});
}
}
//展示/商品
@Override
public void info(String url) {
if (!TextUtils.isEmpty(url)){
model.request(url, new IView.IModel.Click() {
@Override
public void stringmmm(List<User.DataBean> data) {
view.showinfo(data);
}
});
}
}
}
3.model
package com.example.week20.mvp;
import com.example.week20.bean.Bean;
import com.example.week20.bean.User;
import com.example.week20.ui.OkHttpUtlis;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class IModel implements IView.IModel{
//展示列表
@Override
public void requestData(String url, final oncallback oncallback) {
OkHttpUtlis.getinstan().get(url, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Gson gson=new Gson();
Bean bean = gson.fromJson(response.body().string(), Bean.class);
List<Bean.DataBean> list = bean.getData();
oncallback.stringmag(list);
}
});
}
@Override
public void request(String utl, final Click click) {
OkHttpUtlis.getinstan().get(utl, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Gson gson=new Gson();
User user = gson.fromJson(response.body().string(), User.class);
List<User.DataBean> data = user.getData();
click.stringmmm(data);
}
});
}
//展示商品
}
4.mainactivity
package com.example.week20;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.Toast;
import com.example.week20.bean.Bean;
import com.example.week20.bean.User;
import com.example.week20.mvp.IPresenter;
import com.example.week20.mvp.IView;
import com.example.week20.myadapter.Myadapter;
import com.example.week20.myadapter.Myadapter2;
import com.google.gson.Gson;
import java.util.List;
public class MainActivity extends AppCompatActivity implements IView.View {
private RecyclerView recyclerview1,recyclerview2;
private IPresenter iPresenter;
private Myadapter myadapter;
private Myadapter2 myadapter2;
private List<Bean.DataBean> lisbs;
private int cid=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerview1 = findViewById(R.id.recyclerview1);
recyclerview2=findViewById(R.id.recyclerview2);
iPresenter = new IPresenter();
iPresenter.attData(this);
//列表
lei();
//列表
shang();
}
//列表
private void lei() {
iPresenter.infoData("http://www.zhaoapi.cn/product/getCatagory");
StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL);
recyclerview1.setLayoutManager(layoutManager);
myadapter = new Myadapter(MainActivity.this);
recyclerview1.setAdapter(myadapter);
}
//商品
private void shang() {
LinearLayoutManager layoutManager=new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerview2.setLayoutManager(layoutManager);
}
@Override
protected void onDestroy() {
super.onDestroy();
iPresenter.deleData(this);
}
//展示列表
@Override
public void showData(final List<Bean.DataBean> list) {
runOnUiThread(new Runnable() {
@Override
public void run() {
lisbs = list;
myadapter.setList(list);
myadapter.setItemClickListener(new Myadapter.itemClickListener() {
@Override
public void clickListener(int position) {
cid = lisbs.get(position).getCid();
iPresenter.info("http://www.zhaoapi.cn/product/getProductCatagory?cid="+cid);
}
});
}
});
}
//展示商品
@Override
public void showinfo(final List<User.DataBean> data) {
runOnUiThread(new Runnable() {
@Override
public void run() {
myadapter2 = new Myadapter2(data,MainActivity.this);
recyclerview2.setAdapter(myadapter2);
}
});
}
}
5.adapter
package com.example.week20.myadapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.week20.R;
import com.example.week20.bean.Bean;
import java.util.ArrayList;
import java.util.List;
public class Myadapter extends RecyclerView.Adapter<Myadapter.ViewHodel> {
private List<Bean.DataBean> list=new ArrayList<>();
private Context context;
public Myadapter( Context context) {
this.context = context;
}
@NonNull
@Override
public Myadapter.ViewHodel onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = View.inflate(context,R.layout.itm1,null);
ViewHodel viewHodel=new ViewHodel(view);
return viewHodel;
}
@Override
public void onBindViewHolder(@NonNull Myadapter.ViewHodel holder, final int position) {
holder.text_name1.setText(list.get(position).getName());
holder.text_name1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
itemClickListener.clickListener(position);
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public void setList(List<Bean.DataBean> list) {
this.list = list;
notifyDataSetChanged();
}
class ViewHodel extends RecyclerView.ViewHolder{
private TextView text_name1;
public ViewHodel(View itemView) {
super(itemView);
text_name1 = itemView.findViewById(R.id.text_name1);
}
}
private itemClickListener itemClickListener;
public void setItemClickListener(Myadapter.itemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
public interface itemClickListener{
void clickListener(int position);
}
}
6.adapter2
package com.example.week20.myadapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.week20.R;
import com.example.week20.bean.Bean;
import com.example.week20.bean.User;
import java.util.ArrayList;
import java.util.List;
public class Myadapter2 extends RecyclerView.Adapter<Myadapter2.ViewHodel> {
private List<User.DataBean> data;
private Context context;
public Myadapter2(List<User.DataBean> data, Context context) {
this.data = data;
this.context = context;
}
@NonNull
@Override
public Myadapter2.ViewHodel onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.itm2, parent, false);
ViewHodel viewHodel=new ViewHodel(view);
return viewHodel;
}
@Override
public void onBindViewHolder(@NonNull Myadapter2.ViewHodel holder, int position) {
holder.text_name2.setText(data.get(position).getName());
StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
holder.recyclerview3.setLayoutManager(layoutManager);
Myadapter3 myadapter3=new Myadapter3(data.get(position).getList(),context);
holder.recyclerview3.setAdapter(myadapter3);
}
@Override
public int getItemCount() {
return data.size();
}
class ViewHodel extends RecyclerView.ViewHolder{
private RecyclerView recyclerview3;
private TextView text_name2;
public ViewHodel(View itemView) {
super(itemView);
text_name2 = itemView.findViewById(R.id.text_name2);
recyclerview3 = itemView.findViewById(R.id.recyclerview3);
}
}
}
7.adapter3
package com.example.week20.myadapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.week20.R;
import com.example.week20.bean.User;
import com.squareup.picasso.Picasso;
import java.util.List;
public class Myadapter3 extends RecyclerView.Adapter<Myadapter3.ViewHodel> {
private List<User.DataBean.ListBean> list;
private Context context;
public Myadapter3(List<User.DataBean.ListBean> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public Myadapter3.ViewHodel onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.itm3, parent, false);
ViewHodel viewHodel=new ViewHodel(view);
return viewHodel;
}
@Override
public void onBindViewHolder(@NonNull Myadapter3.ViewHodel holder, int position) {
holder.text_name3.setText(list.get(position).getName());
Picasso.with(context).load(list.get(position).getIcon()).into(holder.ima_name);
}
@Override
public int getItemCount() {
return list.size();
}
class ViewHodel extends RecyclerView.ViewHolder{
private ImageView ima_name;
private TextView text_name3;
public ViewHodel(View itemView) {
super(itemView);
ima_name = itemView.findViewById(R.id.ima_name);
text_name3 = itemView.findViewById(R.id.text_name3);
}
}
}
8.布局mainactivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:background="@drawable/buju1"
android:layout_width="match_parent"
android:layout_height="50dp">
<ImageView
android:src="@drawable/b"
android:layout_width="50dp"
android:layout_height="match_parent" />
<EditText
android:hint="沙社傻逼"
android:background="@null"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview1"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview2"
android:layout_weight="7"
android:layout_width="0dp"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
9.itm1
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@drawable/buju2"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_name1"
android:text="xxx"
android:textSize="30sp"
android:gravity="center"
android:layout_width="500dp"
android:layout_height="150dp" />
</RelativeLayout>
10.itm2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:text="xxx"
android:textSize="30sp"
android:id="@+id/text_name2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="@+id/text_name2"
android:layout_below="@+id/text_name2"
/>
</LinearLayout>
11.itm3
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingBottom="60dp"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/a"
android:id="@+id/ima_name"
android:layout_width="160dp"
android:layout_height="160dp" />
<TextView
android:id="@+id/text_name3"
android:layout_width="wrap_content"
android:text="xxx"
android:textSize="20sp"
android:layout_height="wrap_content" />
</LinearLayout>