0

我的應用程序一個CardView我需要創建一個類似的佈局:如何創建帶有可點擊行

layout

我一直在想有一個RecyclerView裏面CardView。但是如果我這樣做,我必須創建兩個獨立的row.xml(一個用於第一和第二行,另一個用於第三個和第四個)。我想知道是否有更簡單的方法來獲得相同的結果。

你能指出正確的方向嗎?

+0

將底部文本的可見性設置爲VISIBLE/GONE –

+0

感謝您的評論。我想過那個,但是如何讓上面的文字垂直居中呢? – Daniele

+0

@daniele如果你的底部textview消失了,父​​母的引力就像center_vertical一樣,它會自動中心對齊 – Payal

回答

2

使用相對佈局或線性佈局,並設置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> 

輸出

enter image description here

+0

謝謝。我正在檢查這個 – Daniele

+0

我通過修改你的代碼而成功了一點。我添加了ImageViews,這種方式我不需要使用RecyclerViews。如果您有一些時間,請添加ImageView以供將來參考。非常感謝 – Daniele

+0

很高興幫助你快樂編碼:) –

1

採取一個列表視圖,並最初使底部textview消失。在需要時使其可見。使用cardview不會幫助你創建這樣的佈局。

+0

我選擇使用CardView,因爲我想要有陰影的邊框。有沒有其他方法可以得到它? – Daniele

+0

檢查了這..這可能會幫助你與邊界http://stackoverflow.com/questions/24095223/android-linearlayout-add-border-with-shadow-around-a-linearlayout – Payal

+0

謝謝。我會檢查出 – Daniele

1

也許你可以創建一個單獨的row.xml並在必要時隱藏字幕。

mySubtitleTextView.setVisibility(View.GONE)像那個副標題不佔用任何空間和標題仍然居中。

+0

感謝您的答案。我可以做到這一點,但在前兩行標題不是垂直居中。當我將字幕設置爲「View.GONE」時,如何使其居中? – Daniele

+0

對於使用重心center_vertical的textview,您將使用父級,子級將始終居中居中。 – Cochi

1

我會建議製作多張卡片視圖並使用getItemViewType方法。您可以爲不同的佈局設置不同的視圖類型,並且可以在onCreateViewHolder方法中根據視圖類型爲視圖充氣。 How to create RecyclerView with multiple view type?

+0

感謝您的回答。這絕對是一個有趣的選項 – Daniele

1

首先一個點在一個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像一個適配器。