使用 Recyclerview 创建卡片视图
creating Cardviews with Recyclerview
我正在使用 Recyclerview 创建 Cardview,就像每个教程所说的那样,在 Stackerflow 和 Youtube 中。它对我有用,但是当我 运行 这个应用程序时,它只显示一个包含全部数据的 Cardview。
Activity 主要:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/reciclador"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dp"
android:scrollbars="vertical" />
布局卡片:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imgRestaurant"
android:layout_width="400dp"
android:layout_height="100dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
card_view:srcCompat="@drawable/soporte_it" />
<TextView
android:id="@+id/lblNombre"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/imgRestaurant"
android:layout_marginTop="10dp"
android:text="Restaurant Soporte" />
<TextView
android:id="@+id/lblDescripcion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/lblNombre"
android:layout_marginTop="22dp"
android:text="TextView" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Class 数据 (clsRestaurants):
package com.soft.kukito.cardviewprueba;
/**
* Created by Hernan on 16/7/2017.
*/
public class clsRestaurants {
private int imagen_r;
private String nombre_r;
private String descripcion_r;
public clsRestaurants(int imagen_r, String nombre_r, String descripcion_r) {
this.imagen_r = imagen_r;
this.nombre_r = nombre_r;
this.descripcion_r = descripcion_r;
}
public int getImagen_r() {
return imagen_r;
}
public String getNombre_r() {
return nombre_r;
}
public String getDescripcion_r() {
return descripcion_r;
}
}
适配器(餐厅适配器):
package com.soft.kukito.cardviewprueba;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import java.lang.reflect.Array;
import java.util.ArrayList;
import static android.os.Build.VERSION_CODES.M;
/**
* Created by Hernan on 16/7/2017.
*/
public class restaurantsAdapter extends RecyclerView.Adapter<restaurantsAdapter.restaurantsViewHolder>
{
private ArrayList<clsRestaurants> restaurant_item;
public restaurantsAdapter(ArrayList<clsRestaurants> restaurant_item) {
this.restaurant_item = restaurant_item;
}
@Override
public restaurantsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_cards,viewGroup,false);
restaurantsViewHolder restaurants = new restaurantsViewHolder(v);
return restaurants;
}
@Override
public void onBindViewHolder(restaurantsViewHolder restaurantsViewHolder, int i) {
restaurantsViewHolder.nombre.setText(restaurant_item.get(i).getNombre_r());
restaurantsViewHolder.descripcion.setText(restaurant_item.get(i).getDescripcion_r());
restaurantsViewHolder.imagen.setImageResource(restaurant_item.get(i).getImagen_r());
}
@Override
public int getItemCount() {
return restaurant_item.size();
}
public class restaurantsViewHolder extends RecyclerView.ViewHolder{
TextView nombre,descripcion;
ImageView imagen;
public restaurantsViewHolder(View itemView) {
super(itemView);
nombre=(TextView)itemView.findViewById(R.id.lblNombre);
descripcion=(TextView) itemView.findViewById(R.id.lblDescripcion);
imagen=(ImageView)itemView.findViewById(R.id.imgRestaurant);
}
}
}
这让我头疼了几个小时,谢谢大家的回答。
我得到了什么:
预期结果:
所以基本上recyclerview默认是不自带分隔线的。您应该添加分隔线。
https://developer.android.com/reference/android/support/v7/widget/DividerItemDecoration.html
这会让您了解如何使用分隔线。
此外 this 会让您清楚地了解如何使用 RecyclerView 和 CardView
看到每个角上的小灰色 space 了吗?
我怀疑您可以在 CardView
中添加边距,添加这一行。
android:layout_marginBottom="10dp"
所以看起来像这样
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
android:layout_marginBottom="10dp">
在您的布局卡片中,您当然可以轻松地对左右边距执行相同的操作,使其看起来像您预期的结果。
将此文件添加到您的 build.gradle 文件中
compile 'com.android.support:cardview-v7:23.4.0'
并在回收器适配器的 xml 文件中使用下面的代码(您在其中为行项目编写代码)
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true">
</android.support.v7.widget.CardView>
使用这个标签作为根标签...完成!!!
我正在使用 Recyclerview 创建 Cardview,就像每个教程所说的那样,在 Stackerflow 和 Youtube 中。它对我有用,但是当我 运行 这个应用程序时,它只显示一个包含全部数据的 Cardview。
Activity 主要:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/reciclador"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dp"
android:scrollbars="vertical" />
布局卡片:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imgRestaurant"
android:layout_width="400dp"
android:layout_height="100dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
card_view:srcCompat="@drawable/soporte_it" />
<TextView
android:id="@+id/lblNombre"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/imgRestaurant"
android:layout_marginTop="10dp"
android:text="Restaurant Soporte" />
<TextView
android:id="@+id/lblDescripcion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/lblNombre"
android:layout_marginTop="22dp"
android:text="TextView" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Class 数据 (clsRestaurants):
package com.soft.kukito.cardviewprueba;
/**
* Created by Hernan on 16/7/2017.
*/
public class clsRestaurants {
private int imagen_r;
private String nombre_r;
private String descripcion_r;
public clsRestaurants(int imagen_r, String nombre_r, String descripcion_r) {
this.imagen_r = imagen_r;
this.nombre_r = nombre_r;
this.descripcion_r = descripcion_r;
}
public int getImagen_r() {
return imagen_r;
}
public String getNombre_r() {
return nombre_r;
}
public String getDescripcion_r() {
return descripcion_r;
}
}
适配器(餐厅适配器):
package com.soft.kukito.cardviewprueba;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import java.lang.reflect.Array;
import java.util.ArrayList;
import static android.os.Build.VERSION_CODES.M;
/**
* Created by Hernan on 16/7/2017.
*/
public class restaurantsAdapter extends RecyclerView.Adapter<restaurantsAdapter.restaurantsViewHolder>
{
private ArrayList<clsRestaurants> restaurant_item;
public restaurantsAdapter(ArrayList<clsRestaurants> restaurant_item) {
this.restaurant_item = restaurant_item;
}
@Override
public restaurantsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_cards,viewGroup,false);
restaurantsViewHolder restaurants = new restaurantsViewHolder(v);
return restaurants;
}
@Override
public void onBindViewHolder(restaurantsViewHolder restaurantsViewHolder, int i) {
restaurantsViewHolder.nombre.setText(restaurant_item.get(i).getNombre_r());
restaurantsViewHolder.descripcion.setText(restaurant_item.get(i).getDescripcion_r());
restaurantsViewHolder.imagen.setImageResource(restaurant_item.get(i).getImagen_r());
}
@Override
public int getItemCount() {
return restaurant_item.size();
}
public class restaurantsViewHolder extends RecyclerView.ViewHolder{
TextView nombre,descripcion;
ImageView imagen;
public restaurantsViewHolder(View itemView) {
super(itemView);
nombre=(TextView)itemView.findViewById(R.id.lblNombre);
descripcion=(TextView) itemView.findViewById(R.id.lblDescripcion);
imagen=(ImageView)itemView.findViewById(R.id.imgRestaurant);
}
}
}
这让我头疼了几个小时,谢谢大家的回答。
我得到了什么:
预期结果:
所以基本上recyclerview默认是不自带分隔线的。您应该添加分隔线。
https://developer.android.com/reference/android/support/v7/widget/DividerItemDecoration.html
这会让您了解如何使用分隔线。
此外 this 会让您清楚地了解如何使用 RecyclerView 和 CardView
看到每个角上的小灰色 space 了吗?
我怀疑您可以在 CardView
中添加边距,添加这一行。
android:layout_marginBottom="10dp"
所以看起来像这样
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
android:layout_marginBottom="10dp">
在您的布局卡片中,您当然可以轻松地对左右边距执行相同的操作,使其看起来像您预期的结果。
将此文件添加到您的 build.gradle 文件中
compile 'com.android.support:cardview-v7:23.4.0'
并在回收器适配器的 xml 文件中使用下面的代码(您在其中为行项目编写代码)
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardBackgroundColor="@android:color/white"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true">
</android.support.v7.widget.CardView>
使用这个标签作为根标签...完成!!!