卡片视图布局Android片段

时间:2016-06-13 08:05:59

标签: android xml layout fragment android-cardview

我想在片段中实现卡片视图布局,如下所示: https://www.docdroid.net/VT1Fo56/esempio.pdf.html

这是我希望插入列表的片段代码:

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类中(如果不是,我会放置他们的代码)。

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>

2 个答案:

答案 0 :(得分: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" />

答案 1 :(得分:0)

尝试更改android:layout_height="wrap_content"中根目录LinearLayout上的card_view_layout.xml。和android:layout_height="wrap content"

中的RecyclerView