RecyClerView和CardView的使用
RecyClerView和CardView已经出来很久了,只是一直没有时间去研究,最近公司做的项目个人感觉ui实在太丑...屋里吐槽
代码实现,RecyClerView替代ListView CardView可以看成是一个布局
记得一定要导入相关的包,否则无法运行
主布局文件
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/recyclerView" android:scrollbars="vertical" android:layout_width="match_parent" android:layout_height="match_parent" />
list的布局文件 一定要加上 xmlns:card_view="http://schemas.android.com/apk/res-auto" 否则card_view的属性无法添加
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" card_view:cardBackgroundColor="@color/material_blue_grey_800" card_view:cardCornerRadius="5dp" card_view:cardPreventCornerOverlap="true" > <RelativeLayout android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="100dp" android:background="#f7f7f7" android:id="@+id/recyclerView_list_id"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/sc_action_left" android:layout_centerVertical="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="中间的文字" android:id="@+id/textView" android:layout_above="@+id/textView2" android:layout_toLeftOf="@+id/textView2" android:layout_toStartOf="@+id/textView2" android:layout_marginRight="57dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="后面的文字" android:id="@+id/textView2" android:layout_alignBottom="@+id/imageView" android:layout_alignParentRight="true" /> </RelativeLayout> </android.support.v7.widget.CardView>
Activity的代码
package com.example.administrator.degsin; import android.app.Activity; import android.os.Bundle; import android.os.SystemClock; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; /** * RecyclerView的使用,话说这个类可以替代ListView,还可以实现下拉刷新,感觉很强大有没有 */ public class MainActivity extends Activity { private RecyclerView recyclerView; private LinearLayoutManager linearLayoutManager; private RecyclerViewAdapter adapter; private List<User> mlist = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1,创建对象 recyclerView = (RecyclerView) findViewById(R.id.recyclerView); //将RecyclerView设置成横向(竖向也可以) //2,创建布局管理器 linearLayoutManager = new LinearLayoutManager(MainActivity.this); //3,设置布局管理器的组件方向 // linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);//水平方向 linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);//垂直方向 //4,设置RecyclerView的布局管理 recyclerView.setLayoutManager(linearLayoutManager); //设置原始数据 if (mlist == null) { mlist = new ArrayList<User>(); for (int i = 0; i < 20; i++) { User u = new User(); u.setT_date("" + SystemClock.elapsedRealtime()); u.setT_user("百合不是茶"); mlist.add(u); } } //5,设置数据源,使用RecyclerView的Adapter adapter = new RecyclerViewAdapter(); //设置数据源 recyclerView.setAdapter(adapter); } /** * RecyclerView的适配器类 * 快速导入相关的包快捷键 ctrl+o * 格式化形式优化代码 ctrl+alt+l */ class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> { //创建ViewHolder继承RecyclerView.ViewHolder类 public class ViewHolder extends RecyclerView.ViewHolder { private ImageView imageView; private TextView t1; private TextView t2; public ViewHolder(View view_list) { super(view_list); imageView = (ImageView) view_list.findViewById(R.id.imageView); t1 = (TextView) view_list.findViewById(R.id.textView); t2 = (TextView) view_list.findViewById(R.id.textView2); } } @Override public int getItemCount() { //判断是否为0 if (mlist.size() == 0) { return 0; } return mlist.size(); } @Override public void onBindViewHolder(RecyclerViewAdapter.ViewHolder viewHolder, int i) { User obj =mlist.get(i); viewHolder.t1.setText(obj.getT_date()); viewHolder.t2.setText(obj.getT_user()+i); } @Override public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { //获得布局对象,类似与ListView中的getView() View view_list = LayoutInflater.from(MainActivity.this).inflate(R.layout.recyclerview_list, null); ViewHolder viewHolder = new ViewHolder(view_list); return viewHolder; } public void add_list(User item, int position) { mlist.add(position, item); notifyItemInserted(position); } public void removes(User item) { int position = mlist.indexOf(item); mlist.remove(position); notifyItemRemoved(position); } } class User { private String t_date; private String t_user = "百合不是茶"; public String getT_date() { return t_date; } public String getT_user() { return t_user; } public void setT_date(String t_date) { this.t_date = t_date; } public void setT_user(String t_user) { this.t_user = t_user; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
相关推荐
lgzaaron 2017-04-25
jerny0 2015-08-16
magic00 2019-12-24
sfhong00 2020-06-05
sheikhdz 2020-05-18
chenjinlong 2020-05-05
安辉 2020-04-30
chenjinlong 2020-02-03
fengyeezju 2019-12-12
magic00 2019-10-21
whale 2019-06-30
DaLei 2019-06-30
mbcsdn 2019-06-28
yinbaoshiguang 2019-06-27