2016-06-13 194 views
0

我想在一個片段來實現卡片視圖佈局,這樣的: https://www.docdroid.net/VT1Fo56/esempio.pdf.html卡片視圖佈局的Android在片段

這是片段的,其中我要插入列表的代碼:

public class Frag_Lista extends Fragment { 
    RecyclerView recyclerView; 
    RecyclerView.Adapter adapter; 
    RecyclerView.LayoutManager layoutManager; 
    ArrayList<Food_Card> list=new ArrayList<Food_Card>(); 
    String[] name=new String[50]; 
    String[] email=new String[50]; 
    String[] mobile=new String[50]; 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.frag_lista, container, false); 
     return(v); 
    } 
    @Override 
    public void onViewCreated (View view, Bundle savedIstanceState) { 

     //Azioni per generare pulsante menù 
     FloatingActionButton fab = (FloatingActionButton) getView().findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       /*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show();*/ 
       android.app.FragmentManager FM = getFragmentManager(); 
       FragmentTransaction FT = FM.beginTransaction(); 
       Frag_Nuova_Ricetta FL = new Frag_Nuova_Ricetta(); 
       FT.replace(R.id.posto_per_fragment,FL); 
       FT.addToBackStack(null); 
       FT.commit(); 
      } 
     }); 




     Context context = getActivity(); 

     name[0]="nome0"; 
     name[1]="nome1"; 
     name[2]="nome2"; 

     email[0]="email0"; 
     email[1]="email1"; 
     email[2]="email2"; 

     mobile[0]="mobile0"; 
     mobile[1]="mobile1"; 
     mobile[2]="mobile2"; 

     int count; 
     for(count=0;count<3;count++){ 
      Food_Card food_card=new Food_Card(0,name[count],email[count],mobile[count]); 
      list.add(food_card); 
     } 
     recyclerView = (RecyclerView) getView().findViewById(R.id.recyclerView); 
     layoutManager = new LinearLayoutManager(context); 
     recyclerView.setLayoutManager(layoutManager); 
     recyclerView.setHasFixedSize(true); 
     adapter=new food_adapter(list); 
     recyclerView.setAdapter(adapter); 

    } 

} 

做我獲得此: https://sendvid.com/2s2blbcz

我認爲這有什麼不對下面的XML的一個,而不是在我的Food_Card類或food_adapter類(如果不是,IW生病把他們的代碼)。

card_view_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <android.support.v7.widget.CardView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/card_view"> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <ImageView 
       android:layout_width="100dp" 
       android:layout_height="100dp" 
       android:id="@+id/food_image"/> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_toRightOf="@+id/food_image" 
       android:hint="edit1" 
       android:textSize="20dp" 
       android:textStyle="bold" 
       android:layout_marginLeft="20dp" 
       android:layout_marginTop="15dp" 
       android:layout_alignParentTop="true" 
       android:id="@+id/food_name"/> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/food_name" 
       android:layout_toRightOf="@id/food_image" 
       android:hint="edit2" 
       android:textSize="18dp" 
       android:textStyle="bold" 
       android:layout_marginLeft="20dp" 
       android:layout_marginTop="5dp" 
       android:id="@+id/food_email"/> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/food_email" 
       android:layout_toRightOf="@+id/food_image" 
       android:hint="edit3" 
       android:textSize="18dp" 
       android:textStyle="bold" 
       android:layout_marginLeft="20dp" 
       android:layout_marginTop="5dp" 
       android:id="@+id/food_mobile"/> 
     </RelativeLayout> 
    </android.support.v7.widget.CardView> 

</LinearLayout> 

frag_lista.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFFF00"> 

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


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

    <!--Pulsante menu--> 
    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_input_add" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true"/> 

</RelativeLayout> 

回答

0

嘗試用它來取代你的循環:

int count; 
for(count=0;count<3;count++) { 
    // set count as the id 
    Food_Card food_card = new Food_Card(count,name[count],email[count],mobile[count]); 
    list.add(food_card); 
} 

,並與您frag_lista.xml:

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

重複count ++是一個錯誤,但它不能解決這種情況。編輯問題。 – SctALE

0

嘗試更改android:layout_height="wrap_content"在您的根LinearLayout,card_view_layout.xml。和android:layout_height="wrap content"RecyclerView