RecyclerView是android-support-v7-21版本中新增的一个Widget,官方介绍RecyclerView 是 ListView 的升级版本,更加先进和灵活。
开发环境
- IDE: ADT
- SDK: Android L
首先在Android SDK Manager上下载Android Support Repository,
它会在sdk/extras/android目录下生成m2repository目录,
进入RecycleView目录。用解压文件打开recyclerview-v7-21.0.0-rc1.aar文件,将claaes.jar改名后放入lib文件夹。
如果想在ADT中关联源代码,可以在libs下新建文件
recycleview.jar.properties
编辑
src=D:\\adt-bundle-windows-x86_64-20131030\\sdk\\extras\\android\\m2repository\\com\\android\\support\\recyclerview-v7\\21.0.0-rc1\\recyclerview-v7-21.0.0-rc1-sources.jar
右键项目close project,然后open project这样就可以关联源代码了。
创建项目如图
布局文件中使用 RecyclerView
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
package com.leigo.recyclerview; import android.app.Activity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; /** * @ClassName RecyclerViewActivity * @Description TODO * @author GaoLei * @date 2014-7-2 */ public class RecyclerViewActivity extends Activity { private static final String TAG = RecyclerViewActivity.class .getSimpleName(); private RecyclerView mRecyclerView; /** * @Method: onCreate * @Description: TODO * @param savedInstanceState * @see android.app.Activity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recycle_view); mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView); LinearLayoutManager layoutManager = new LinearLayoutManager(this); // layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); // 设置布局管理器 mRecyclerView.setLayoutManager(layoutManager); String[] dataset = new String[100]; for (int i = 0; i < dataset.length; i++) { dataset[i] = "item" + i; } RecyclerAdapter mAdapter = new RecyclerAdapter(dataset); mRecyclerView.setAdapter(mAdapter); } }<strong> </strong>
Adapter
package com.leigo.recyclerview; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView.Adapter; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * @ClassName RecyclerAdapter * @Description TODO * @author GaoLei * @date 2014-7-2 */ public class RecyclerAdapter extends Adapter<RecyclerAdapter.ViewHolder> { private String[] mDataset; /** * @Description: TODO */ public RecyclerAdapter(String[] dataset) { mDataset = dataset; } public static class ViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; /** * @Description: TODO * @param itemView */ public ViewHolder(View itemView) { super(itemView); mTextView = (TextView) itemView; } } /** * @Method: getItemCount * @Description: TODO * @return * @see android.support.v7.widget.RecyclerView.Adapter#getItemCount() */ @Override public int getItemCount() { return mDataset.length; } /** * @Method: onBindViewHolder * @Description: TODO * @param holder * @param position * @see android.support.v7.widget.RecyclerView.Adapter#onBindViewHolder(android.support.v7.widget.RecyclerView.ViewHolder, * int) */ @Override public void onBindViewHolder(ViewHolder holder, int position) { holder.mTextView.setText(mDataset[position]); } /** * @Method: onCreateViewHolder * @Description: TODO * @param parent * @param viewType * @return * @see android.support.v7.widget.RecyclerView.Adapter#onCreateViewHolder(android.view.ViewGroup, * int) */ @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(parent.getContext(), android.R.layout.simple_list_item_1, null); ViewHolder holder = new ViewHolder(view); return holder; } }
截图
Android学习之RecyclerView,布布扣,bubuko.com
原文:http://blog.csdn.net/le_go/article/details/36628161