二级联动

//MainActivity页面

public class MainActivity extends AppCompatActivity implements IView ,View.OnClickListener{
    private String urlleft="http://www.zhaoapi.cn/product/getCatagory";
    private String urlRigth="http://www.zhaoapi.cn/product/getProductCatagory";
    private RecyclerView rightrecycle;
    private RecyclerView leftrecycle;
    private LeftAdapter leftAdapter;
    private RightAdapter rightAdapter;
    IPersentermpl iPersentermpl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rightrecycle = findViewById(R.id.rightrecy);
        leftrecycle = findViewById(R.id.leftrecy);
        leftAdapter = new LeftAdapter(this);
        rightAdapter = new RightAdapter(this);
        initLeft();
        initRight();
    }
    private void initLeft(){
        iPersentermpl=new IPersentermpl(this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        leftrecycle.setLayoutManager(linearLayoutManager);

        leftrecycle.setAdapter(leftAdapter);
        HashMap<String,String> map=new HashMap<>();
        iPersentermpl.startRequest(urlleft,map,Left.class);
         leftAdapter.setOnClickListener(new LeftAdapter.onClickListener() {
             @Override
             public void onClick(int position, String cid) {
                 getData(cid);
             }
         });
        }

    private void getData(String uid) {
        HashMap<String,String>map = new HashMap<>();
        map.put("cid",uid);
        iPersentermpl.startRequest(urlRigth,map,Right.class);
    }

    private void initRight(){
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        rightrecycle.setLayoutManager(linearLayoutManager);
        rightrecycle.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        rightrecycle.setAdapter(rightAdapter);

//        adapterr=new RightAdapter(this);
//        rightrecycle.setAdapter(adapter);
//        Map<String,String> map=new HashMap<>();
//        map.put("cid",1+"");

    }
    @Override
    public void setonSuccess(Object o) {
        if(o instanceof Left){
            Left bean= (Left) o;
            leftAdapter.setList(bean.getData());
        }
        if(o instanceof Right){
            Right bean= (Right) o;
            rightAdapter.setList(bean.getData());
        }
    }

    @Override
    public void onClick(View v) {

    }
}

//LeftAdapter页面

public class LeftAdapter extends RecyclerView.Adapter<LeftAdapter.ViewHolder> {
    private Context context;
    private List<Left.DataBean> list;

    public LeftAdapter(Context context) {
        this.context = context;
        list=new ArrayList<>();
    }

    public void setList(List<Left.DataBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull LeftAdapter.ViewHolder viewHolder, final int i) {
        viewHolder.textView.setText(list.get(i).getName());
        viewHolder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(monClickListener!=null){
                    monClickListener.onClick(i,list.get(i).getCid()+"");
                }
            }
        });


    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        LinearLayout linearLayout;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.typetitle);
            linearLayout = itemView.findViewById(R.id.liner);
        }
    }
    private onClickListener monClickListener;
    public void setOnClickListener(onClickListener onClickListener){
        monClickListener = onClickListener;
    }
    //接口
    public interface  onClickListener{
        void onClick(int position,String cid);
    }
}

//RightAdapter页面

public class RightAdapter extends RecyclerView.Adapter<RightAdapter.ViewHolder> {
    private Context context;
    private List<Right.DataBean> list;

    public RightAdapter(Context context) {
        this.context = context;
        list=new ArrayList<>();
    }

    public void setList(List<Right.DataBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

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

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.textView.setText(list.get(i).getName());
        GoodsAdpter goodsAdpter = new GoodsAdpter(context);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(context,3);
        viewHolder.recyclerView.setLayoutManager(gridLayoutManager);
        viewHolder.recyclerView.setAdapter(goodsAdpter);
        viewHolder.recyclerView.addItemDecoration(new DividerItemDecoration(context,DividerItemDecoration.VERTICAL));
        goodsAdpter.setList(list.get(i).getList());
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        RecyclerView recyclerView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.rigthtitle);
            recyclerView = itemView.findViewById(R.id.goodsrecycle);
        }
    }
}

//GoodsAdapter页面

public class GoodsAdpter extends RecyclerView.Adapter<GoodsAdpter.ViewHolder> {
    private List<Right.DataBean.ListBean> list;
    private Context context;

    public GoodsAdpter(Context context) {
        this.context = context;
        list=new ArrayList<>();
    }

    public void setList(List<Right.DataBean.ListBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public GoodsAdpter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(context,R.layout.goods,null);
        GoodsAdpter.ViewHolder holder = new GoodsAdpter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull GoodsAdpter.ViewHolder viewHolder, int i) {
        Glide.with(context).load(list.get(i).getIcon()).into(viewHolder.imageView);
        viewHolder.textView.setText(list.get(i).getName());
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView textView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.goodstitle);
            imageView = itemView.findViewById(R.id.goodsimage);
        }
    }
}

//Main_XML页面

<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"
    tools:context=".MainActivity">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/leftrecy"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rightrecy"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="5"
        ></android.support.v7.widget.RecyclerView>
</LinearLayout>

//LeftXml页面
<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/liner"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/typetitle"
        />
</LinearLayout>

//right页面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <TextView

        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:id="@+id/rigthtitle"
        android:background="#FF99"
        android:textSize="20sp"
        />
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/goodsrecycle"
        />

//goodsxml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/goodstitle"
        />
    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/goodsimage"
        />
</LinearLayout>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值