1activity_main.xml主页面布局
<?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=".MainActivity">
<RelativeLayout
android:id="@+id/layout_top"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorAccent"
>
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:background="@drawable/ic_left_name"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:text="购物车"
android:textSize="24sp"
android:background="#ffffff"
/>
</RelativeLayout>
<!--左边的列表-->
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle_left"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_below="@id/layout_top"
/>
<View
android:id="@+id/view_line"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_below="@id/layout_top"
android:layout_toRightOf="@id/recycle_left"
android:background="#333333"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycle_right"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/layout_top"
android:layout_toRightOf="@id/view_line"
android:layout_alignRight="@id/layout_top"
/>
</RelativeLayout>
2.左边Recycleview的条目布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/con"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/left_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:text="title"
android:textSize="24sp"
android:textColor="#00f"
/>
</android.support.constraint.ConstraintLayout>
3右面Recycleview的条目布局
<?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="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/type_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:padding="2dp"
android:textColor="#ffffff"
android:textSize="20sp"
android:text="name"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/right_item_recycle"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
</LinearLayout>
4.显示右面图片和标题的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:id="@+id/right_image"
android:layout_width="80dp"
android:layout_height="80dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<TextView
android:id="@+id/right_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@id/right_image"
app:layout_constraintTop_toTopOf="@id/right_image"
app:layout_constraintBottom_toBottomOf="@id/right_image"
app:layout_constraintRight_toRightOf="parent"
android:text="name"
android:textSize="20sp"
android:textColor="#f0f"
/>
</android.support.constraint.ConstraintLayout>
5.左面的适配器
package com.example.erjiliandong01.adaper;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
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.erjiliandong01.R;
import com.example.erjiliandong01.bean.LeftBean;
import java.util.ArrayList;
import java.util.List;
public class LeftAdaper extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<LeftBean.DataBean> mLeftData;
private Context mContext;
public LeftAdaper(Context mContext) {
this.mContext = mContext;
mLeftData = new ArrayList<>();
}
public void setmLeftData(List<LeftBean.DataBean> datas){
mLeftData = datas;
notifyDataSetChanged();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.left_item,viewGroup,false);
ViewHolderLeft holderLeft = new ViewHolderLeft(view);
return holderLeft;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
ViewHolderLeft holderLeft = (ViewHolderLeft) viewHolder;
holderLeft.left_text.setText(mLeftData.get(i).getName());
holderLeft.constraintLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(leftCallBaack!=null){
leftCallBaack.callBack(i,mLeftData.get(i).getCid());
}
}
});
}
@Override
public int getItemCount() {
return mLeftData.size();
}
class ViewHolderLeft extends RecyclerView.ViewHolder{
public final TextView left_text;
public final ConstraintLayout constraintLayout;
public ViewHolderLeft(@NonNull View itemView) {
super(itemView);
left_text = itemView.findViewById(R.id.left_text);
constraintLayout = itemView.findViewById(R.id.con);
}
}
private LeftCallBaack leftCallBaack;
public void setLeftCallBaack(LeftCallBaack leftCallBaack){
this.leftCallBaack = leftCallBaack;
}
//定义接口
public interface LeftCallBaack{
void callBack(int i,int cid);
}
}
6.右面的适配器
package com.example.erjiliandong01.adaper;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
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.erjiliandong01.R;
import com.example.erjiliandong01.bean.RightBean;
import java.util.ArrayList;
import java.util.List;
public class RightAdaper extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<RightBean.DataBean> mData;
private Context mContext;
public RightAdaper(Context mContext) {
this.mContext = mContext;
mData = new ArrayList<>();
}
public void setmData(List<RightBean.DataBean> datas){
mData = datas;
notifyDataSetChanged();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.right_item,viewGroup,false);
ViewHolderRight holderRight = new ViewHolderRight(view);
return holderRight;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
ViewHolderRight holderRight = (ViewHolderRight) viewHolder;
holderRight.type_name.setText(mData.get(i).getName());
RightItemAdaper itemAdaper = new RightItemAdaper(mContext);
LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
holderRight.right_item_recycle.setLayoutManager(layoutManager);
holderRight.right_item_recycle.setAdapter(itemAdaper);
holderRight.right_item_recycle.addItemDecoration(new DividerItemDecoration(mContext,DividerItemDecoration.VERTICAL));
itemAdaper.setmList(mData.get(i).getList());
}
@Override
public int getItemCount() {
return mData.size();
}
class ViewHolderRight extends RecyclerView.ViewHolder{
public final TextView type_name;
public final RecyclerView right_item_recycle;
public ViewHolderRight(@NonNull View itemView) {
super(itemView);
type_name = itemView.findViewById(R.id.type_name);
right_item_recycle = itemView.findViewById(R.id.right_item_recycle);
}
}
}
7.右面内容的适配器
package com.example.erjiliandong01.adaper;
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.bumptech.glide.Glide;
import com.example.erjiliandong01.R;
import com.example.erjiliandong01.bean.RightBean;
import java.util.ArrayList;
import java.util.List;
public class RightItemAdaper extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<RightBean.DataBean.ListBean> mList;
private Context mContext;
public RightItemAdaper(Context mContext) {
this.mContext = mContext;
mList = new ArrayList<>();
}
public void setmList(List<RightBean.DataBean.ListBean> lists){
mList = lists;
notifyDataSetChanged();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(mContext).inflate(R.layout.reigeh_item_type,viewGroup,false);
ViewHolderRigthItem holderRigthItem = new ViewHolderRigthItem(view);
return holderRigthItem;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
ViewHolderRigthItem holderRigthItem = (ViewHolderRigthItem) viewHolder;
Glide.with(mContext).load(mList.get(i).getIcon()).into(holderRigthItem.right_image);
holderRigthItem.right_name.setText(mList.get(i).getName());
}
@Override
public int getItemCount() {
return mList.size();
}
class ViewHolderRigthItem extends RecyclerView.ViewHolder{
public final ImageView right_image;
public final TextView right_name;
public ViewHolderRigthItem(@NonNull View itemView) {
super(itemView);
right_image = itemView.findViewById(R.id.right_image);
right_name = itemView.findViewById(R.id.right_name);
}
}
}
8.MainActivity页面
package com.example.erjiliandong01;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import com.example.erjiliandong01.adaper.LeftAdaper;
import com.example.erjiliandong01.adaper.RightAdaper;
import com.example.erjiliandong01.api.Apis;
import com.example.erjiliandong01.bean.LeftBean;
import com.example.erjiliandong01.bean.RightBean;
import com.example.erjiliandong01.presenter.PresentImpl;
import com.example.erjiliandong01.view.Iview;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements Iview {
private PresentImpl present;
private RecyclerView recycle_left;
private LeftAdaper leftAdaper;
private RecyclerView recycle_right;
private RightAdaper rightAdaper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
present = new PresentImpl(this);
initRecycleLeft();
initRecycleRight();
initData();
}
/**
* 获取左侧列表数据
*/
private void initData() {
Map<String, String> map = new HashMap<>();
present.startRequest(Apis.URL_PRODUCT_GET_CATAGORY, map, LeftBean.class);
}
/**
* 初始化右侧recyclerView,加载右侧adapter
*/
private void initRecycleRight() {
recycle_right = findViewById(R.id.recycle_right);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(OrientationHelper.VERTICAL);
recycle_right.setLayoutManager(layoutManager);
recycle_right.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
rightAdaper = new RightAdaper(this);
recycle_right.setAdapter(rightAdaper);
}
/**
* 初始化左侧recyclerView,加载左侧adapter
*/
private void initRecycleLeft() {
recycle_left = findViewById(R.id.recycle_left);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(OrientationHelper.VERTICAL);
recycle_left.setLayoutManager(layoutManager);
recycle_left.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
leftAdaper = new LeftAdaper(this);
recycle_left.setAdapter(leftAdaper);
//添加回调
leftAdaper.setLeftCallBaack(new LeftAdaper.LeftCallBaack() {
@Override
public void callBack(int i, int cid) {
//拿到cid之后,通过接口获得对应的数据,展示在右侧列表中
initRightData(cid);
}
});
}
/**
* 获取右侧列表数据
*/
private void initRightData(int cid) {
Map<String, String> map = new HashMap<>();
map.put("cid", String.valueOf(cid));
present.startRequest(Apis.URL_PRODUCT_GET_PRODUCT_CATAGORY, map, RightBean.class);
}
@Override
public void requestData(Object o) {
if (o instanceof LeftBean) {
LeftBean leftBean = (LeftBean) o;
if (leftBean == null || !leftBean.isSuccess()) {
Toast.makeText(MainActivity.this, leftBean.getMsg(), Toast.LENGTH_SHORT).show();
} else {
leftAdaper.setmLeftData(leftBean.getData());
}
} else if (o instanceof RightBean) {
RightBean rightBean = (RightBean) o;
if (rightBean == null || !rightBean.isSuccess()) {
Toast.makeText(MainActivity.this, rightBean.getMsg(), Toast.LENGTH_SHORT).show();
} else {
rightAdaper.setmData(rightBean.getData());
recycle_right.scrollToPosition(0);
}
}
}
@Override
public void requestFail(Object o) {
if (o instanceof Exception) {
Exception e = (Exception) o;
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "网络请求错误", Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
present.onDetach();
}
}
详细代码请访问以下链接
https://github.com/guoxinyu1995/yuekao/tree/master/erjiliandong01