如何在扩展片段中添加RecyclerView以显示列表

时间:2017-01-04 08:56:00

标签: android android-fragments android-recyclerview recycler-adapter

我在主要活动中扩展了3个片段。另外我如何添加recyclerview以在我的一个片段中显示一个列表。

请帮忙

2 个答案:

答案 0 :(得分:1)

别介意你是乞丐:)。首先看看片段是如何工作的。片段需要一个活动。在活动中,您必须在framelayout(Activity)中创建framelayout并加载或提交片段。

现在,如果您想要更改活动中的任何视图,您必须在片段中更改它,并在更改其视图的活动中调用相应的片段。

这意味着您必须在活动中添加recyclerview,但首先必须将包含recyclerview的片段添加到活动的framelayout。

答案 1 :(得分:0)

你正在寻找这样的东西:

注意:请务必将其添加到您的应用程序gradle:

compile 'com.android.support:recyclerview-v7:23.4.0'

你有fragment1.java,布局为fragment1.xml: 的 fragment1.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:layout_width="match_parent"
    android:layout_height="match_parent">
                <android.support.v7.widget.RecyclerView
                    android:id="@+id/fragment1_recycler_view"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:clickable="true">
                </android.support.v7.widget.RecyclerView>
</RelativeLayout>

在fragment1.java中初始化此RecyclerView,并写入适配器。 有关更多信息,请尝试以下链接: How to implement RecyclerView with CardView rows in a Fragment with TabLayout http://www.androidhive.info/2016/01/android-working-with-recycler-view/