【发布时间】:2017-06-19 02:14:04
【问题描述】:
我的Fragment 中有两个RecyclerView。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#E6E6E6">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:gravity="left"
android:text="Trending"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:layout_below="@+id/textview"
/>
<TextView
android:id="@+id/popular_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#000"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:gravity="left"
android:text="Popular"
android:layout_below="@+id/recycler_view"
android:layout_marginTop="200dip"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/popular_recycler_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:layout_below="@+id/popular_textview"/>
</RelativeLayout>
但这只是第一个 RecyclerView 工作。第二个是不可见的。
我的Fragment 代码是这样的:
private void populatRecyclerView(ArrayList<HashMap<String, String>> imageList) {
ArrayList<Data_Model> arrayList = new ArrayList<>();
for (int i = 0; i < imageList.size(); i++) {
HashMap<String, String> vector = imageList.get(i);
category = vector.get("deal_category");
if (category.equalsIgnoreCase("17")) {
String imageurl = vector.get("deal_thumbnail");
String title = vector.get("title");
Uri uri = Uri.parse(imageurl);
String filename = uri.getLastPathSegment();
arrayList.add(new Data_Model(title, filename));
RecyclerView_Adapter adapter = new RecyclerView_Adapter(getActivity(), arrayList);
recyclerView.setAdapter(adapter);// first set adapter on recyclerview
adapter.notifyDataSetChanged();// Notify the adapter
} else {
String imageurl = vector.get("deal_thumbnail");
String title = vector.get("title");
Uri uri = Uri.parse(imageurl);
String filename = uri.getLastPathSegment();
arrayList.add(new Data_Model(title, filename));
RecyclerView_Popular_Adapter popular_adapter = new RecyclerView_Popular_Adapter(getActivity(), arrayList);
popular_recyclerView.setAdapter(popular_adapter);// second set adapter on recyclerview
popular_adapter.notifyDataSetChanged();// Notify the adapter
}
}
}
甚至第二个TextView 也没有出现。我在评论中指定了第一个和第二个适配器。那么如何让第二个RecyclerView 也可见呢?
【问题讨论】:
-
由于您已指定条件,因此在单个实例中只有一个可见(这意味着您的 if(true) 始终)
-
在某些情况下它会进入 else 循环。我已经交叉检查了那个。
-
尝试修复 Recyclerview 的高度。如果您有条件地填充 recyclerview 为什么需要其中两个?
-
第一个是自动滚动条,第二个不是自动滚动条。所以我需要两个不同的回收视图
标签: android android-recyclerview fragment android-adapter