RecyclerView添加头布局和尾布局
2018-07-09 本文已影响89人
FlyClound
1,添加一个头布局
效果图- 1,Xml布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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/rv_linear_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"/>
</android.support.constraint.ConstraintLayout>
- 2,Activity
public class RvLinearActivity2 extends AppCompatActivity {
private RecyclerView rvlinearlist;
private ArrayList<String> mData;
private RvLinearAdapter2 mAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linear);
this.rvlinearlist = (RecyclerView) findViewById(R.id.rv_linear_list);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//设置方向,默认方向(垂直)
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rvlinearlist.setLayoutManager(linearLayoutManager);
mData = new ArrayList<>();
for (int i = 0; i < 20; i++) {
mData.add("频道"+i);
}
//直接在adapter里面加上了头布局
mAdapter = new RvLinearAdapter2(this, mData);
rvlinearlist.setAdapter(mAdapter);
//自定义分割线,顶部有线
rvlinearlist.addItemDecoration(new ListViewHeadDecoration
(this,R.drawable.listview_decoration));
}
}
- 3,Adapter
public class RvLinearAdapter2 extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private final LayoutInflater mLayoutInflater;
private Context mContext;
private List<String>mData;
//Type
private int TYPE_HEADER = 1001;
public RvLinearAdapter2(Context context, List<String> data) {
mContext = context;
mData = data;
mLayoutInflater = LayoutInflater.from(context);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType==TYPE_HEADER){
View headerView = mLayoutInflater.inflate(R.layout.header_rv_linear, parent, false);
return new HeaderViewHolder(headerView);
}
View inflate = mLayoutInflater.inflate(R.layout.item_rv_linear, parent, false);
return new ViewHolder(inflate);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ViewHolder){
//postion==0被头布局占据
((ViewHolder) holder).tvText.setText(mData.get(position-1));
((ViewHolder) holder).tvText.setOnClickListener(view ->
Toast.makeText(mContext, mData.get(position-1), Toast.LENGTH_SHORT).show() );
}
}
@Override
public int getItemCount() {
//加了头布局,多一个
return mData.size()+1;
}
@Override
public int getItemViewType(int position) {
//在第一个位置添加头
if (position==0){
return TYPE_HEADER;
}
return super.getItemViewType(position);
}
/**
* 头布局的viewholder
*/
class HeaderViewHolder extends RecyclerView.ViewHolder {
public HeaderViewHolder(View itemView) {
super(itemView);
}
}
/**
* 正常内容的viewholder
*/
class ViewHolder extends RecyclerView.ViewHolder {
TextView tvText;
public ViewHolder(View itemView) {
super(itemView);
tvText=itemView.findViewById(R.id.tv_item_rv_linear_text);
}
}
}
2,添加一个头布局,一个尾布局
效果图其他都一样,除了Adapter不同.
public class RvHFAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final LayoutInflater mLayoutInflater;
private Context mContext;
private List<String>mData;
private final static int ITEM_HEADER=0;
private final static int ITEM_CONTENT=1;
private final static int ITEM_FOOT=2;
/**
* 头布局和尾布局的数量
*/
private int mHeader=1;
private int mFoot=1;
public RvHFAdapter(Context context, List<String> data) {
mContext = context;
mData = data;
mLayoutInflater = LayoutInflater.from(context);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType==ITEM_CONTENT){
View view = mLayoutInflater.inflate(R.layout.item_rv_linear, parent, false);
return new ViewHolder(view);
}
if (viewType==ITEM_HEADER){
View view = mLayoutInflater.inflate(R.layout.header_rv_linear, parent, false);
return new HeaderViewHolder(view);
}
if (viewType==ITEM_FOOT){
View view = mLayoutInflater.inflate(R.layout.footer_rv_linear, parent, false);
return new FooterViewHolder(view);
}
return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
//Log.e("MMM", "onBindViewHolder: "+position);
if (holder instanceof ViewHolder){
((ViewHolder) holder).tvText.setText(mData.get(position-mHeader));
((ViewHolder) holder).tvText.setOnClickListener(view ->
Toast.makeText(mContext, mData.get(position-mHeader), Toast.LENGTH_SHORT).show());
}
}
@Override
public int getItemCount() {
//固定数目,一个头,一个尾
return mData.size()+mHeader+mFoot;
}
@Override
public int getItemViewType(int position) {
if (mHeader!=0&&position<mHeader){
return ITEM_HEADER;
}
if (mFoot!=0&&position>=mData.size()+mHeader){
return ITEM_FOOT;
}
return ITEM_CONTENT;
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView tvText;
public ViewHolder(View itemView) {
super(itemView);
tvText=itemView.findViewById(R.id.tv_item_rv_linear_text);
}
}
class HeaderViewHolder extends RecyclerView.ViewHolder {
public HeaderViewHolder(View itemView) {
super(itemView);
}
}
class FooterViewHolder extends RecyclerView.ViewHolder {
public FooterViewHolder(View itemView) {
super(itemView);
}
}
}
3,网格布局添加头布局和尾布局
image.pngxml布局相同
- 1,Activity
public class GridActivity extends AppCompatActivity {
private android.support.v7.widget.RecyclerView rvlinearlist;
private ArrayList<String> mData;
private GridAdapter mGridAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linear);
this.rvlinearlist = (RecyclerView) findViewById(R.id.rv_linear_list);
initData();
initAdapter();
}
private void initAdapter() {
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
rvlinearlist.setLayoutManager(gridLayoutManager);
mGridAdapter = new GridAdapter(this, mData);
//rvlinearlist.setAdapter(mGridAdapter);
//使用添加头和尾的适配器
RvHFAdapter rvHFAdapter = new RvHFAdapter(this, mData);
rvlinearlist.setAdapter(rvHFAdapter);
//处理头和尾布局,使其占据一行
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
//判断是否是头或者尾布局
if (position==0||position==mData.size()+1){
return gridLayoutManager.getSpanCount();
}
return 1;
}
});
//添加分割线
//rvlinearlist.addItemDecoration(new Divider2(this));
}
private void initData() {
mData = new ArrayList<>();
for (int i = 0; i < 10; i++) {
mData.add("频道"+i);
}
}
}
- 2,Adapter
public class RvHFAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final LayoutInflater mLayoutInflater;
private Context mContext;
private List<String>mData;
private final static int ITEM_HEADER=0;
private final static int ITEM_CONTENT=1;
private final static int ITEM_FOOT=2;
/**
* 头布局和尾布局的数量
*/
private int mHeader=1;
private int mFoot=1;
public RvHFAdapter(Context context, List<String> data) {
mContext = context;
mData = data;
mLayoutInflater = LayoutInflater.from(context);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType==ITEM_CONTENT){
View view = mLayoutInflater.inflate(R.layout.item_rv_linear, parent, false);
return new ViewHolder(view);
}
if (viewType==ITEM_HEADER){
View view = mLayoutInflater.inflate(R.layout.header_rv_linear, parent, false);
return new HeaderViewHolder(view);
}
if (viewType==ITEM_FOOT){
View view = mLayoutInflater.inflate(R.layout.footer_rv_linear, parent, false);
return new FooterViewHolder(view);
}
return null;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
//Log.e("MMM", "onBindViewHolder: "+position);
if (holder instanceof ViewHolder){
((ViewHolder) holder).tvText.setText(mData.get(position-mHeader));
((ViewHolder) holder).tvText.setOnClickListener(view ->
Toast.makeText(mContext, mData.get(position-mHeader), Toast.LENGTH_SHORT).show());
}
}
@Override
public int getItemCount() {
//固定数目,一个头,一个尾
return mData.size()+mHeader+mFoot;
}
@Override
public int getItemViewType(int position) {
if (mHeader!=0&&position<mHeader){
return ITEM_HEADER;
}
if (mFoot!=0&&position>=mData.size()+mHeader){
return ITEM_FOOT;
}
return ITEM_CONTENT;
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView tvText;
public ViewHolder(View itemView) {
super(itemView);
tvText=itemView.findViewById(R.id.tv_item_rv_linear_text);
}
}
class HeaderViewHolder extends RecyclerView.ViewHolder {
public HeaderViewHolder(View itemView) {
super(itemView);
}
}
class FooterViewHolder extends RecyclerView.ViewHolder {
public FooterViewHolder(View itemView) {
super(itemView);
}
}
}