【发布时间】:2016-07-12 09:06:10
【问题描述】:
我需要以编程方式将多个 RecyclerViews 添加到片段中。我已经设法使用 xml 布局(如下)包含一个 RecyclerView,它工作正常,但是,当我尝试以编程方式添加任何内容时,即使返回的 RecyclerViews 不为空,片段视图中也没有出现。因为我的数据源是 Web API 驱动的,所以我无法在 xml 布局中添加特定数量的 RecyclerView,因为所需的数量会不时改变,因此必须以编程方式完成。我尝试了许多不同的方法,但所有结果都是相同的,例如:没有一个 RecyclerView。我还需要在每个 RecyclerView 上方添加 TextViews 作为标题,我已经完成并且它们运行良好,但是从下面的代码中删除以使其更容易消化。为了完成我的项目,我需要做的就是添加多个 RecyclerViews。希望有人能帮忙?
片段:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
svv = new ScrollView(getActivity());
svv.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, ScrollView.LayoutParams.WRAP_CONTENT));
linLayout = new LinearLayout(getActivity());
linLayout.setOrientation(LinearLayout.VERTICAL);
linLayoutParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
View rootView = inflater.inflate(R.layout.fragment_root, container, false);
HorizontalScrollView svh;
RecyclerView itemsListing;
int top = 100;
for(int i = 0; i < itemCount; i++) {
String strSubURL = myListItem.myListUrls.get(i).toString();
sharedData.setCurrMyURL(String.valueOf(strSubURL));
// Below is where the problems start
// This works fine but only provides one recyclerview
//itemsListing = (RecyclerView) rootView.findViewById(R.id.items_listing);
// This does not work at all, showing zero recyclerviews even though the views are not null and are therefore actually created
itemsListing = new RecyclerView(inflater.getContext());
itemsListing.setPadding(0, top, 0, 0);
mLayoutManager = new LinearLayoutManager(itemsListing.getContext(), LinearLayoutManager.HORIZONTAL, false);
itemsListing.setLayoutManager(mLayoutManager);
mAdapter = new ItemsListingAdapter(mItems, this);
itemsListing.setAdapter(mAdapter);
svh = new HorizontalScrollView(getActivity());
svh.setPadding(0, top, 0, 0);
top=top+400;
}
svv.addView(linLayout);
RelativeLayout mainLayout = (RelativeLayout) rootView.findViewById(R.id.item_layout);
mainLayout.addView(svv);
return rootView;
}
XML 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#000000"
tools:context="com.myco.myapp.items.listing.ItemsListingListingFragment">
<android.support.v7.widget.RecyclerView
android:id="@+id/items_listing"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
【问题讨论】:
-
我不明白你为什么要这样做:这是一个解决方案,实现有限数量的 recyclerview,每个 recyclerview 行包含另一个 recyclerview。所以现在你可以显示尽可能多的 recyclerview 项目
-
嗨@suku,谢谢你的回复,虽然我一点也不明白!我有一个对集合进行分类的 Web API,每个集合都有一个标题。这些集合是动态的,它们的数量、其中的项目和标题会定期变化。在 iOS 中,我使用以编程方式创建的带有标题标签的集合视图,并且效果很好。在 Android 中,我还编写了静态 XML 回收器视图,其中集合不会更改并且它们再次正常工作。所以你是说我可以将程序化创建的回收器视图嵌套在一个 xml 声明的回收器视图中?
-
现在我明白你想要做什么了。看看我的答案
标签: android android-fragments android-studio android-recyclerview