[英]Card View Layout Android in fragment
我想在一個片段中實現卡片視圖布局,例如: 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>
嘗試以此替換循環:
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" />
嘗試在card_view_layout.xml
的根LinearLayout
上更改android:layout_height="wrap_content"
。 和android:layout_height="wrap content"
在RecyclerView
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.