Fragment放到RecyclerView的item佈局中

權聲明:本文爲博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連接和本聲明。
本文連接:https://blog.csdn.net/zp000123/article/details/81086710java


效果圖:
問題
1. 動態加載會致使全部的PicFragment都加載在第一個Item項中。
2. 靜態加載的時候,在佈局文件中添加id,或tag 都會應用崩潰。
3. 不設置id, 或tag會致使找不到Fragment。
1
2
3
思路
1. 靜態加載
2. 不設置id,和tag
3. 獲取 Fragment
    方法一: 經過全局的FragmentManager 的getFragments() ,因爲我使用的PicFragment根據類型獲取。
            但可能拿到的Fragment和我佈局的順序不一樣,雖然我測得順序是相同的。
    方法二: 經過ViewGroup 獲取子View,經過ChildView獲取到數據。
    下面主要介紹這種方式。
1
2
3
4
5
6
7
經過方法二獲取PicFragment:android

思路
item中獲取picFragment的根View
根據PicFragment的根View(RecyclerView)獲取數據源
先看Item的佈局文件,ll_edit中第二個元素爲靜態加載的fragment佈局

部分佈局:item.xmlui


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="vertical"
        android:paddingLeft="12dp"
        android:id="@+id/ll_edit"
        android:paddingRight="12dp">this


        <EditText
            android:id="@+id/et_evaluate_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:gravity="start"
            android:hint="@string/evaluate_content"
            android:minLines="7"
            android:paddingTop="10dp"
            android:textSize="14sp" />lua

        <fragment
            android:name="cn.j0.nmeshop.function.fragment.pic.PicFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"/>spa


    </LinearLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
注意: OrderEvaluateAdapter.java.net

LinearLayout llEdit = helper.itemView.findViewById(R.id.ll_edit);
//注意:getChildAt(1)就是fragment_pic.xml的根佈局,在這裏是RecyclerView 
//這樣咱們就能夠獲取到Adapter,而且獲取到其中的數據。
RecyclerView recyclerView = (RecyclerView) llEdit.getChildAt(1);
PicAdapter picAdapter = (PicAdapter) recyclerView.getAdapter();
PicFragment picFragment = picAdapter.getPicFragment(); //自定義的getPicFragment方法
1
2
3
4
5
6
注意: 在llEdit中我獲取到的View有兩個一個是 EditText ,一個是RecyclerView 。而這個RecyclerView是PicFrament的根佈局。重點就是這個加粗的地方。xml

PicFragment的佈局:fragment_pic.xmlblog

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rv_pic"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:overScrollMode="never">

</android.support.v7.widget.RecyclerView>
1
2
3
4
5
6
7
8
9
拿到這個RecyclerView天然它的數據源就很容易獲得了。好比獲取他的Fragment,以下。

PicAdapter.java

public class PicAdapter extends BaseMultiItemQuickAdapter<MultiplePicItem, BaseViewHolder> {
    private PicFragment picFragment;
    public void setPicFragment(PicFragment picFragment) {
        this.picFragment = picFragment;
    }

    public PicFragment getPicFragment() {
        return picFragment;
    }
 }
1
2
3
4
5
6
7
8
9
10
最後庫說明:

    1. 使用的v4的Fragment

    2.  supportVersion = '27.1.1'
————————————————
版權聲明:本文爲CSDN博主「zp000123」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連接及本聲明。
原文連接:https://blog.csdn.net/zp000123/article/details/81086710

 

 

https://blog.csdn.net/zp000123/article/details/81086710

相關文章
相關標籤/搜索