2017-07-17 36 views
-2

我使用Recyclerview創建Cardviews,例如每個教程在Stackerflow和Youtube中說。它適用於我,但是當我運行該應用程序時,它顯示了僅有一個Cardview與整個數據的內容。使用Recyclerview創建Cardviews

活動主:

<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> 

類數據(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; 
    } 
} 

適配器(restaurantAdapter):

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); 
     } 
    } 
} 

從幾個小時起就讓我頭疼,謝謝大家回答。

我得到什麼: Result and problem


預期的結果: Expected result

+0

您尚未添加適配器類,但已將Data類本身添加了兩次。添加適配器類,並最好是截圖 –

+0

我現在已經添加了一個圖像,並且適配器 – Natarr

+0

@Natarr'RecyclerView.Adapter'確實缺失了......雖然我懷疑,您可能打算使用LinearLayout「或者」RelativeLayout「(也可以嵌套這些,垂直和水平)...甚至可以通過簡單地在底部邊緣添加一個LinearLayout作爲」divider「來僞造以下建議的'DividerItemDecoration'。 –

回答

2

看到每個角落灰色的小空間? 我懷疑你可以爲你的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"> 
在佈局卡

,當然你也可以很容易地做同樣的左,右頁邊距,使其看起來像你預期的結果。

+0

它爲我工作!這就是我所需要的,非常感謝你! – Natarr

+0

您的歡迎。 @Natarr – Andy

0

在你的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> 

使用該標籤作爲根標籤...完成!

相關問題