如何在带有片段的 activity 中显示 RecyclerView 和 CardView 布局?
How can I show RecyclerView and CardView layouts in an activity with a fragment?
这是我的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>
如何在带有片段的 activity 中显示 RecyclerView 和 CardView 布局?我还有 MainActivity 和 City Model 代码。请帮助我,谢谢。
首先你必须在片段中初始化 recyclerview activity
要显示 cardview,您必须创建 RecyclerViewAdapter class,并且在 class 中,您必须将要显示的数据放入 cardview。
之后只需在片段中调用 RecyclerViewAdapter。
这是我的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>
如何在带有片段的 activity 中显示 RecyclerView 和 CardView 布局?我还有 MainActivity 和 City Model 代码。请帮助我,谢谢。
首先你必须在片段中初始化 recyclerview activity 要显示 cardview,您必须创建 RecyclerViewAdapter class,并且在 class 中,您必须将要显示的数据放入 cardview。 之后只需在片段中调用 RecyclerViewAdapter。