权声明:本文为博主原创文章,遵循 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