1. 程式人生 > >Android 仿京東分類頁面

Android 仿京東分類頁面

activity_main

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

   <android.support.v7.widget.RecyclerView
       android:id="@+id/recycle"
       android:layout_width="0dp"
       android:layout_weight="3"
       android:layout_height="match_parent"/>
   <ScrollView
       android:layout_width="0dp"
       android:layout_weight="7"
       android:layout_height="match_parent"
       android:id="@+id/srcollView">
       <android.support.v7.widget.RecyclerView
           android:id="@+id/recycle_right"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"/>
   </ScrollView>
</LinearLayout>

MianActivity

public class MainActivity extends AppCompatActivity implements GroupPresenter.GroupListener, RightPresenter.RightListener {
    RecyclerView mRecyclerView,recycle_right;
    Madapter madapter;
    RightAdapter rightAdapter;
    RightPresenter rightPresenter;
    GroupPresenter groupPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecyclerView  =findViewById(R.id.recycle);
        recycle_right = findViewById(R.id.recycle_right);

        //設定左邊佈局
        initLeft();
        //設定右邊佈局   預設載入第一頁
        initRight("http://www.zhaoapi.cn/product/getProductCatagory?cid=1");
    }

    private void initRight(String urls) {

        //p層
        rightPresenter = new RightPresenter(this);
        rightPresenter.loadData(urls);
        //介面卡
        rightAdapter = new RightAdapter(MainActivity.this);
        recycle_right.setAdapter(rightAdapter);

        recycle_right.setLayoutManager(new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false));

    }


    private void initLeft() {
        //左邊佈局  介面卡
        madapter = new Madapter(this);
        mRecyclerView.setAdapter(madapter);

        //p層  請求資料
        groupPresenter = new GroupPresenter(this);
        groupPresenter.requestData();

        //佈局管理器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        //條目點選
        madapter.setMadapterListener(new Madapter.MadapterListener() {
            @Override
            public void setonitemclick(int cid) {

              Log.e("哈哈哈哈哈哈哈哈哈哈",cid+"");
                String path =  "http://www.zhaoapi.cn/product/getProductCatagory?cid="+cid;
                 initRight(path);

            }
        });
    }

    @Override
    public void datasuccess(List<GroupBean.DataBean> dataBeans) {

        madapter.setData(dataBeans);
    }

    @Override
    public void rightsuccess(List<RightBean.DataBean> dataBeans) {
       rightAdapter.setData(dataBeans);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

         rightPresenter = null;
         groupPresenter=null;

    }
}

左邊條目的介面卡 Madapter

**
 * date:2018/11/21
 * author:QMY(QMY)
 * function:
 */
public class Madapter extends RecyclerView.Adapter<Madapter.ViewHolder> {
    private final MainActivity mainActivity;
    private final List<GroupBean.DataBean> grouplist;

    public Madapter(MainActivity mainActivity) {
        this.mainActivity=mainActivity;
        grouplist = new ArrayList<>();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(mainActivity, R.layout.group, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.tv.setText(grouplist.get(i).getName());

        final GroupBean.DataBean bean = grouplist.get(i);
        //條目點選事件
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMadapterListener.setonitemclick(bean.getCid());
            }
        });
    }

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

    public void setData(List<GroupBean.DataBean> dataBeans) {
        grouplist.clear();
        grouplist.addAll(dataBeans);
        notifyDataSetChanged();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            tv = itemView.findViewById(R.id.tv);
        }
    }

    public interface  MadapterListener{
        void setonitemclick(int cid);
    }

    public MadapterListener mMadapterListener;

    public void setMadapterListener(MadapterListener madapterListener){
        mMadapterListener=madapterListener;
    }
}

//右邊第一個介面卡

public class RightAdapter extends RecyclerView.Adapter<RightAdapter.ViewHolder> {
    private final List<RightBean.DataBean> rightlist;
    private final MainActivity mainActivity;


    public RightAdapter(MainActivity mainActivity) {

        this.mainActivity =mainActivity;
        rightlist=new ArrayList<>();

    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(mainActivity, R.layout.right, null);

        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.name.setText(rightlist.get(i).getName());
        List<RightBean.DataBean.ListBean> list = rightlist.get(i).getList();

       RightAdapter1 rightAdapter1 = new RightAdapter1(list,mainActivity);
       viewHolder.recycle_right1.setAdapter(rightAdapter1);

       viewHolder.recycle_right1.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));

       rightAdapter1.setOnItemClickListener(new RightAdapter1.OnItemClickListener() {
           @Override
           public void onItemClickListener(int layoutPosition) {
               //點選條目的時候要根據點選的下標跳轉到相應的頁面,
               // 所以需要拿到解析完的集合list.get(傳過來的下標).地址
                Intent intent = new Intent(mainactivity, Goods.class);
                intent.putExtra("pscid",pscid);
                activity.startActivity(intent);

           }
       });
    }

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

    public void setData(List<RightBean.DataBean> dataBeans) {
        this.rightlist.clear();
        rightlist.addAll(dataBeans);
        notifyDataSetChanged();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        RecyclerView recycle_right1;
        TextView name;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            recycle_right1 = itemView.findViewById(R.id.recycle_right1);
            name = itemView.findViewById(R.id.name);
        }
    }

//右邊下邊適配

/**
 * date:2018/11/22
 * author:QMY(QMY)
 * function:
 */
class RightAdapter1 extends RecyclerView.Adapter<RightAdapter1.ViewHolder> {
    private final List<RightBean.DataBean.ListBean> list;
    private final MainActivity mainactivity;

    public RightAdapter1(List<RightBean.DataBean.ListBean> list, MainActivity mainActivity) {
        this.list=list;
        this.mainactivity = mainActivity;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(mainactivity, R.layout.right1, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {

        viewHolder.name.setText(list.get(i).getName());
        ImageLoader.getInstance().displayImage(list.get(i).getIcon(),
                viewHolder.image,ImageLoderConfigs.getimageoption(mainactivity));

        viewHolder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(list.get(i).getPscid());
            }
        });

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView name;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            image = itemView.findViewById(R.id.image);
            name = itemView.findViewById(R.id.name);
        }
    }

    //實現介面
    private OnItemClickListener mOnItemClickListener;
    //建立介面
    public interface OnItemClickListener{
        void onItemClickListener(int layoutPosition);
    }
    //外界呼叫的方法
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener= onItemClickListener;
    }

}

//左邊條目p層

public class GroupPresenter {

    public  void requestData(){
       String path =  "http://www.zhaoapi.cn/product/getCatagory";
        OkHttpUtils.getInstance().doGet(path, new OkHttpUtils.Utils() {
            @Override
            public void failed() {

            }

            @Override
            public void success(String string) {
                GroupBean groupBean = new Gson().fromJson(string, GroupBean.class);
                mGroupListener.datasuccess(groupBean.getData());
            }
        });
    }

    public interface GroupListener{
        void datasuccess(List<GroupBean.DataBean> dataBeans);
    }

    public GroupListener mGroupListener;

    public GroupPresenter(GroupListener groupListener) {
        mGroupListener = groupListener;
    }
}

右邊p層

public class RightPresenter {



     public void loadData(String urls){

         OkHttpUtils.getInstance().doGet(urls, new OkHttpUtils.Utils() {
             @Override
             public void failed() {

             }

             @Override
             public void success(String string) {
                 //解析
                 RightBean rightBean = new Gson().fromJson(string, RightBean.class);
                 List<RightBean.DataBean> data = rightBean.getData();
                 Log.e("gsaga",rightBean+"");

                 mRightListener.rightsuccess(data);
             }
         });



     }

     public interface  RightListener{
         void rightsuccess(List<RightBean.DataBean> dataBeans);
     }
     public RightListener mRightListener;

    public RightPresenter(RightListener rightListener) {
        mRightListener = rightListener;
    }
}

左邊佈局

<?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="match_parent"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"
        android:text="哈哈哈哈"
        android:textSize="16sp"/>
</android.support.constraint.ConstraintLayout>

右上

<?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="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:textSize="25sp"
        android:text="哈哈哈"
        />
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        app:layout_constraintTop_toBottomOf="@+id/name"
        android:layout_height="wrap_content"
        android:id="@+id/recycle_right1"/>
</android.support.constraint.ConstraintLayout>

右下

<?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="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/image"
        android:src="@mipmap/ic_launcher"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/image"
        android:id="@+id/name"
        />
</android.support.constraint.ConstraintLayout>