RecycleView嵌套與多級列表

首先這并不是多級列表實現(xiàn)的最優(yōu)方式脓钾,多級列表從表現(xiàn)形式上來看無非就是數(shù)據(jù)+縮進(或者顏色,字體大小等)竞帽,通過讀個RecycleView且套可以實現(xiàn)但是如果列表層級過多則會比較累贅,前面說到的縮進等表現(xiàn)形式完全可以在一個RecycleView中來實現(xiàn)具體可以參考以下鏈接
[1]: http://www.reibang.com/p/b76572fb4e60

布局文件

主界面布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="rar.uchannel.com.recyclelviewandrecycleview.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>

主RecycleView的Item的布局

<?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:orientation="vertical">

  <TextView
      android:id="@+id/tv"
      android:layout_width="match_parent"
      android:layout_height="30dp"
      android:background="@color/colorPrimary"
      android:gravity="center" />

  <LinearLayout
      android:id="@+id/ll"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">

      <android.support.v7.widget.RecyclerView
          android:id="@+id/recycleView_son"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />
  </LinearLayout>

</LinearLayout>

子RecycleView的Item的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_other"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_other"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

這玩意就不多說了,主要講嵌套中會出現(xiàn)的問題

主RecycleView的適配器

 public class CustomAdapter extends RecyclerView.Adapter<CustomViewHolder> {
     private Context context;
     private List<String> data;

     CustomAdapter(Context context, List<String> data) {
         this.context = context;
         this.data = data;
     }

     @Override
     public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         return new CustomViewHolder(LayoutInflater.from(context).inflate(R.layout.item_for_recycleview, parent, false));
     }


     @Override
     public void onBindViewHolder(final CustomViewHolder holder, final int position) {
         holder.textView.setText(data.get(position));
         if (onItemClickListener != null) {
             holder.textView.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View view) {
                     Log.e("xxx", holder.isRecyclable() + "");
                     int pos = holder.getLayoutPosition();
                     onItemClickListener.onItemClick(holder, pos);
                     holder.setIsRecyclable(false);//view復(fù)用造成一處點擊多處響應(yīng)的問題 設(shè)置為被點擊的item不可以被復(fù)用,
                     Log.e("xxx", holder.isRecyclable() + "");
                 }
             });
         }
     }

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

     void setOnItemClickListener(OnItemClickListener itemClickListener) {
         this.onItemClickListener = itemClickListener;
     }

     private OnItemClickListener onItemClickListener;

     interface OnItemClickListener {
         void onItemClick(CustomViewHolder holder, int position);
     }
 }

問題1:點擊RecycleView中的某個Item會出現(xiàn)多個Item響應(yīng)的問題歹袁,可能有規(guī)律。
解決:RecycleView中的點擊事件不要直接在Adapter中實現(xiàn)寝优,通過回調(diào)傳入在外部實現(xiàn)条舔,回調(diào)接口中參數(shù)為ViewHolder是應(yīng)為某些情況下點擊事件有一個控件響應(yīng)之后去修改其他控件的內(nèi)容》Ψ可以修改為對應(yīng)View

問題2:點擊一個RecycleView中的Item展開其次級列表時后面的Item也會復(fù)用當前已經(jīng)展開的Item(這個跟上面有點類似)
解決: 這里解決方案比較多設(shè)置Tag之類的都是可行的孟抗,究其原理無非是展開的Item不進行復(fù)用迁杨,這里采用的方式是直接設(shè)置Item不可復(fù)用holder.setIsRecyclable(false);這里僅僅對單個已經(jīng)點擊的item有效,而且當該item畫出屏幕外再次滑動回到屏幕內(nèi)時其復(fù)用狀態(tài)會重新更新為可以復(fù)用凄硼,可以看到代碼中的兩個Log铅协,當Item出現(xiàn)在屏幕內(nèi)首次點擊之前該Item都是可以復(fù)用的,對性能影響較小摊沉。

這里滑動沖突不需要進行處理警医,應(yīng)為RecycleView布局使用的是android:layout_height="wrap_content"如果各位在且套的時候要求內(nèi)部子RecycleView長度固定且可以滑動就需要通過setScrollEnabled處理滑動沖突,(額坯钦,直說吧這里我根本沒嘗試...)

MainActivity代碼

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    List<String> getData() {
        ArrayList<String> strings = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            strings.add("i=" + i);
        }
        return strings;
    }

    List<String> getData2Inner() {
        ArrayList<String> strings = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            strings.add("i=" + i);
        }
        return strings;
    }

    void initView() {
        RecyclerView recyclerView = findViewById(R.id.recycleView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        final CustomAdapter adapter = new CustomAdapter(this, getData());
        adapter.setOnItemClickListener(new CustomAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(CustomViewHolder holder, int position) {
                LinearLayoutManager llm = new LinearLayoutManager(MainActivity.this);
                llm.setOrientation(LinearLayoutManager.VERTICAL);
                InnerAdapter innerAdapter = new InnerAdapter(MainActivity.this, getData2Inner());
                holder.recyclerView.setLayoutManager(llm);
                holder.recyclerView.setAdapter(innerAdapter);
                ViewGroup.LayoutParams layoutParams = holder.linearLayout.getLayoutParams();
                layoutParams.height = 0;
                holder.linearLayout.setLayoutParams(layoutParams);
                startAnimation(holder.linearLayout);
            }
        });
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(linearLayoutManager);
    }


    public void startAnimation(final View view) {
        ValueAnimator animator;
        if (view.getHeight() == 0) {
            animator = ValueAnimator.ofInt(0, getMeasureHeight(view));
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                    layoutParams.height = (int) animation.getAnimatedValue();
                    view.setLayoutParams(layoutParams);
                }
            });

        } else {//if (view.getHeight() == getMeasureHeight(view))
            animator = ValueAnimator.ofInt(view.getHeight(), 0);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                    layoutParams.height = (int) animation.getAnimatedValue();
                    view.setLayoutParams(layoutParams);
                }
            });
        }
        animator.setDuration(1000);
        animator.start();
    }

    /**
     * 測量控件的顯示的時候的真實高度
     *
     * @param view target view
     * @return target view real height
     */
    public int getMeasureHeight(View view) {
        int w = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        view.measure(w, h);
        return view.getMeasuredHeight();
    }
}

其他ViewHolder和子Adapter就不貼了预皇,沒有什么需要注意的地方改怎么寫就怎么寫

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市婉刀,隨后出現(xiàn)的幾起案子吟温,更是在濱河造成了極大的恐慌,老刑警劉巖突颊,帶你破解...
    沈念sama閱讀 219,589評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鲁豪,死亡現(xiàn)場離奇詭異,居然都是意外死亡律秃,警方通過查閱死者的電腦和手機爬橡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,615評論 3 396
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來棒动,“玉大人糙申,你說我怎么就攤上這事〈遥” “怎么了柜裸?”我有些...
    開封第一講書人閱讀 165,933評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長粱锐。 經(jīng)常有香客問我疙挺,道長,這世上最難降的妖魔是什么怜浅? 我笑而不...
    開封第一講書人閱讀 58,976評論 1 295
  • 正文 為了忘掉前任铐然,我火速辦了婚禮,結(jié)果婚禮上恶座,老公的妹妹穿的比我還像新娘搀暑。我一直安慰自己,他們只是感情好奥裸,可當我...
    茶點故事閱讀 67,999評論 6 393
  • 文/花漫 我一把揭開白布险掀。 她就那樣靜靜地躺著,像睡著了一般湾宙。 火紅的嫁衣襯著肌膚如雪樟氢。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,775評論 1 307
  • 那天侠鳄,我揣著相機與錄音埠啃,去河邊找鬼。 笑死伟恶,一個胖子當著我的面吹牛碴开,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播博秫,決...
    沈念sama閱讀 40,474評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼潦牛,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了挡育?” 一聲冷哼從身側(cè)響起巴碗,我...
    開封第一講書人閱讀 39,359評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎即寒,沒想到半個月后橡淆,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,854評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡母赵,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,007評論 3 338
  • 正文 我和宋清朗相戀三年逸爵,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片凹嘲。...
    茶點故事閱讀 40,146評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡师倔,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出周蹭,到底是詐尸還是另有隱情溯革,我是刑警寧澤,帶...
    沈念sama閱讀 35,826評論 5 346
  • 正文 年R本政府宣布谷醉,位于F島的核電站致稀,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏俱尼。R本人自食惡果不足惜抖单,卻給世界環(huán)境...
    茶點故事閱讀 41,484評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望遇八。 院中可真熱鬧矛绘,春花似錦、人聲如沸刃永。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,029評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽斯够。三九已至囚玫,卻和暖如春喧锦,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背抓督。 一陣腳步聲響...
    開封第一講書人閱讀 33,153評論 1 272
  • 我被黑心中介騙來泰國打工燃少, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人铃在。 一個月前我還...
    沈念sama閱讀 48,420評論 3 373
  • 正文 我出身青樓阵具,卻偏偏與公主長得像,于是被迫代替她去往敵國和親定铜。 傳聞我的和親對象是個殘疾皇子阳液,可洞房花燭夜當晚...
    茶點故事閱讀 45,107評論 2 356

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,193評論 25 707
  • 【Android 控件 RecyclerView】 概述 RecyclerView是什么 從Android 5.0...
    Rtia閱讀 307,536評論 27 439
  • 事件: 多次聽到小伙伴分享關(guān)于被他人欺騙的事。 思維空性: 就那只筆揣炕,我會聽到會看到只是因為我有這樣的種子帘皿。 從小...
    黛兒微笑閱讀 238評論 0 2
  • 第一次看到小確幸這個詞時,不知道為什么腦子里突然出現(xiàn)“喵星人”伸懶腰祝沸,用舌頭清舔皮毛,還有用小肉爪子抓魚缸里的小魚...
    雪韻_蓮心閱讀 319評論 0 2
  • Luke 14:1-24 研讀問題: 7-14節(jié)罩锐。在這幾節(jié)中,耶穌的教導(dǎo)和世人的方法有什么相反之處涩惑?祂怎樣以身作則...
    biddon閱讀 424評論 0 0