【问题标题】:Can one use cardview for listview item and how可以将卡片视图用于列表视图项目以及如何使用
【发布时间】:2015-06-10 09:06:45
【问题描述】:

我想在我的应用程序中实现CardView,以便所有ListView 项目都是CardViews。是不是像在CardView中封装一个ListView item XML一样简单?

【问题讨论】:

  • 不,您必须使用 recyclerview 而不是 listview 并使用 cardview 添加自定义适配器。
  • 当然可以。 CardView 是一个 FrameLayout

标签: android android-cardview


【解决方案1】:

如果其他人遇到这个问题,其他答案是正确的,但是您应该将 CardView 放在 FrameLayout 中,并且应该使 ListView 的分隔符透明。 CardView 的高程和边距属性将不起作用,除非您在 FrameLayout 中使用它。

【讨论】:

  • 在文档中它只是说它扩展了 FrameLayout,希望他们能像你一样直接说出来。谢谢。
  • 很高兴我的回答对人们有所帮助。
【解决方案2】:

是的。在CardView 下方只是一个简单的FrameLayout,您可以将其扩展为ListView(或RecyclerView)。

这是一个例子:

<android.support.v7.widget.CardView
    android:id="@+id/ly_root"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FEFEFE"
    android:layout_margin="8dp"
    app:cardCornerRadius="4dp">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <ImageView
            android:id="@+id/iv_cover"
            android:layout_width="wrap_content"
            android:layout_height="160dp"
            android:scaleType="centerCrop"
            android:src="@drawable/placeholder"/>

        ...

    </LinearLayout>
</android.support.v7.widget.CardView>

下面是这个例子:

当然,您需要实现一个自定义适配器来将它们连接在一起。但这实际上与任何自定义 ListView 项目一样。没什么特别的。

【讨论】:

    【解决方案3】:

    CardView 和 RecyclerView 一起使用效果更好,这里有一个例子。

    • activity_main.xml(它包含recyclerview)

       <?xml version="1.0" encoding="utf-8"?>
       <LinearLayout 
       xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
           <android.support.v7.widget.RecyclerView
           android:id="@+id/recyclerview"
           android:layout_height="match_parent"
           android:layout_width="match_parent"/>
      </LinearLayout>
      
    • cardview.xml

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.v7.widget.CardView
      xmlns:card_view="http://schemas.android.com/apk/res-auto"
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_height="wrap_content"
      android:layout_width="match_parent"
      card_view:cardCornerRadius="4dp"
      android:layout_margin="10dp">
          <TextView
          android:id="@+id/text_cardview"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:padding="20dp" />
      </android.support.v7.widget.CardView>
      
    • buid.gradle(Module:app)

      dependencies {
          compile fileTree(dir: 'libs', include: ['*.jar'])
          testCompile 'junit:junit:4.12'
          compile 'com.android.support:appcompat-v7:23.3.0'
          compile 'com.android.support:cardview-v7:23.0.+'
          compile 'com.android.support:recyclerview-v7:23.0.+'
      }
      
    • RecyclerViewAdapter.java

      public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
          public ArrayList<String> myValues;
          public RecyclerViewAdapter (ArrayList<String> myValues){
              this.myValues= myValues;
          }
      
          @Override
          public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
              View listItem = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview.xml, parent, false);
              return new MyViewHolder(listItem);
          }
      
          @Override
          public void onBindViewHolder(MyViewHolder holder, int position) {
               holder.myTextView.setText(myValues.get(position));
          }
      
      
          @Override
          public int getItemCount() {
              return myValues.size();
          }
      
          public static class MyViewHolder extends RecyclerView.ViewHolder {
              private TextView myTextView;
              public MyViewHolder(View itemView) {
                  super(itemView);
              myTextView = (TextView)itemView.findViewById(R.id.text_cardview);
              }
          }
      }
      
    • MainActivity.java

      public class MainActivity extends AppCompatActivity {
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      
          ArrayList<String> myValues = new ArrayList<String>();
      
          //Populate the ArrayList with your own values
          myValues.add("KitKat");
          myValues.add("Lollipop");
          myValues.add("Marshmallow");
      
          RecyclerViewAdapter adapter = new RecyclerViewAdapter(myValues);
          RecyclerView myView =  (RecyclerView)findViewById(R.id.recyclerview);
          myView.setHasFixedSize(true);
          myView.setAdapter(adapter);
          LinearLayoutManager llm = new LinearLayoutManager(this);
          llm.setOrientation(LinearLayoutManager.VERTICAL);
          myView.setLayoutManager(llm);
       }     
       }
      

    更多详情请参考本教程:A Guide to Android RecyclerView and CardView

    【讨论】:

    • 感谢您的示例。我不得不从 RecyclerViewAdapter.java 中的“R.layout.cardview.xml”中删除“.xml”
    【解决方案4】:

    是的,您可以将CardView 用于 ListView 中的列表项。但是我建议你使用 RecyclerView 而不是 ListView,因为它是 ListView 的更新版本。使用 CardView 检查 this 的 RecyclerView。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      • 2019-08-31
      • 2018-12-01
      相关资源
      最近更新 更多