1. 程式人生 > >Android 使用BRVAH 實現二級選單。類似QQ分組

Android 使用BRVAH 實現二級選單。類似QQ分組

首先配置什麼的。點選這裡

程式碼:
兩個類,一個是一級的類,一個是二級的類

直接貼上

 package com.mogujie.tt.ui.model;

import com.chad.library.adapter.base.entity.AbstractExpandableItem;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.mogujie.tt.ui.adapter.FenZuAdapter;

/**
 * Created by luoxw on 2016/8/10.
 * 第一組
 */
public class Level0Item extends AbstractExpandableItem<Person> implements MultiItemEntity { public String title; public String subTitle;//標記 public Level0Item( String title, String subTitle) { this.subTitle = subTitle; this.title = title; } @Override public
int getItemType() { return FenZuAdapter.TYPE_LEVEL_0; } @Override public int getLevel() { return 1; } } package com.mogujie.tt.ui.model; import com.chad.library.adapter.base.entity.MultiItemEntity; import com.mogujie.tt.ui.adapter.FenZuAdapter; /** * Created by luoxw on 2016/8/10. * 第二組 */
public class Person implements MultiItemEntity{ public String name; public Person(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int getItemType() { return FenZuAdapter.TYPE_PERSON; } }

介面卡

 package com.mogujie.tt.ui.adapter;

import android.content.Context;
import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.mogujie.tt.ui.model.Level0Item;
import com.mogujie.tt.ui.model.Person;
import com.sankeyun.bb.R;

import java.util.List;

/**
 * Created by ${CWJ} on 2017/8/2.
 * 描述: 分組activity
 */

public class FenZuAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {
    private Context context;
    public static final int TYPE_LEVEL_0 = 0;
    public static final int TYPE_PERSON = 1;
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public FenZuAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(TYPE_LEVEL_0, R.layout.item_head);
        addItemType(TYPE_PERSON, R.layout.item_item);
//        addItemType(TYPE_PERSON, R.layout.item_expandable_lv2);
    }
    @Override
    protected void convert(final BaseViewHolder helper, final MultiItemEntity item) {
        switch (helper.getItemViewType()) {
            case TYPE_LEVEL_0:
                final Level0Item lv0 = (Level0Item) item;
                helper.setText(R.id.head_name, lv0.title)//父佈局標題
                        .setText(R.id.subTitle_name, lv0.subTitle);//下標
//                        .setImageResource(R.id.head_iv, lv0.isExpanded() ? R.drawable.arrow_b : R.drawable.arrow_r)
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = helper.getPosition();
//                        Log.d(TAG, "Level 0 item pos: " + pos);
                        if (lv0.isExpanded()) {
                            collapse(pos);
                        } else {
//                            if (pos % 3 == 0) {
//                                expandAll(pos, false);
//                            } else {
                            expand(pos);
//      }
                        }
                    }
                });
                break;
            case TYPE_PERSON:
                Person person = (Person) item;
                helper.setText(R.id.item_name, person.getName());
//                helper.itemView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        int cp = getParentPosition(person);
//                        ((Level0Item) getData().get(cp)).removeSubItem(person);
//                        getData().remove(helper.getClickPosition());
//                        notifyItemRemoved(helper.getClickPosition());
//                    }
//                });
                break;
        }
    }

}

顯示頁面
我這裡是Fragment

package com.mogujie.tt.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.mogujie.tt.ui.adapter.FenZuAdapter;
import com.mogujie.tt.ui.model.Level0Item;
import com.mogujie.tt.ui.model.Person;
import com.sankeyun.bb.R;

import java.util.ArrayList;
import java.util.List;

public class AddressBooksFragment_item extends Fragment {
    private FenZuAdapter adapter;
    private List<MultiItemEntity> list;
    private RecyclerView recyclerView;
    /**
     * @param args
     * Fragment 中的Fragment
     * 這裡要承載 四種類型的Aapater
     */
    private String title;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_address_books_item, container, false);
        recyclerView= (RecyclerView) v.findViewById(R.id.address_rl);
        //設定 recyclerView
//        final GridLayoutManager manager=new GridLayoutManager(getActivity(),2);
//        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
//            @Override
//            public int getSpanSize(int i) {
//                return adapter.getItemViewType(i)==FenZuAdapter.TYPE_PERSON ? 0:manager.getSpanCount();
//            }
//        });
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        initView();
        return v;
    }

   <!-- //公共方法 獲取Fragment
    public static Fragment getFragment(Bundle bundle) {
        AddressBooksFragment_item transactionDetailFragment = new AddressBooksFragment_item();
        transactionDetailFragment.setArguments(bundle);
        return transactionDetailFragment;
    }-->

    //獲取資料
   private ArrayList<MultiItemEntity> getDat(){
       int lv0Count = 5;
       int personCount = 5;
       ArrayList<MultiItemEntity> res = new ArrayList<>();
       Person p=new Person("人類");
       for (int i = 0; i < lv0Count; i++) {//設定外層資料
           Level0Item lv0 = new Level0Item( "第",i+"組");
           for (int k = 0; k < personCount; k++) {
               //設定內層資料
               lv0.addSubItem(i,p);
           }
           res.add(lv0);
       }
       return res;
    }
    public void initView() {
    //只看這個企業通訊錄裡面的
        list=new ArrayList<>();
        list=getDat();
        //adapter 初始化。
        title = getArguments().getString("address_type");
        if (title.equals("企業通訊錄")) {
            adapter=new FenZuAdapter(list);
            recyclerView.setAdapter(adapter);
//            adapter.expandAll();//預設展開全部 不寫就不展開

        } else if (title.equals("群組")) {

        } else if (title.equals("討論組")) {

        } else {//常用聯絡人

        }
    }
}

還有兩個佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:background="@color/bg_color"
    android:id="@+id/head_layout"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/head_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="父佈局名稱"
        android:textColor="@color/font_default_color"
        android:gravity="center|left"
        />
    <TextView
        android:id="@+id/subTitle_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="父佈局名稱"
        android:textColor="@color/font_default_color"
        android:gravity="center|left"
        />
</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/item_layout"
    android:background="@color/group_manager_user_title"
    android:orientation="vertical">
    <TextView
        android:id="@+id/item_name"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center|left"
        android:layout_marginLeft="10dp"
        android:textColor="@color/font_qianhei_color"
        android:text="子佈局Name" />
</LinearLayout>

最後效果圖:
有點醜。不過實現了。