我的應用程序一個CardView我需要創建一個類似的佈局:如何創建帶有可點擊行
我一直在想有一個RecyclerView
裏面CardView
。但是如果我這樣做,我必須創建兩個獨立的row.xml
(一個用於第一和第二行,另一個用於第三個和第四個)。我想知道是否有更簡單的方法來獲得相同的結果。
你能指出正確的方向嗎?
我的應用程序一個CardView我需要創建一個類似的佈局:如何創建帶有可點擊行
我一直在想有一個RecyclerView
裏面CardView
。但是如果我這樣做,我必須創建兩個獨立的row.xml
(一個用於第一和第二行,另一個用於第三個和第四個)。我想知道是否有更簡單的方法來獲得相同的結果。
你能指出正確的方向嗎?
使用相對佈局或線性佈局,並設置ID父佈局創建它
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="@+id/card_view_notification"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
card_view:cardCornerRadius="2dp"
card_view:contentPadding="7dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:id="@+id/rel_one"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Version"
android:id="@+id/hedone"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5.3(123)"
android:textColor="#000000"
android:layout_below="@+id/hedone"
android:textSize="14sp" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#7B7A7F"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:id="@+id/relative_two"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Version"
android:id="@+id/hedtwo"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Disponibile"
android:textColor="#000000"
android:layout_below="@+id/hedtwo"
android:textSize="14sp" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#7B7A7F"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:id="@+id/relative_three"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Your text"
android:textColor="#000000"
android:textSize="18sp" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.1dp"
android:background="#7B7A7F"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:id="@+id/relative_four"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Yourtext"
android:textColor="#000000"
android:textSize="18sp" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
輸出
我會建議製作多張卡片視圖並使用getItemViewType方法。您可以爲不同的佈局設置不同的視圖類型,並且可以在onCreateViewHolder方法中根據視圖類型爲視圖充氣。 How to create RecyclerView with multiple view type?
感謝您的回答。這絕對是一個有趣的選項 – Daniele
首先一個點在一個RecyclerView
有將CardView
不反轉。如果您想用RecyclerView
來完成,請按照以下步驟操作像: 步驟1: 創建名爲作爲具有像RecyclerViewActivity_main.xm
升佈局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ui.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recycler_view"
android:scrollbars="vertical"
></android.support.v7.widget.RecyclerView>
</RelativeLayout>
步驟2.創建佈局命名爲product_layout
其中具有CardView
像:
<?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="100dp">
<!--<TextView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:text="Products"-->
<!--android:gravity="center"/>-->
<!--<RelativeLayout-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content">-->
<!--<TextView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:gravity="center"-->
<!--android:textStyle="italic"-->
<!--android:text="Products"-->
<!--android:textAppearance="?android:textAppearanceLarge"/>-->
<!--</RelativeLayout>-->
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cardview">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/smartphone"
android:id="@+id/mobileimage"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SmartPhones"
android:layout_toRightOf="@+id/mobileimage"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:id="@+id/productname"
android:layout_alignParentTop="true"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
步驟3 。現在在MainActivity.java
中設置第一個RecyclerView佈局,如果你在使用json,然後在設置你的適配器之後解析它:
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
adapter = new ProductAdapter(list, getApplicationContext());
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
如果u要添加
RecyclerView
點擊然後設置onClickListener上RecyclerView在
Activity_main.java
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.dharmendras.productdetail.models.Products;
import com.example.dharmendras.productdetail.R;
import java.util.ArrayList;
public class ProductAdapter extends RecyclerView.Adapter <ProductAdapter.ProductsViewHolder> {
ArrayList<Products> products = new ArrayList<Products>();
private Context context;
public ProductAdapter(ArrayList<Products> products,Context context){
this.context = context;
this.products = products;
}
@Override
public ProductsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.product_layout,parent,false);
ProductsViewHolder productsViewHolder = new ProductsViewHolder(view);
return productsViewHolder;
}
@Override
public void onBindViewHolder(ProductsViewHolder holder, int position) {
Products pdt = products.get(position);
//Picasso.with(context).load(pdt.getImage_url()).resize(120, 60).into(holder.product_img);
//pdt.getProduct_name()
holder.product_name.setText(pdt.getProduct_name());
//Drawable myDrawable = context.getResources().getDrawable(pdt.getImage_url());
// int id = getContext().getResources().getIdentifier("imageName", "drawable", getContext().getPackageName());
// ImageView myImageView = (ImageView)findViewById(R.id.myImage);
//myImageView.setImageResource(R.drawable.icon);
// holder.product_img.setImageDrawable(pdt.getImage_url());
//imageView.setBackground(getResources().getDrawable(getResources().getIdentifier("name","id",getPackageName())));
String name = pdt.getImage_url();
int id = context.getResources().getIdentifier(name, "drawable",context.getPackageName());
Drawable drawable = context.getResources().getDrawable(id);
holder.product_img.setImageDrawable(drawable);
}
@Override
public int getItemCount() {
return products.size();
}
public static class ProductsViewHolder extends RecyclerView.ViewHolder{
ImageView product_img;
TextView product_name;
public ProductsViewHolder(View view){
super(view);
product_img = (ImageView) view.findViewById(R.id.mobileimage);
product_name = (TextView) view.findViewById(R.id.productname);
}
}
}
第5步:
步驟4.創建命名爲ProductAdapter.java
dafile像一個適配器。
將底部文本的可見性設置爲VISIBLE/GONE –
感謝您的評論。我想過那個,但是如何讓上面的文字垂直居中呢? – Daniele
@daniele如果你的底部textview消失了,父母的引力就像center_vertical一樣,它會自動中心對齊 – Payal