效果图
这里就不粘工具类了
一.model
1.GetLeftListener(左侧数据监听类)
public interface GetLeftListener { void getLeftSuccess(String json); void getLeftError(String error); }
2.GetRightListener(右侧数据监听类)
public interface GetRightListener { void getRightSuccess(String json); void getRightError(String error); }
3.IModel
public interface IModel { //获取左侧列表数据 void getLeftList(String url, Map<String,String> map, GetLeftListener getLeftListener); //获取右侧列表数据 void getRightList(String url, Map<String,String> map,GetRightListener getRightListener); }
4.ModelImpl
public class ModelImpl implements IModel{ private static final String TAG = "ModelImpl----"; @Override public void getLeftList(String url, Map<String, String> map, final GetLeftListener getLeftListener) { HttpUtils httpUtils = HttpUtils.getHttpUtils(); httpUtils.okPost(url,map); httpUtils.setOkLoadListener(new OkLoadListener() { @Override public void okLoadSuccess(String json) { Log.d(TAG, "okLoadSuccess: "+json); getLeftListener.getLeftSuccess(json); } @Override public void okLoadError(String error) { Log.d(TAG, "okLoadError: "+error); getLeftListener.getLeftError(error); } }); } @Override public void getRightList(String url, Map<String, String> map, final GetRightListener getRightListener) { HttpUtils httpUtils = HttpUtils.getHttpUtils(); httpUtils.okPost(url,map); httpUtils.setOkLoadListener(new OkLoadListener() { @Override public void okLoadSuccess(String json) { Log.d(TAG, "okLoadSuccess: "+json); getRightListener.getRightSuccess(json); } @Override public void okLoadError(String error) { Log.d(TAG, "okLoadError: "+error); getRightListener.getRightSuccess(error); } }); } }
二.presenter包
1.IPresenter
public interface IPresenter { //显示左侧列表 void showLeftToView(IModel iModel, IMainView iMainView); //显示右侧列表 void showRightToView(IModel iModel,IMainView iMainView); }
2.Presenter
public class Presenter implements IPresenter{ private static final String TAG = "Presenter----------"; @Override public void showLeftToView(IModel iModel, final IMainView iMainView) { Map<String,String> map = new HashMap<>(); iModel.getLeftList(HttpConfig.left_url, map, new GetLeftListener() { @Override public void getLeftSuccess(String json) { Gson gson = new Gson(); LeftBean leftBean = gson.fromJson(json, LeftBean.class); if(leftBean.getCode().equals("0")){ iMainView.showLeftView(leftBean.getData()); }else { Log.d(TAG, "getLeftSuccess: "+json); } } @Override public void getLeftError(String error) { Log.d(TAG, "getLeftError: "+error); } }); } @Override public void showRightToView(IModel iModel, final IMainView iMainView) { Log.d(TAG, iMainView.getCid()+"执行了"); Map<String,String> map = new HashMap<>(); Log.d(TAG, "执行了"+iMainView.getCid()); map.put("cid",iMainView.getCid()); iModel.getRightList(HttpConfig.right_url, map, new GetRightListener() { @Override public void getRightSuccess(String json) { Gson gson = new Gson(); RightBean rightBean = gson.fromJson(json, RightBean.class); if(rightBean.getCode().equals("0")){ iMainView.showRightView(rightBean.getData()); }else { Log.d(TAG, "getRightSuccess: "+json); } } @Override public void getRightError(String error) { Log.d(TAG, "getRightError: "+error); } }); } }
三.view包
1.IMainView
1.IMainView
public interface IMainView { void showLeftView(List<LeftBean.DataBean> list); void showRightView(List<RightBean.DataBean> list); String getCid(); }
2.MainActivity public class MainActivity extends AppCompatActivity implements IMainView{
private static final String TAG = "MainActivity";
private ListView left_listview;
private ListView right_listview;
private List<LeftBean.DataBean> list;
private String cid;
private Presenter presenter;
private ListView left_listview;
private ListView right_listview;
private List<LeftBean.DataBean> list;
private String cid;
private Presenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initDatas();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initDatas();
}
private void initDatas() {
presenter = new Presenter();
presenter.showLeftToView(new ModelImpl(),this);
}
presenter = new Presenter();
presenter.showLeftToView(new ModelImpl(),this);
}
private void initViews() {
left_listview = (ListView) findViewById(R.id.left_listview);
right_listview = (ListView)findViewById(R.id.right_listview);
//设置条目点击事件
left_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MainActivity.this.cid = list.get(position).getCid() + "";
//调用p层的方法
presenter.showRightToView(new ModelImpl(),MainActivity.this);
}
});
}
left_listview = (ListView) findViewById(R.id.left_listview);
right_listview = (ListView)findViewById(R.id.right_listview);
//设置条目点击事件
left_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
MainActivity.this.cid = list.get(position).getCid() + "";
//调用p层的方法
presenter.showRightToView(new ModelImpl(),MainActivity.this);
}
});
}
@Override
public void showLeftView(List<LeftBean.DataBean> list) {
this.list = list;
Log.d(TAG, "showLeftView: "+list);
MyAdapter1 myAdapter1 = new MyAdapter1(this,list);
left_listview.setAdapter(myAdapter1);
//显示右侧的默认值,就是cid=1
int cid = list.get(0).getCid();
MainActivity.this.cid = cid+"";
presenter.showRightToView(new ModelImpl(),MainActivity.this);
}
public void showLeftView(List<LeftBean.DataBean> list) {
this.list = list;
Log.d(TAG, "showLeftView: "+list);
MyAdapter1 myAdapter1 = new MyAdapter1(this,list);
left_listview.setAdapter(myAdapter1);
//显示右侧的默认值,就是cid=1
int cid = list.get(0).getCid();
MainActivity.this.cid = cid+"";
presenter.showRightToView(new ModelImpl(),MainActivity.this);
}
@Override
public void showRightView(List<RightBean.DataBean> list) {
Log.d(TAG, "右侧------: "+list);
MyAdapter2 myAdapter2 = new MyAdapter2(this,list);
right_listview.setAdapter(myAdapter2);
}
public void showRightView(List<RightBean.DataBean> list) {
Log.d(TAG, "右侧------: "+list);
MyAdapter2 myAdapter2 = new MyAdapter2(this,list);
right_listview.setAdapter(myAdapter2);
}
@Override
public String getCid() {
return this.cid;
}
}
2.5activity_main
<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="horizontal"
tools:context="com.example.lenovo.day16_fenlei.view.MainActivity">
public String getCid() {
return this.cid;
}
}
2.5activity_main
<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="horizontal"
tools:context="com.example.lenovo.day16_fenlei.view.MainActivity">
<ListView
android:layout_weight="1"
android:id="@+id/left_listview"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<ListView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:id="@+id/right_listview"/>
android:layout_weight="1"
android:id="@+id/left_listview"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<ListView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:id="@+id/right_listview"/>
</LinearLayout>
3.MyAdapter1
public class MyAdapter1 extends BaseAdapter{
private Context context;
private List<LeftBean.DataBean> list;
public MyAdapter1(Context context, List<LeftBean.DataBean> list) {
this.context = context;
this.list = list;
}
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder myViewHolder = null;
if(convertView==null){
convertView = View.inflate(context, R.layout.left_item,null);
TextView textView = (TextView) convertView.findViewById(R.id.left_item_tv);
myViewHolder =new MyViewHolder(textView);
convertView.setTag(myViewHolder);
}else{
myViewHolder = (MyViewHolder) convertView.getTag();
}
myViewHolder.getTextView().setText(list.get(position).getName());
return convertView;
}
class MyViewHolder{
private TextView textView;
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder myViewHolder = null;
if(convertView==null){
convertView = View.inflate(context, R.layout.left_item,null);
TextView textView = (TextView) convertView.findViewById(R.id.left_item_tv);
myViewHolder =new MyViewHolder(textView);
convertView.setTag(myViewHolder);
}else{
myViewHolder = (MyViewHolder) convertView.getTag();
}
myViewHolder.getTextView().setText(list.get(position).getName());
return convertView;
}
class MyViewHolder{
private TextView textView;
public MyViewHolder(TextView textView) {
this.textView = textView;
}
this.textView = textView;
}
public TextView getTextView() {
return textView;
}
return textView;
}
public void setTextView(TextView textView) {
this.textView = textView;
}
}
}
3.5 left_item
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/left_item_tv"
android:text="商品"
android:textSize="20sp">
this.textView = textView;
}
}
}
3.5 left_item
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="@+id/left_item_tv"
android:text="商品"
android:textSize="20sp">
</TextView>
4.MyAdapter2
public class MyAdapter2 extends BaseAdapter{
private Context context;
private List<RightBean.DataBean> list;
public class MyAdapter2 extends BaseAdapter{
private Context context;
private List<RightBean.DataBean> list;
public MyAdapter2(Context context, List<RightBean.DataBean> list) {
this.context = context;
this.list = list;
}
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder2 myViewHolder2 =null;
if(convertView==null){
convertView = View.inflate(context, R.layout.right_item,null);
TextView right_title = (TextView) convertView.findViewById(R.id.right_title);
GridView right_gv = (GridView) convertView.findViewById(R.id.right_gv);
myViewHolder2 = new MyViewHolder2(right_title,right_gv);
convertView.setTag(myViewHolder2);
}else{
myViewHolder2 = (MyViewHolder2) convertView.getTag();
}
myViewHolder2.getRight_tv().setText(list.get(position).getName());
List<RightBean.DataBean.ListBean> list= this.list.get(position).getList();
MyAdapter3 myAdapter3 = new MyAdapter3(context, list);
myViewHolder2.getRight_gv().setAdapter(myAdapter3);
myViewHolder2.getRight_gv().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(context,"Postiion"+position, Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
class MyViewHolder2{
private TextView right_tv;
private GridView right_gv;
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder2 myViewHolder2 =null;
if(convertView==null){
convertView = View.inflate(context, R.layout.right_item,null);
TextView right_title = (TextView) convertView.findViewById(R.id.right_title);
GridView right_gv = (GridView) convertView.findViewById(R.id.right_gv);
myViewHolder2 = new MyViewHolder2(right_title,right_gv);
convertView.setTag(myViewHolder2);
}else{
myViewHolder2 = (MyViewHolder2) convertView.getTag();
}
myViewHolder2.getRight_tv().setText(list.get(position).getName());
List<RightBean.DataBean.ListBean> list= this.list.get(position).getList();
MyAdapter3 myAdapter3 = new MyAdapter3(context, list);
myViewHolder2.getRight_gv().setAdapter(myAdapter3);
myViewHolder2.getRight_gv().setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(context,"Postiion"+position, Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
class MyViewHolder2{
private TextView right_tv;
private GridView right_gv;
public MyViewHolder2(TextView right_tv, GridView right_gv) {
this.right_tv = right_tv;
this.right_gv = right_gv;
}
this.right_tv = right_tv;
this.right_gv = right_gv;
}
public TextView getRight_tv() {
return right_tv;
}
return right_tv;
}
public void setRight_tv(TextView right_tv) {
this.right_tv = right_tv;
}
this.right_tv = right_tv;
}
public GridView getRight_gv() {
return right_gv;
}
return right_gv;
}
public void setRight_gv(GridView right_gv) {
this.right_gv = right_gv;
}
}
}
4.5 right_item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
this.right_gv = right_gv;
}
}
}
4.5 right_item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/right_title"
android:text="标题"
android:textSize="20sp"/>
<com.example.lenovo.day16_fenlei.view.MyGridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/right_gv"
android:numColumns="3">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/right_title"
android:text="标题"
android:textSize="20sp"/>
<com.example.lenovo.day16_fenlei.view.MyGridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/right_gv"
android:numColumns="3">
</com.example.lenovo.day16_fenlei.view.MyGridView>
</LinearLayout>
5.MyGridView
public class MyGridView extends GridView{
public MyGridView(Context context) {
super(context);
}
5.MyGridView
public class MyGridView extends GridView{
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
super(context, attrs);
}
public MyGridView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int i = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, i);
}
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int i = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, i);
}
}
6.MyAdapter3
public class MyAdapter3 extends BaseAdapter{
private Context context;
private List<RightBean.DataBean.ListBean> list;
public MyAdapter3(Context context, List<RightBean.DataBean.ListBean> list){
this.context=context;
this.list=list;
}
@Override
public int getCount() {
return list.size();
}
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder2 myViewHolder2=null;
if (convertView==null){
convertView=View.inflate(context, R.layout.gv_item,null);
ImageView imageView = (ImageView) convertView.findViewById(R.id.gv_pic);
TextView textView = (TextView) convertView.findViewById(R.id.gv_name);
if (convertView==null){
convertView=View.inflate(context, R.layout.gv_item,null);
ImageView imageView = (ImageView) convertView.findViewById(R.id.gv_pic);
TextView textView = (TextView) convertView.findViewById(R.id.gv_name);
myViewHolder2= new MyViewHolder2(imageView,textView);
convertView.setTag(myViewHolder2);
}
else {
myViewHolder2= (MyViewHolder2) convertView.getTag();
}
//赋值
myViewHolder2.getGv_tv().setText(list.get(position).getName());
//
Glide.with(context).load(list.get(position).getIcon()).into(myViewHolder2.getGv_pic());
return convertView;
}
class MyViewHolder2{
private ImageView gv_pic;
private TextView gv_tv;
else {
myViewHolder2= (MyViewHolder2) convertView.getTag();
}
//赋值
myViewHolder2.getGv_tv().setText(list.get(position).getName());
//
Glide.with(context).load(list.get(position).getIcon()).into(myViewHolder2.getGv_pic());
return convertView;
}
class MyViewHolder2{
private ImageView gv_pic;
private TextView gv_tv;
public MyViewHolder2(ImageView gv_pic, TextView gv_tv) {
this.gv_pic = gv_pic;
this.gv_tv = gv_tv;
}
this.gv_pic = gv_pic;
this.gv_tv = gv_tv;
}
public ImageView getGv_pic() {
return gv_pic;
}
return gv_pic;
}
public void setGv_pic(ImageView gv_pic) {
this.gv_pic = gv_pic;
}
this.gv_pic = gv_pic;
}
public TextView getGv_tv() {
return gv_tv;
}
return gv_tv;
}
public void setGv_tv(TextView gv_tv) {
this.gv_tv = gv_tv;
}
}
}
6.5 gv_item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
this.gv_tv = gv_tv;
}
}
}
6.5 gv_item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="@+id/gv_pic"
android:src="@mipmap/ic_launcher"
android:scaleType="centerCrop"/>
android:layout_width="60dp"
android:layout_height="60dp"
android:id="@+id/gv_pic"
android:src="@mipmap/ic_launcher"
android:scaleType="centerCrop"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/gv_name"
android:text="商品名称"
android:textSize="15sp"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/gv_name"
android:text="商品名称"
android:textSize="15sp"/>
</LinearLayout>
会用到的接口
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.okhttp3:okhttp:3.3.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
别忘了添加<uses-permission android:name="android.permission.INTERNET"/>权限