左右联动(仿京东分类)

效果图

 整体布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".fragment.FenleiFragment">

   <android.support.v7.widget.RecyclerView
       android:id="@+id/left_recycler_view"
       android:layout_width="0dp"
       android:layout_weight="3"
       android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/right_recycler_view"
        android:layout_width="0dp"
        android:layout_weight="7"
        android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>

</LinearLayout>

activity或fragment

public class FenleiFragment extends Fragment implements IFenLeiView{
    @BindView(R.id.left_recycler_view)
    RecyclerView leftRecyclerView;
    @BindView(R.id.right_recycler_view)
    RecyclerView rightRecyclerView;
    Unbinder unbinder;
    private String cid = "2";
    private FenLeiPresenter fenLeiPresenter;
    private MyRightAdapter myRightAdapter;
    private MyLeftAdapter myLeftAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fenlei, container, false);
        unbinder = ButterKnife.bind(this, view);
        fenLeiPresenter = new FenLeiPresenter(FenleiFragment.this);
        fenLeiPresenter.getPresenterData();
       fenLeiPresenter.getRightPresenterData(cid);

        return view;
    }


    @Override
    public void getViewData(String data) {
        Gson gson = new Gson();
        final FenLeiBean fenLeiBean = gson.fromJson(data, FenLeiBean.class);
        List<FenLeiBean.DataBean> fenLeiBeanData = fenLeiBean.getData();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        leftRecyclerView.setLayoutManager(linearLayoutManager);
        //创建适配器
        myLeftAdapter = new MyLeftAdapter(getActivity(),fenLeiBeanData);


        myLeftAdapter.setSetOnItemClickListener(new MyLeftAdapter.SetOnItemClickListener() {
            @Override
            public void onItemClick(int position, List<FenLeiBean.DataBean> fenLeiBeanData) {
                fenLeiPresenter.getRightPresenterData(fenLeiBean.getData().get(position).getCid()+"");
                myRightAdapter.notifyDataSetChanged();
            }
        });
        leftRecyclerView.setAdapter(myLeftAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void getRightViewData(String data) {
        Gson rightGson = new Gson();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),3);
        rightRecyclerView.setLayoutManager(gridLayoutManager);
        RightBean rightBean = rightGson.fromJson(data, RightBean.class);
        List<RightBean.DataBean> rightBeanData = rightBean.getData();
        //创建适配器
        myRightAdapter = new MyRightAdapter(getActivity(),rightBeanData);
        rightRecyclerView.setAdapter(myRightAdapter);
    }
}

左边布局

<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:id="@+id/left_text_view"
        android:text="@string/jing"
        android:textSize="20sp"
        android:padding="10dp"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="wrap_content" />

    <View
        android:layout_width="match_parent"
        android:background="#666666"
        android:layout_height="1dp"></View>
</LinearLayout>

左边adapter

public class MyLeftAdapter extends RecyclerView.Adapter<MyLeftAdapter.ViewHolder> {

    private Context context;
    private List<FenLeiBean.DataBean> fenLeiBeanData;
    private SetOnItemClickListener setOnItemClickListener;

    public void setSetOnItemClickListener(SetOnItemClickListener setOnItemClickListener) {
        this.setOnItemClickListener = setOnItemClickListener;
    }

    public MyLeftAdapter(Context context, List<FenLeiBean.DataBean> fenLeiBeanData) {
        this.context = context;
        this.fenLeiBeanData = fenLeiBeanData;
    }

    //定义接口
    public interface SetOnItemClickListener{
        void onItemClick(int position,List<FenLeiBean.DataBean> fenLeiBeanData);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.left_layout, viewGroup, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;

    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        viewHolder.leftTextView.setText(fenLeiBeanData.get(i).getName());

        viewHolder.leftTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setOnItemClickListener.onItemClick(i,fenLeiBeanData);
            }
        });
    }

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

    @OnClick(R.id.left_text_view)
    public void onViewClicked() {

    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.left_text_view)
        TextView leftTextView;
        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this,view);
        }
    }
}

右边布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/right_img"
        android:layout_margin="10dp"
        android:layout_gravity="center"
        android:src="@mipmap/ic_launcher_round"
        android:layout_width="100dp"
        android:layout_height="100dp" />
    <TextView
        android:id="@+id/right_text_name"
        android:textSize="20sp"
        android:text="京东"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

右边adapter

public class MyRightAdapter extends RecyclerView.Adapter<MyRightAdapter.ViewHolder> {

    private Context context;
    private List<RightBean.DataBean> rightBeanData;

    public MyRightAdapter(Context context, List<RightBean.DataBean> rightBeanData) {
        this.context = context;
        this.rightBeanData = rightBeanData;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.rightlayout, viewGroup, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        Glide.with(context).load(rightBeanData.get(i).getList().get(i).getIcon()).into(viewHolder.rightImg);
        viewHolder.rightTextName.setText(rightBeanData.get(i).getName());
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.right_img)
        ImageView rightImg;
        @BindView(R.id.right_text_name)
        TextView rightTextName;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值