Android Recyclerview适配器 加载头部 以及自定义View

 

 

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import bean.TuijianBean;
import commerce.bick.com.jdcommerce.R;

/**
* Created by wsy on 2016/8/4.
*/
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyHolder> {

private RecyclerView mRecyclerView;

private List<TuijianBean> data;
private Context mContext;

private View VIEW_FOOTER;
private View VIEW_HEADER;

private View VIEW_VIEWPAGE;
private View VIEW_MIAOPAGE;
//Type
private int TYPE_NORMAL = 1000;//默认
private int TYPE_HEADER = 1001;//头部
private int TYPE_FOOTER = 1002;//脚部
private int TYPE_VIEWPAGE=1003;//Viewpage
private int TYPE_MIAOPAGE=1004;//秒杀部位

public RecycleAdapter(List<TuijianBean> data, Context mContext) {
this.data = data;
this.mContext = mContext;
}

@Override
public RecycleAdapter.MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_FOOTER) {
return new MyHolder(VIEW_FOOTER);
} else if (viewType == TYPE_HEADER) {
return new MyHolder(VIEW_HEADER);
}else if(viewType==TYPE_VIEWPAGE){
return new MyHolder(VIEW_VIEWPAGE);
}else if(viewType==TYPE_MIAOPAGE){
return new MyHolder(VIEW_MIAOPAGE);
}
else {
return new MyHolder(getLayout(R.layout.item_list_layout));
}
}
@Override
public void onBindViewHolder(final MyHolder holder, int position) {
if (!isHeaderView(position) && !isFooterView(position) && !isViewPageView(position) &&!isMiaoPageView(position)) {
if (haveHeaderView()) position--;
if (havePageView()) position--;
if (haveMiaoView()) position--;
ImageView iv = holder.itemView.findViewById(R.id.zhu_iv_gv);
TextView tv = holder.itemView.findViewById(R.id.zhu_tv);
TextView zhu_tv_price=holder.itemView.findViewById(R.id.zhu_tv_price);
zhu_tv_price.setText("¥"+data.get(position).price);
// Toast.makeText(mContext,"商品pid"+data.get(position).pid+data.get(position).title,Toast.LENGTH_SHORT).show();
Glide.with(mContext).load(data.get(position).images).into(iv);
tv.setText(data.get(position).title);

final int pos = holder.getLayoutPosition()-3;
/**
* 主页填充数据
*/
if(mOnItemClickListener!=null){
//item的点击事件
final int finalPosition = position;
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

mOnItemClickListener.onClick(finalPosition,data.get(pos).pid);
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
mOnItemClickListener.onLongClick(finalPosition,data.get(pos).pid);
return false;
}
});
}
}
}
private OnItemClickListener mOnItemClickListener;
public interface OnItemClickListener{
//点击事件
void onClick( int position,String pid);
//长按事件
void onLongClick( int position,String pid);
}

public void setOnItemClickListener(OnItemClickListener onItemClickListener ){
this. mOnItemClickListener=onItemClickListener;
}

@Override
public int getItemCount() {
int count = (data == null ? 0 : data.size());
if (VIEW_FOOTER != null) {
count++;
}
if (VIEW_HEADER != null) {
count++;
}
if(VIEW_MIAOPAGE !=null){
count++;
}
if(VIEW_VIEWPAGE !=null){
count++;
}
return count;
}
@Override
public int getItemViewType(int position) {
if (isHeaderView(position)) {
return TYPE_HEADER;
} else if (isFooterView(position)) {
return TYPE_FOOTER;
}else if(isViewPageView(position)){
return TYPE_VIEWPAGE;
}else if(isMiaoPageView(position)){
return TYPE_MIAOPAGE;
}
else {
return TYPE_NORMAL;
}
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
try {
if (mRecyclerView == null && mRecyclerView != recyclerView) {
mRecyclerView = recyclerView;
}
ifGridLayoutManager();
} catch (Exception e) {
e.printStackTrace();
}
}

private View getLayout(int layoutId) {
return LayoutInflater.from(mContext).inflate(layoutId, null);
}

public void addHeaderView(View headerView) {
if (haveHeaderView()) {
throw new IllegalStateException("hearview has already exists!");
} else {
//避免出现宽度自适应
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
headerView.setLayoutParams(params);
VIEW_HEADER = headerView;
ifGridLayoutManager();
notifyItemInserted(0);
}

}

public void addFooterView(View footerView) {
if (haveFooterView()) {
throw new IllegalStateException("footerView has already exists!");
} else {
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
footerView.setLayoutParams(params);
VIEW_FOOTER = footerView;
ifGridLayoutManager();
notifyItemInserted(getItemCount() - 1);
}
}
public void addPageView(View pageView) {
if (havePageView()) {
throw new IllegalStateException("pageView has already exists!");
} else {
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
pageView.setLayoutParams(params);
VIEW_VIEWPAGE = pageView;
ifGridLayoutManager();
notifyItemInserted(1);
}
}
public void addMiaoView(View miaoView) {
if (haveMiaoView()) {
throw new IllegalStateException("miaoView has already exists!");
} else {
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
miaoView.setLayoutParams(params);
VIEW_MIAOPAGE = miaoView;
ifGridLayoutManager();
notifyItemInserted(2);
}
}

private void ifGridLayoutManager() {
if (mRecyclerView == null) return;
final RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
if (layoutManager instanceof GridLayoutManager) {
final GridLayoutManager.SpanSizeLookup originalSpanSizeLookup =
((GridLayoutManager) layoutManager).getSpanSizeLookup();
((GridLayoutManager) layoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return (isHeaderView(position) || isFooterView(position) ||isViewPageView(position)) || isMiaoPageView(position) ?
((GridLayoutManager) layoutManager).getSpanCount() :
1;
}
});
}
}

private boolean haveHeaderView() {
return VIEW_HEADER != null;
}

public boolean haveFooterView() {
return VIEW_FOOTER != null;
}
private boolean havePageView() {
return VIEW_VIEWPAGE != null;
}
private boolean haveMiaoView() {
return VIEW_MIAOPAGE != null;
}



private boolean isHeaderView(int position) {
return haveHeaderView() && position == 0;
}
private boolean isFooterView(int position) {
return haveFooterView() && position == getItemCount() - 1;
}
private boolean isViewPageView(int position) {
return havePageView() && position == 1;
}
private boolean isMiaoPageView(int position) {
return haveMiaoView() && position == 2;
}


public static class MyHolder extends RecyclerView.ViewHolder {

public MyHolder(View itemView) {
super(itemView);
}
}

}

————————————————
版权声明:本文为CSDN博主「_lonty」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_39200790/article/details/78333806

posted @ 2022-05-07 14:18  新感觉  阅读(233)  评论(0编辑  收藏  举报