码迷,mamicode.com
首页 > 其他好文 > 详细

RecyclerView和CardView

时间:2017-04-12 02:47:36      阅读:353      评论:0      收藏:0      [点我收藏+]

标签:import   void   extends   emc   automatic   ast   arraylist   android   scroll   

相对其他View,这个比较复杂

1.新建一个layout文件item_list_content.xml来存放CardView

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="wrap_content"
 4     android:layout_height="wrap_content"
 5     android:orientation="horizontal">
 6 
 7     <android.support.v7.widget.CardView
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content">
10 
11         <LinearLayout
12             android:padding="2dp"
13             android:layout_width="match_parent"
14             android:layout_height="wrap_content"
15             android:orientation="vertical">
16 
17             <TextView
18                 android:id="@+id/textViewFirst"
19                 android:layout_width="match_parent"
20                 android:layout_height="wrap_content"
21                 android:layout_margin="@dimen/text_margin"
22                 android:textAppearance="?attr/textAppearanceListItem" />
23 
24             <TextView
25                 android:id="@+id/textViewLast"
26                 android:layout_width="match_parent"
27                 android:layout_height="wrap_content"
28                 android:layout_margin="@dimen/text_margin"
29                 android:textAppearance="?attr/textAppearanceListItem" />
30 
31         </LinearLayout>
32 
33     </android.support.v7.widget.CardView>
34 
35 
36 </LinearLayout>

 

2.加入RecyclerView

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     tools:context="com.mazingtec.mazing.Hall">
 8 
 9     <android.support.design.widget.AppBarLayout
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content">
12 
13         <android.support.v7.widget.Toolbar
14             android:id="@+id/toolbar"
15             android:layout_width="match_parent"
16             app:navigationIcon="@mipmap/ic_menu_white_48dp"
17             app:titleTextAppearance="@style/TextAppearance.AppCompat.Large"
18             android:layout_height="?attr/actionBarSize"
19             android:background="@color/primary_dark_material_dark">
20 
21         <TextView
22             android:id="@+id/toolbar_title"
23             android:layout_width="wrap_content"
24             android:layout_height="wrap_content"
25             android:layout_gravity="center"
26             android:textAppearance="@style/TextAppearance.AppCompat.Large"
27             android:text="@string/main_title1"
28             android:textSize="20sp" />
29 
30         </android.support.v7.widget.Toolbar>
31 
32     </android.support.design.widget.AppBarLayout>
33 
34     <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
35         xmlns:app="http://schemas.android.com/apk/res-auto"
36         xmlns:tools="http://schemas.android.com/tools"
37         android:id="@+id/recyclerView"
38         android:layout_width="match_parent"
39         android:layout_height="wrap_content"
40         android:layout_marginLeft="16dp"
41         android:layout_marginRight="16dp"
42         app:layout_behavior="@string/appbar_scrolling_view_behavior"
43         app:layoutManager="LinearLayoutManager"
44         tools:listitem="@layout/item_list_content"/>
45 
46     <android.support.design.widget.BottomNavigationView
47         android:id="@+id/navigation"
48         android:layout_width="match_parent"
49         android:layout_height="wrap_content"
50         android:layout_gravity="bottom"
51         android:background="?android:attr/windowBackground"
52         app:menu="@menu/bottomnav_menu" />
53 
54 </android.support.design.widget.CoordinatorLayout>

 

3.建立一个新的Package用来存放所有的数据类,data,建立RecyclerView的数据类

 1 public class HallData {
 2     private String first;
 3     private String last;
 4 
 5     public HallData () {}
 6 
 7     public HallData(String first, String last) {
 8         this.first = first;
 9         this.last = last;
10     }
11 
12     public String getFirst() {
13 
14         return first;
15     }
16 
17     public String getLast() {
18 
19         return last;
20     }
21 
22 }

 

4.新建一个Package用于存放控制器,mAdapter,并新件一个class来控制RecyclerView,RvAdapter

 1 public class RvAdapter extends RecyclerView.Adapter<RvAdapter.ViewHolder> {
 2 
 3     private List<HallData> hallDatas;
 4     private Context context;
 5 
 6     public RvAdapter(List<HallData> hallDatas, Context context) {
 7         this.hallDatas = hallDatas;
 8         this.context = context;
 9     }
10 
11     @Override
12     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
13         View v = LayoutInflater.from(parent.getContext())
14                 .inflate(R.layout.item_list_content, parent, false);
15 
16         return new ViewHolder(v);
17     }
18 
19     @Override
20     public void onBindViewHolder(ViewHolder holder, int position) {
21         HallData hallData = hallDatas.get(position);
22 
23         holder.textViewFirst.setText(hallData.getFirst());
24         holder.textViewLast.setText(hallData.getLast());
25 
26     }
27 
28     @Override
29     public int getItemCount() {
30         return hallDatas.size();
31     }
32 
33     public  class  ViewHolder extends RecyclerView.ViewHolder {
34 
35         public TextView textViewFirst;
36         public TextView textViewLast;
37 
38         public ViewHolder(View itemView) {
39             super(itemView);
40 
41             textViewFirst = (TextView) itemView.findViewById(R.id.textViewFirst);
42             textViewLast = (TextView) itemView.findViewById(R.id.textViewLast);
43         }
44     }
45 }

 

5.最后就是代码实现了,做了一些假数据

  1 package com.mazingtec.mazing;
  2 
  3 import android.support.annotation.NonNull;
  4 import android.support.design.widget.BottomNavigationView;
  5 import android.support.v7.app.AppCompatActivity;
  6 import android.os.Bundle;
  7 import android.support.v7.widget.LinearLayoutManager;
  8 import android.support.v7.widget.RecyclerView;
  9 import android.support.v7.widget.Toolbar;
 10 import android.view.Menu;
 11 import android.view.MenuItem;
 12 import android.view.View;
 13 import android.widget.TextView;
 14 import android.widget.Toast;
 15 
 16 import com.mazingtec.mazing.data.HallData;
 17 import com.mazingtec.mazing.mAdapter.RvAdapter;
 18 
 19 import java.util.ArrayList;
 20 import java.util.List;
 21 
 22 public class Hall extends AppCompatActivity implements View.OnClickListener{
 23 
 24     private TextView toolbartitle;
 25 
 26     private RecyclerView recyclerView;
 27     private RecyclerView.Adapter adapter;
 28     private List<HallData> hallDatas;
 29 
 30     private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
 31             = new BottomNavigationView.OnNavigationItemSelectedListener() {
 32 
 33         @Override
 34         public boolean onNavigationItemSelected(@NonNull MenuItem item) {
 35             switch (item.getItemId()) {
 36                 case R.id.navigation_hunt:
 37                     toolbartitle.setText(R.string.main_title1);
 38                     return true;
 39 
 40                 case R.id.navigation_hall:
 41                     toolbartitle.setText(R.string.main_title2);
 42                     return true;
 43 
 44                 case R.id.navigation_treasury:
 45                     toolbartitle.setText(R.string.main_title3);
 46                     return true;
 47             }
 48             return false;
 49         }
 50 
 51     };
 52 
 53     @Override
 54     protected void onCreate(Bundle savedInstanceState) {
 55         super.onCreate(savedInstanceState);
 56         setContentView(R.layout.activity_hall);
 57 
 58         //Toolbar setup
 59         toolbartitle = (TextView) findViewById(R.id.toolbar_title);
 60         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 61         toolbar.setTitle("");
 62         setSupportActionBar(toolbar);
 63         getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 64         getSupportActionBar().setHomeButtonEnabled(true);
 65 
 66         //Add BottomNavigationView listener
 67         BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
 68         navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
 69 
 70         //Setup RecyclerView
 71         recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
 72         recyclerView.setHasFixedSize(true);
 73         recyclerView.setLayoutManager(new LinearLayoutManager(this));
 74         hallDatas = new ArrayList<>();
 75 
 76         for(int i = 0; i <= 10; i++) {
 77             HallData hallData = new HallData(
 78                     "first " + (i+1),
 79                     "Last"
 80             );
 81 
 82             hallDatas.add(hallData);
 83         }
 84 
 85         adapter = new RvAdapter(hallDatas, this);
 86         recyclerView.setAdapter(adapter);
 87     }
 88 
 89     @Override
 90     public void onClick(View v) {
 91 
 92     }
 93 
 94     @Override
 95     public boolean onCreateOptionsMenu(Menu menu) {
 96         // Inflate the menu; this adds items to the action bar if it is present.
 97         getMenuInflater().inflate(R.menu.toolbar_menu, menu);
 98         return true;
 99     }
100 
101     @Override
102     public boolean onOptionsItemSelected(MenuItem item) {
103         // Handle action bar item clicks here. The action bar will
104         // automatically handle clicks on the Home/Up button, so long
105         // as you specify a parent activity in AndroidManifest.xml.
106         int id = item.getItemId();
107 
108         //noinspection SimplifiableIfStatement
109         if (id == R.id.action_search) {
110             Toast.makeText(this, "搜索!", Toast.LENGTH_LONG).show();
111             return true;
112         }
113 
114         if (id == android.R.id.home) {
115             Toast.makeText(this, "哈哈!", Toast.LENGTH_LONG).show();
116             return true;
117         }
118 
119         return super.onOptionsItemSelected(item);
120     }
121 
122 }

 

RecyclerView和CardView

标签:import   void   extends   emc   automatic   ast   arraylist   android   scroll   

原文地址:http://www.cnblogs.com/mazing/p/6696812.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!