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像一个适配器。