android - 如何在带有 fragment 的 Activity 中显示 RecyclerView 和 CardView 布局?

标签 android android-fragments android-activity android-recyclerview android-fragmentactivity

这是我的activity_main.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"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Mucitler"
            android:textColor="#000"
            android:textSize="16sp"/>

    </android.support.v7.widget.Toolbar>

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar">

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

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


</RelativeLayout>

这是我的 custom_recycler_view_row.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card:cardBackgroundColor="#ffffff"
    card:cardCornerRadius="5dp"
    card:cardElevation="2dp"
    card:cardUseCompatPadding="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <ImageView
                android:id="@+id/ivImageOfCity"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="centerCrop"
                android:src="@drawable/android_image"/>

            <TextView
                android:id="@+id/tvNameOfCity"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:background="#55000000"
                android:paddingBottom="20dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="20dp"
                android:singleLine="true"
                android:text="Sehir Adı"
                android:textColor="#fff"
                android:textSize="18sp"/>

        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageFrame"
            android:orientation="vertical">

            <TextView
                android:id="@+id/tvCountryOfCity"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="Austalia"/>

            <TextView
                android:id="@+id/tvPopulationOfCity"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="1 M population"/>

        </LinearLayout>

    </RelativeLayout>

</android.support.v7.widget.CardView>

如何在带有 fragment 的 Activity 中显示 RecyclerView 和 CardView 布局?我还有 MainActivity 和 City Model 代码。请帮助我,谢谢。

最佳答案

首先你必须在你的 fragment Activity 中初始化recyclerview 要显示 cardview,您必须创建 RecyclerViewAdapter 类,并且在该类中,您必须将要显示的数据放入 cardview 中。 之后只需在 fragment 中调用 RecyclerViewAdapter。

关于android - 如何在带有 fragment 的 Activity 中显示 RecyclerView 和 CardView 布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33199401/

相关文章:

android - 如何获取来电者详细信息 - 小区 ID?

安卓多点触控!破解任何人?

android - 为什么这个 fragment 的字段为空?

android - 异常 java.lang.IllegalStateException : Can not perform this action after onSaveInstanceState

java - 如何添加列表 Activity 而不是 fragment ?

android - 如何在前台(顶部) Activity (应用程序)更改时收到通知

android ImageButton缩放并保持纵横比

android - p4a 在 Android SDK 中找不到 `android` 或 `sdkmanager` 二进制文件。退出

android - 更改首选项 Activity 的外观

android - 以编程方式创建和显示 ProgressBar