2015-06-10 16 views
10

Tôi muốn triển khai CardView trong ứng dụng của mình để tất cả các mục ListViewCardView s. Việc này có đơn giản như đóng gói một XML mục ListView trong CardView không?Có thể sử dụng chế độ xem trước cho mục danh sách xem và cách

+1

Không, bạn phải sử dụng recyclerview thay vì listview và thêm adapter tùy chỉnh với cardview. – Shvet

+2

tất nhiên bạn có thể. CardView là FrameLayout – Blackbelt

Trả lời

14

Có. Bên dưới CardView chỉ đơn giản là FrameLayout mà bạn chỉ có thể thổi phồng vào một ListView (hoặc RecyclerView).

Dưới đây là một ví dụ:

<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> 

Và đây là ví dụ mà sống trong hành động:

enter image description here

Tất nhiên bạn sẽ cần phải thực hiện một bộ chuyển đổi tùy chỉnh để dây chúng lên với nhau. Nhưng điều này là như với bất kỳ tùy chỉnh ListView mục thực sự. Không có gì đặc biệt về nó.

4

Có, bạn có thể sử dụng CardView cho mục danh sách trong ListView. Nhưng tôi đề nghị bạn sử dụng RecyclerView thay vì ListView, vì nó là phiên bản cập nhật của ListView. Kiểm tra this cho RecyclerView bằng CardView.

23

Trong trường hợp người khác bắt gặp câu hỏi này, các câu trả lời khác là đúng nhưng sau đó bạn nên đặt CardView trong FrameLayout và bạn nên đặt dải phân cách ListView trong suốt. CardView Thuộc tính độ cao và lề sẽ không hoạt động trừ khi bạn sử dụng nó bên trong FrameLayout.

+1

Đúng vậy. Cảm ơn bạn đã chỉ ra điều này. – user3144836

+0

Trong tài liệu, nó chỉ nói rằng nó mở rộng FrameLayout, muốn họ nói trực tiếp như bạn đã làm. Cảm ơn bạn. – Prof

+0

vui vì câu trả lời của tôi là giúp mọi người. –

6

Tốt hơn nên sử dụng CardView với RecyclerView, đây là một ví dụ.

  • activity_main.xml (nó chứa 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 (Học phần: ứng dụng)

    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); 
    }  
    } 
    

Tham khảo hướng dẫn này để biết thêm chi tiết: A Guide to Android RecyclerView and CardView

+0

Cảm ơn ví dụ. Tôi đã phải xóa ".xml" khỏi "R.layout.cardview.xml" trong RecyclerViewAdapter.java – Zach

Các vấn đề liên quan