RecyclerView和cardView的初次体验
2016-12-23 18:19
253 查看
第一 引入库依赖
25代表当前编译版本,根据需求改正
compile ‘com.android.support:design:25.0.1’
compile ‘com.android.support:cardview-v7:25.0.+’
布局xml使用
adapter的布局 list_item.xml
编写adapter
代码中使用
25代表当前编译版本,根据需求改正
compile ‘com.android.support:design:25.0.1’
compile ‘com.android.support:cardview-v7:25.0.+’
布局xml使用
<android.support.v7.widget.RecyclerView android:id="@+id/rv" android:layout_width="match_parent" android:layout_height="match_parent"/>
adapter的布局 list_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:padding="10dp"/> </android.support.v7.widget.CardView> </LinearLayout>
编写adapter
public class MyAdapter extends RecyclerView.Adapter<ViewHolder> { @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //最上面获取view,子布局显示不全 //View view = View.inflate(MainActivity.this, R.layout.list_item, null); //View view= LayoutInflater.from(MainActivity.this).inflate(R.layout.list_item,null); View view= LayoutInflater.from(MainActivity.this).inflate(R.layout.list_item,parent,false); ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.mTv.setText(alst.get(position)); } @Override public int getItemCount() { return alst.size(); } } public class ViewHolder extends RecyclerView.ViewHolder { private TextView mTv; public ViewHolder(View itemView) { super(itemView); mTv = (TextView) itemView.findViewById(R.id.tv); } }
代码中使用
public class MainActivity extends Activity { private ArrayList<String> alst; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); alst = new ArrayList<>(); alst.add("这两个包好像都是的,,"); alst.add("放我们日常用到的控件"); alst.add("但是有些是在view包中"); alst.add("有些是在widget包中"); alst.add("放我们日常用到的控件"); alst.add("有些是在widget包中"); alst.add("这两个包好像都是的"); RecyclerView mRv = (RecyclerView) findViewById(R.id.rv); mRv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)); mRv.setAdapter(new MyAdapter()); } }
相关文章推荐
- Material Design最佳体验(2): 使用RecyclerView、CardView、SwipeRefreshLayout实现下拉刷新列表
- RecyclerView 初次体验,实现增加删除条目及item长按和点击事件
- Android5.x 新控件之RecyclerView,CardView,Palette的使用
- Android L中的RecyclerView 、CardView 、Palette的使用
- Android RecyclerView 使用完全解析 体验艺术般的控件
- Android RecyclerView 使用完全解析 体验艺术般的控件
- Android RecyclerView 使用完全解析 体验艺术般的控件
- Android RecyclerView 使用完全解析 体验艺术般的控件
- Android L中的RecyclerView 、CardView 、Palette的使用
- Android 5.0 之 RecyclerView 和 CardView
- RecyclerView与CardView的使用
- RecyclerView和CardView的使用
- 【安卓笔记】CardView+RecyclerView使用示例
- Android RecyclerView与CardView的配合使用
- RecyclerView中CardView的展开与叠起
- Android5.x 新控件之RecyclerView,CardView,Palette的使用
- Material Design:利用RecyclerView CardView实现新闻卡片样式
- 一次项目优化的体验--RecyclerView
- 安卓高级3 RecyclerView 和cardView使用案例
- 使用RecyclerView CardView相关问题 解决The hierarchy of the type is inconsistent问题