优点
加载不同的Item布局,我们的思路就是通过
getItemViewType(int position)
//这个方法来判断是加载那个布局
我们实现的就是一个头布局外加一个i普通的item布局,主要实现步有如下几步
依赖下面加上这句话,主要用来显示圆角图片
compile 'de.hdodenhof:circleimageview:2.0.0'
第一步:创建两个item布局
//头布局的代码,在头布局中,我使用了一个圆角图片的工具,在依赖下面写上这句话
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@color/colorAccent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/head_image"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="@mipmap/touxiang"
android:layout_marginTop="5dp"
android:layout_centerHorizontal="true"
app:civ_border_width="0dp"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_below="@+id/head_image"
android:layout_centerHorizontal="true"
android:text="空灵画师"
android:textColor="@color/white"
android:textSize="22sp"/>
RelativeLayout>
//普通的item布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:padding="16dp"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:id="@+id/tv_text"
android:text="好友分享"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"/>
<ImageView
android:id="@+id/iv_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/iconfont_xiayiye"
android:layout_alignParentRight="true"/>
RelativeLayout>
接下来的是重点部分Adapter的书写
public class MyRecycleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private LayoutInflater mLayoutInflater;
private Context context;
private String[] titles;
//建立枚举 2个item 类型
public enum ITEM_TYPE {
ITEM1,
ITEM2
}
public MyRecycleAdapter(Context context, String[] titles) {
this.titles = titles;
this.context = context;
mLayoutInflater = LayoutInflater.from(context);
}
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == ITEM_TYPE.ITEM1.ordinal()) {
return new Item1ViewHolder(mLayoutInflater.inflate(R.layout.recycle_head, parent, false));
} else {
return new Item2ViewHolder(mLayoutInflater.inflate(R.layout.item_text, parent, false));
}
}
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof Item1ViewHolder) {
((Item1ViewHolder) holder).mTextView.setText(titles[position]);
} else if (holder instanceof Item2ViewHolder) {
((Item2ViewHolder) holder).mTextView.setText(titles[position]);
}
}
public int getItemViewType(int position) {
switch (position) {
case 0:
return ITEM_TYPE.ITEM1.ordinal();
}
return ITEM_TYPE.ITEM2.ordinal();
}
public int getItemCount() {
return titles == null ? 0 : titles.length;
}
//头布局 的ViewHolder
public static class Item1ViewHolder extends RecyclerView.ViewHolder {
TextView mTextView;
public Item1ViewHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.tv_name);
}
}
//普通item 的ViewHolder
public static class Item2ViewHolder extends RecyclerView.ViewHolder {
TextView mTextView;
public Item2ViewHolder(View itemView) {
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.tv_text);
}
}
}
mainactivy中的书写
private RecyclerView mRecyclerView;
//item 显示所需(仅供DEMO)
private String[] title = {"空灵画师", "诸葛亮--运筹帷幄,决胜千里之外", "司马懿 --奸臣贼子", "马超--西凉三马二哥",
"赵子龙--当阳破七进七出", "姜维--一生为蜀国消尽全力", "法正--谋略的决策者", "张飞--猛汉张飞,粗中有细",
"关羽--义气的化身,吾辈敬之", "刘备--草鞋刘备三分天下", "曹操--宁我负天下人,不让天下人负我"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.act_recycleView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.setAdapter(new MyRecycleAdapter(this, title));
}
这几句代码比较重要
//这句代码设置内容显示为垂直显示
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
//将mRecyclerView与线性布局管理器关联起来
mRecyclerView.setLayoutManager(linearLayoutManager);