RecyclerView(替代ListView)使用方法介绍

在build.gradle文件加入以下代码

 compile 'com.android.support:cardview-v7:21.0.3'
compile 'com.android.support:recyclerview-v7:21.0.3'

item类型同样的情况

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin"> <android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>

页面代码

public class OneActivity extends Activity {
private RecyclerView mRecyclerView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one); mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));//这里用线性显示 相似于listview
// mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));//这里用线性宫格显示 相似于grid view
// mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL));//这里用线性宫格显示 相似于瀑布流
mRecyclerView.setAdapter(new RecyclerViewAdapter(this)); } //RecyclerView适配器
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private LayoutInflater mLayoutInflater;
private String[] mCityName; public RecyclerViewAdapter(Context mContext) {
this.mContext = mContext;
mLayoutInflater = LayoutInflater.from(mContext);
mCityName = mContext.getResources().getStringArray(R.array.city_name);
} //创建新ViewHolder,被LayoutManager所调用
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new RecyclerViewViewHolder(mLayoutInflater.inflate(R.layout.item_text, parent, false));
} //将数据与界面进行绑定的操作
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((RecyclerViewViewHolder) holder).mTextView.setText(mCityName[position]);
} //获取数据的数量
@Override
public int getItemCount() {
return mCityName == null ? 0 : mCityName.length;
} //自己定义的ViewHolder,持有每一个Item的的全部界面元素
public class RecyclerViewViewHolder extends RecyclerView.ViewHolder {
TextView mTextView; public RecyclerViewViewHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.tv_city);
mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToastUtil.show(mContext, getPosition() + "");
}
});
}
}
}
}

效果图:

RecyclerView(替代ListView)使用方法介绍

item类型不同的情况

设配器代码

//RecyclerView适配器
public class MultipleRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private Context mContext;
private LayoutInflater mLayoutInflater; public static enum ITEM_TYPE {
ITEM_TYPE_IMAGE, ITEM_TYPE_TEXT
} public MultipleRecyclerViewAdapter(Context mContext) {
this.mContext = mContext;
mLayoutInflater = LayoutInflater.from(mContext);
} //创建新ViewHolder。被LayoutManager所调用
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == ITEM_TYPE.ITEM_TYPE_TEXT.ordinal()) {
return new TextViewViewHolder(mLayoutInflater.inflate(R.layout.item_text, parent, false));
} else {
return new ImageViewViewHolder(mLayoutInflater.inflate(R.layout.item_image, parent, false));
}
} //将数据与界面进行绑定的操作
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
} //获取数据的数量
@Override
public int getItemCount() {
return 10;
} //Item类型
@Override
public int getItemViewType(int position) {
return position % 2 == 0 ? ITEM_TYPE.ITEM_TYPE_TEXT.ordinal() : ITEM_TYPE.ITEM_TYPE_IMAGE.ordinal();
} //ItemHolder类型一
public class TextViewViewHolder extends RecyclerView.ViewHolder {
TextView mTextView; public TextViewViewHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.tv_city);
}
} //ItemHolder类型二
public class ImageViewViewHolder extends RecyclerView.ViewHolder {
ImageView mImageView; public ImageViewViewHolder(View itemView) {
super(itemView);
mImageView = (ImageView) itemView.findViewById(R.id.iv_city);
}
} }

效果图:

RecyclerView(替代ListView)使用方法介绍

上一篇:在电子邮件主题[PHP]中包含html标签


下一篇:C语言格式化输入不定长数组