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