Android 商品分類(簡單實現 僅供參考)
阿新 • • 發佈:2018-11-16
//匯入依賴
implementation 'com.android.support:recyclerview-v7:28.0.0'
//首先在activity頁面新增兩個水平的RecyclerView
<?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:layout_width="100dp" android:layout_height="match_parent" android:id="@+id/recyr_left"/> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/recy_right"/> </LinearLayout>
//在MainActivity頁面進行訪問介面以及建立介面卡的基本操作
package soexample.umeng.com.fenlei01; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import com.google.gson.Gson; import java.util.ArrayList; import java.util.List; import soexample.umeng.com.fenlei01.adapter.MyLeftAdapter; import soexample.umeng.com.fenlei01.adapter.MyRightAdapter; import soexample.umeng.com.fenlei01.bean.ShopLeftBean; import soexample.umeng.com.fenlei01.bean.ShopRightBean; import soexample.umeng.com.fenlei01.utils.OkHttpHelper; public class MainActivity extends AppCompatActivity { private RecyclerView recy_left; private RecyclerView recy_right; private List<ShopLeftBean.DataBean> leftbean = new ArrayList<>(); private MyLeftAdapter myLeftAdapter; private int cid = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main ); recy_left = findViewById( R.id.recyr_left ); recy_right = findViewById( R.id.recy_right ); LinearLayoutManager linearLayoutManager = new LinearLayoutManager( this ); linearLayoutManager.setOrientation( LinearLayoutManager.VERTICAL ); recy_left.setLayoutManager( linearLayoutManager ); myLeftAdapter = new MyLeftAdapter( MainActivity.this, leftbean ); recy_left.setAdapter( myLeftAdapter ); leftbean = new ArrayList<>(); myLeftAdapter.setItemClick( new MyLeftAdapter.ItemClickListener() { @Override public void onItemClickListener(int position) { int id = leftbean.get( position ).getCid(); doHttpRight( id ); } } ); /*myLeftAdapter.setListener( new MyLeftAdapter.ItemClickListener() { @Override public void onItemClick(int position) { int id = leftbean.get( position ).getCid(); doHttpRight( id ); } } );*/ doHttpLeft(); LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager( this ); linearLayoutManager1.setOrientation( LinearLayoutManager.VERTICAL ); recy_right.setLayoutManager( linearLayoutManager1 ); } private void doHttpLeft() { new OkHttpHelper().get( "http://www.zhaoapi.cn/product/getCatagory" ).result( new OkHttpHelper.OkHttpHelperListener() { @Override public void Success(String data) { ShopLeftBean shopLeftBean = new Gson().fromJson( data, ShopLeftBean.class ); leftbean = shopLeftBean.getData(); myLeftAdapter.setList( leftbean ); } @Override public void Error() { } } ); doHttpRight( cid ); } private void doHttpRight(int cid) { new OkHttpHelper().get( "http://www.zhaoapi.cn/product/getProductCatagory?cid=" + cid ).result( new OkHttpHelper.OkHttpHelperListener() { @Override public void Success(String data) { ShopRightBean shopRightBean = new Gson().fromJson( data, ShopRightBean.class ); List<ShopRightBean.DataBean> rightbean = shopRightBean.getData(); MyRightAdapter rightAdapter = new MyRightAdapter( MainActivity.this, rightbean ); recy_right.setAdapter( rightAdapter ); } @Override public void Error() { } } ); } }
//左邊列表展示的的介面卡
package soexample.umeng.com.fenlei01.adapter; import android.content.Context; import android.support.annotation.NonNull; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import soexample.umeng.com.fenlei01.MainActivity; import soexample.umeng.com.fenlei01.R; import soexample.umeng.com.fenlei01.bean.ShopLeftBean; public class MyLeftAdapter extends RecyclerView.Adapter<MyLeftAdapter.MyViewHolder>{ private Context context; private List<ShopLeftBean.DataBean> leftbean = new ArrayList<>( ); public MyLeftAdapter(MainActivity mainActivity, List<ShopLeftBean.DataBean> leftbean) { context = mainActivity; this.leftbean = leftbean; } @NonNull @Override public MyLeftAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = View.inflate( context, R.layout.item,null); MyViewHolder myViewHolder = new MyViewHolder( view ); myViewHolder.shopName = view.findViewById( R.id.text_shop_Name ); return myViewHolder; } @Override public void onBindViewHolder(@NonNull MyLeftAdapter.MyViewHolder myViewHolder, final int i) { myViewHolder.shopName.setText( leftbean.get( i ).getName() ); myViewHolder.shopName.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { listener.onItemClickListener( i ); } } ); } @Override public int getItemCount() { return leftbean.size(); } public void setList(List<ShopLeftBean.DataBean> list) { leftbean = list; notifyDataSetChanged(); } public class MyViewHolder extends RecyclerView.ViewHolder { public MyViewHolder(@NonNull View itemView) { super( itemView ); } TextView shopName; } //由於RecyclerView本身沒有點選事件 所以通過介面回撥的方式進進行獲取id private ItemClickListener listener; public void setItemClick(ItemClickListener listener){ this.listener = listener; } public interface ItemClickListener{ void onItemClickListener(int position); } }
//右邊展示的介面卡
package soexample.umeng.com.fenlei.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import soexample.umeng.com.fenlei.R;
import soexample.umeng.com.fenlei.bean.RightBean;
public class RightAdapter extends RecyclerView.Adapter<RightAdapter.MyViewHolder>{
private List<RightBean.DataBean> list;
private Context context;
public RightAdapter(List<RightBean.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public RightAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate( context, R.layout.right_layout,null );
MyViewHolder viewHolder = new MyViewHolder( view );
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull RightAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.textView.setText( list.get( i ).getName() );
List<RightBean.DataBean.ListBean> listBeans = this.list.get( i ).getList();
GridLayoutManager layoutManager = new GridLayoutManager( context,3 );
myViewHolder.recyclerView.setLayoutManager( layoutManager );
myViewHolder.recyclerView.setAdapter( new ShopAdapter( listBeans,context ) );
}
@Override
public int getItemCount() {
return list.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView textView;
RecyclerView recyclerView;
public MyViewHolder(@NonNull View itemView) {
super( itemView );
textView = itemView.findViewById( R.id.text_title );
recyclerView = itemView.findViewById( R.id.recy_shop );
}
}
}
//右邊 涉及到了兩個RecyclerView
//所以要進行巢狀
//這裡是巢狀的介面卡
package soexample.umeng.com.fenlei01.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
import soexample.umeng.com.fenlei01.R;
import soexample.umeng.com.fenlei01.bean.ShopRightBean;
public class MyRightItemAdapter extends RecyclerView.Adapter<MyRightItemAdapter.MyViewHolder> {
private Context context;
private List<ShopRightBean.DataBean> rightbean = new ArrayList<>();
public MyRightItemAdapter(Context context, List<ShopRightBean.DataBean> rightbean) {
this.context = context;
this.rightbean = rightbean;
}
@NonNull
@Override
public MyRightItemAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate( context, R.layout.right_item_2, null );
MyViewHolder myViewHolder = new MyViewHolder( view );
myViewHolder.imageView = view.findViewById( R.id.imageView );
myViewHolder.textView = view.findViewById( R.id.textView );
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyRightItemAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.textView.setText( rightbean.get( i ).getList().get( i ).getName() );
Glide.with( context ).load( rightbean.get( i ).getList().get( i ).getIcon() ).into(myViewHolder.imageView );
}
@Override
public int getItemCount() {
return rightbean.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(@NonNull View itemView) {
super( itemView );
}
ImageView imageView;
TextView textView;
}
}
//成品打給就是這個樣了了
//一些動畫點選還未操作 粗糙實現 慎用