【问题标题】:How to use Fragments and RecyclerView?如何使用 Fragments 和 RecyclerView?
【发布时间】:2018-09-25 14:46:19
【问题描述】:

我是一名学生,开始使用 Android Studio 编写我的课程作业。我对它比较陌生,并且一直在使用 YouTube 教程来提供帮助,其中没有太多解释。

我尝试将两者合并在一起,并且大部分都成功了,现在的问题是它告诉我需要将RecyclerView 合并到我的activitymain.xml 中,但我已经将它创建为导航查看该文件中的内容,因此我不确定该怎么做。

这两个不同的教程使用片段创建了一个导航菜单,还创建了一个卡片视图,我试图将其放入我的一个片段中。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/draw_menu" />


</android.support.v4.widget.DrawerLayout>

【问题讨论】:

    标签: javascript android xml


    【解决方案1】:

    首先你不需要导航菜单来构建recyclerview。

    制作recyclerview并填充数据有几个步骤。

    1 在您的布局(活动或片段)中使用

    <android.support.v7.widget.RecyclerView
                        android:id="@+id/recycles"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        />
    

    2 在您的(活动或片段)中,您需要设置一个数组和布局管理器,如下所示:

    arrayList = new ArrayList<>();
       binding.returnSelectedRecyler.setNestedScrollingEnabled(false);
        layoutManager = new LinearLayoutManager(getActivity());
        binding.returnSelectedRecyler.setHasFixedSize(true);
        binding.returnSelectedRecyler.setLayoutManager(layoutManager);
    

    您在 recyclerview 的每一行中显示的 3 个构建布局

    4 构建一个将所有东西连接在一起的适配器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多