android实现商品分组主要逻辑分析

首先我这里用到的布局文件
用到的是RecyclerView和ScrollView组合

<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"
    tools:context=".ui.fragment.BlankFragment2"
    android:orientation="horizontal"
    >

  <LinearLayout
      android:layout_width="0dp"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:layout_weight="3"
      >

      <TextView
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:text="国际名牌"
          android:gravity="center_horizontal"
          android:textSize="20sp"
          android:textColor="#000"
          android:layout_weight="1"
          />
      <TextView
          android:layout_width="match_parent"
          android:layout_height="1dp"
          android:text="66666"
          android:textColor="#000"
          android:layout_weight="0.02"
          />
      <android.support.v7.widget.RecyclerView
          android:id="@+id/LeftRecyclerView"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="15"
          android:layout_marginTop="2dp"
          ></android.support.v7.widget.RecyclerView>

  </LinearLayout>
   <ScrollView
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:layout_weight="7"
       >
       <LinearLayout
           android:id="@+id/mLinearLayout"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:padding="15dp"
           android:orientation="vertical"
           ></LinearLayout>

   </ScrollView>
    <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"
        tools:context=".ui.fragment.BlankFragment2"
        android:orientation="horizontal"
        >
    
      <LinearLayout
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:layout_weight="3"
          >
    
          <TextView
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:text="国际名牌"
              android:gravity="center_horizontal"
              android:textSize="20sp"
              android:textColor="#000"
              android:layout_weight="1"
              />
          <TextView
              android:layout_width="match_parent"
              android:layout_height="1dp"
              android:text="66666"
              android:textColor="#000"
              android:layout_weight="0.02"
              />
          <android.support.v7.widget.RecyclerView
              android:id="@+id/LeftRecyclerView"
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="15"
              android:layout_marginTop="2dp"
              ></android.support.v7.widget.RecyclerView>
    
      </LinearLayout>
       <ScrollView
           android:layout_width="0dp"
           android:layout_height="match_parent"
           android:layout_weight="7"
           >
           <LinearLayout
               android:id="@+id/mLinearLayout"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:padding="15dp"
               android:orientation="vertical"
               ></LinearLayout>
    
       </ScrollView>

获取RecyclerView和ScrollView内部的LinearLayout的控件的id

 leftRecyclerView = inflate.findViewById(R.id.LeftRecyclerView);
 mLinearLayout = inflate.findViewById(R.id.mLinearLayout);

在左侧的RecyclerView通过自定义接口回调设置点击事件将两个接口对应的id值传过来
Adapter中的或左侧id

viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                leftAdapterListener.success(list.get(i).getCid()+"");
            }
        });

获取左侧的地址自定义一个变量获取传回来的id值

leftAdapter.setLeftAdapterListener(new LeftAdapter.LeftAdapterListener() {
                    @Override
                    public void success(String data) {
                        cid = data;
                        initRight();
                    }
                });

右侧的数据获取完成后进行右侧的展示

 //请空LinearLayout中的子空间
        mLinearLayout.removeAllViews();
        //判断集合是否为空
        if(data1 != null){
        //遍历集合
            for (int i = 0 ; i < data1.size() ; i++){
                    //动态创建一个textView
                    TextView textView = new TextView(getContext());
                    textView.setTextSize(20);
                    textView.setTextColor(R.color.hui);
                    //给每个组的textview的标题赋值
                    textView.setText(data1.get(i).getName());
                    //动态创建  RecyclerView
                    RecyclerView recyclerView = new RecyclerView(getContext());
                    //设置布局管理器
                    recyclerView.setLayoutManager(new GridLayoutManager(getContext(),3));
                    RightAdapter rightAdapter = new RightAdapter(data1,getActivity());
                    //设置适配器
                    recyclerView.setAdapter(rightAdapter);
                    //因为每次切换都要重新加载布局中的数据.所以要刷新适配器
                    rightAdapter.notifyDataSetChanged();
                    //添加动态生成的控件
                    mLinearLayout.addView(textView);
                    mLinearLayout.addView(recyclerView);
                }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值