編輯:關於Android編程
RecyclerView 水平多行排列添加頭和尾
import android.content.Context; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.StaggeredGridLayoutManager; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.List; /** * Created by guanyueyun on 2016/11/17. */ public abstract class HeaderFooterRecyclerAdapterextends RecyclerView.Adapter { private int verticalItem;//垂直item private int horizontalItem;//水平item private int verticalHeaderItem;//垂直時的頭 private int horizontalHeaderItem;//水平時頭布局 private int verticalFooterItem;//垂直時的尾 private int horizontalFooterItem;//水平時的尾 public void setVerticalItem(int verticalItem) { this.verticalItem = verticalItem; } public void setHorizontalItem(int horizontalItem) { this.horizontalItem = horizontalItem; } public void setVerticalHeaderItem(int verticalHeaderItem) { this.verticalHeaderItem = verticalHeaderItem; } public void setHorizontalHeaderItem(int horizontalHeaderItem) { this.horizontalHeaderItem = horizontalHeaderItem; } public void setVerticalFooterItem(int verticalFooterItem) { this.verticalFooterItem = verticalFooterItem; } public void setHorizontalFooterItem(int horizontalFooterItem) { this.horizontalFooterItem = horizontalFooterItem; } private int mNowItemLayoutID; private int mNowHeaderLayoutID; private int mNowFooterLayoutID; public static final int TYPE_HEADER = 0; public static final int TYPE_FOOTER = 1; public static final int TYPE_NORMAL = 2; private static final String TAG = "HeaderRecyclerAdapter"; protected List mDataList; private Context mContext; public HeaderFooterRecyclerAdapter(List dataList,RecyclerView recyclerView, int mItemLayoutId) { mDataList = dataList; mContext=recyclerView.getContext(); mRecyclerView=recyclerView; if (mItemLayoutId==0){ return; } verticalItem=mItemLayoutId; horizontalItem=mItemLayoutId; } RecyclerView mRecyclerView; private View mHeaderView; private View mFooterView; /** * 設置頭布局 * @param headerViewLayoutID */ public void setHeaderView(int headerViewLayoutID) { dealHeaderFooterLayoutType(); headerViewLayoutID=headerViewLayoutID==0?mNowHeaderLayoutID:headerViewLayoutID; /**注意 root view 為recycleview*/ View headerView = LayoutInflater.from(mContext).inflate(headerViewLayoutID,mRecyclerView,false); mHeaderView = headerView; notifyItemInserted(0); } /** *設置頭布局 * @param headerView */ public void setHeaderView(View headerView) { mHeaderView = headerView; notifyItemInserted(0); } /** * 獲取頭布局視圖對象 * @return */ public View getHeaderView() { return mHeaderView; } /** * 設置尾布局 * @param footerViewLayoutID */ public void setFooterView(int footerViewLayoutID) { dealHeaderFooterLayoutType(); footerViewLayoutID=footerViewLayoutID==0?mNowFooterLayoutID:footerViewLayoutID; View footerView = LayoutInflater.from(mContext).inflate(footerViewLayoutID,mRecyclerView,false); mFooterView = footerView; notifyItemInserted(getItemCount() - 1); } /** * 設置尾布局 * @param footerView */ public void setFooterView(View footerView) { mFooterView = footerView; notifyItemInserted(getItemCount() - 1); } /** * 獲取尾布局對象 * @return */ public View getFooterView() { return mFooterView; } /** * 更新數據源 * @param dataList */ public void refreshDataList(List dataList) { mDataList.clear(); mDataList.addAll(dataList); notifyDataSetChanged(); } /** * 數據源天加新數據 * @param dataList */ public void appendDataList(List dataList) { mDataList.addAll(dataList); notifyDataSetChanged(); } @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); //為GridLayoutManager 合並頭布局的跨度 RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager(); if (layoutManager instanceof GridLayoutManager) { final GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager; gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() { /** * 抽象方法 返回當前index位置的item所占用的跨度的數量 * ##單元格合並 就是相當於占據了設定列spanCount的數量 * ##不合並 就是相當於占據了原來1個跨度 * * @param position * @return */ @Override public int getSpanSize(int position) { int i = (getItemViewType(position) == TYPE_HEADER || getItemViewType(position) == TYPE_FOOTER) ? gridLayoutManager.getSpanCount() : 1; return i; } }); // mNowItemLayoutID = gridLayoutManager.getOrientation() == GridLayoutManager.HORIZONTAL ? horizontalItem : verticalItem; // } else if (layoutManager instanceof StaggeredGridLayoutManager) { StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager; mNowItemLayoutID = staggeredGridLayoutManager.getOrientation() == StaggeredGridLayoutManager.HORIZONTAL ? horizontalItem : verticalItem; } else if (layoutManager instanceof LinearLayoutManager) { LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager; mNowItemLayoutID = linearLayoutManager.getOrientation() == LinearLayoutManager.HORIZONTAL ?horizontalItem : verticalItem; } } @Override public int getItemViewType(int position) { if (mHeaderView != null && mFooterView != null) { if (position == 0) { return TYPE_HEADER; } if (position == getItemCount() - 1) { return TYPE_FOOTER; } return TYPE_NORMAL; } else if (mHeaderView != null) { if (position == 0) { return TYPE_HEADER; } return TYPE_NORMAL; } else if (mFooterView != null) { if (position == getItemCount() - 1) { return TYPE_FOOTER; } return TYPE_NORMAL; } return TYPE_NORMAL; } @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (mHeaderView != null && viewType == TYPE_HEADER) { return new HeaderFooterRecyclerViewHolder(mHeaderView); } if (mFooterView != null && viewType == TYPE_FOOTER) { return new HeaderFooterRecyclerViewHolder(mFooterView); } View view = LayoutInflater.from(parent.getContext()).inflate(mNowItemLayoutID, parent, false); return new HeaderFooterRecyclerViewHolder(view); } @Override public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { ViewGroup.LayoutParams vg_lp = viewHolder.itemView.getLayoutParams(); if (vg_lp != null && vg_lp instanceof StaggeredGridLayoutManager.LayoutParams) { StaggeredGridLayoutManager.LayoutParams sglm_lp = (StaggeredGridLayoutManager.LayoutParams) vg_lp; if (getItemViewType(position) == TYPE_HEADER) { sglm_lp.setFullSpan(position == 0); } if (getItemViewType(position) == TYPE_FOOTER) { sglm_lp.setFullSpan(position == getItemCount() - 1); } } int itemType = getItemViewType(position); if (itemType == TYPE_HEADER || itemType == TYPE_FOOTER) { return; } int realPos = position; if (mHeaderView != null) { realPos = position - 1; } final int pos = realPos; final T data = mDataList.get(pos); onBindViewHolderInner(viewHolder, pos, data); viewHolder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(onItemClickListener != null) { onItemClickListener.onItemClick(v,pos, data); } } }); } public void dealHeaderFooterLayoutType(){ /**單獨為HORIZONTAL 設置左側的布局 右側布局*/ RecyclerView.LayoutManager layoutManager=mRecyclerView.getLayoutManager(); if (layoutManager instanceof StaggeredGridLayoutManager){ StaggeredGridLayoutManager staggeredGridLayoutManager= (StaggeredGridLayoutManager) layoutManager; mNowHeaderLayoutID=staggeredGridLayoutManager.getOrientation()==StaggeredGridLayoutManager.HORIZONTAL ?horizontalHeaderItem :verticalHeaderItem; mNowFooterLayoutID=staggeredGridLayoutManager.getOrientation()==StaggeredGridLayoutManager.HORIZONTAL ?horizontalFooterItem :verticalFooterItem; }else if (layoutManager instanceof GridLayoutManager){ GridLayoutManager gridLayoutManager= (GridLayoutManager) layoutManager; mNowHeaderLayoutID=gridLayoutManager.getOrientation()==GridLayoutManager.HORIZONTAL ?horizontalHeaderItem :verticalHeaderItem; mNowFooterLayoutID=gridLayoutManager.getOrientation()==GridLayoutManager.HORIZONTAL ?horizontalFooterItem :verticalFooterItem; }else if (layoutManager instanceof LinearLayoutManager){ LinearLayoutManager linearLayoutManager= (LinearLayoutManager) layoutManager; mNowHeaderLayoutID=linearLayoutManager.getOrientation()==LinearLayoutManager.HORIZONTAL ?horizontalHeaderItem :verticalHeaderItem; mNowFooterLayoutID=linearLayoutManager.getOrientation()==LinearLayoutManager.HORIZONTAL ?horizontalFooterItem :verticalFooterItem; } } public abstract void onBindViewHolderInner(RecyclerView.ViewHolder viewHolder, int realPosition, T data); @Override public int getItemCount() { if (mHeaderView != null && mFooterView != null) { return mDataList.size() + 1 + 1; } else if (mHeaderView != null) { return mDataList.size() + 1; } else if (mFooterView != null) { return mDataList.size() + 1; } return mDataList.size(); } public interface OnItemClickListener { void onItemClick(View itemView, int position, Object data); } private OnItemClickListener onItemClickListener; public void setOnItemClickListener(OnItemClickListener onItemClickListener) { this.onItemClickListener = onItemClickListener; } }
封裝ViewHolder
package com.guanyueyun.mylook.adapter; import android.support.v7.widget.RecyclerView; import android.util.SparseArray; import android.view.View; /** * */ public class HeaderFooterRecyclerViewHolder extends RecyclerView.ViewHolder{ private SparseArraymViewSparseArray = new SparseArray<>(); private View mItemView; public HeaderFooterRecyclerViewHolder(View itemView) { super(itemView); mItemView=itemView; } public T onBindView(int viewId) { View view = mViewSparseArray.get(viewId); if (view == null) { view = mItemView.findViewById(viewId); mViewSparseArray.put(viewId, view); } return (T) view; } }
下面是自己適配器
package com.guanyueyun.mylook.adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.widget.ImageView; import android.widget.TextView; import com.guanyueyun.mylook.R; import com.guanyueyun.mylook.bean.GoodsListInfo; import com.squareup.picasso.Picasso; import java.util.List; /** * Created by Administrator on 2016/11/17. */ public class HomeHeaderAdapter extends HeaderFooterRecyclerAdapter{ private Context context; /** * * @param dataList 數據源 * @param recyclerView * @param mItemLayoutId item */ public HomeHeaderAdapter(List dataList, RecyclerView recyclerView, int mItemLayoutId) { super(dataList, recyclerView, mItemLayoutId); //設置中間布局 this.context = recyclerView.getContext(); } @Override public void onBindViewHolderInner(RecyclerView.ViewHolder viewHolder, int position, Object data) { GoodsListInfo.SkuBean skuBean = (GoodsListInfo.SkuBean) mDataList.get(position); if(viewHolder instanceof HeaderFooterRecyclerViewHolder) { HeaderFooterRecyclerViewHolder holder= (HeaderFooterRecyclerViewHolder) viewHolder; ImageView itemIv=holder.onBindView(R.id.item_homegoodslist_item_iv); TextView itemtv=holder.onBindView(R.id.item_homegoodslist_item_tv); Picasso.with(context).load(skuBean.getImages().get(0).getUrl()).into(itemIv); itemtv.setText(skuBean.getPrice2().getValue_display()); } } }
接下來就是添加數據源添加頭和尾
RecyclerView recyclerView = holder.itemHomeGoodslistRecyclerview; LinearLayoutManager layoutManager = new LinearLayoutManager(context); //設置布局管理器 GridLayoutManager.HORIZONTAL recyclerView.setLayoutManager(new GridLayoutManager(context,2, GridLayoutManager.HORIZONTAL,false)); // //設置為垂直布局,這也是默認的 // layoutManager.setOrientation(OrientationHelper.HORIZONTAL); HomeGoodsListAdapter recycleAdapter = new HomeGoodsListAdapter(info.getSku(),context); HomeHeaderAdapter adapter = new HomeHeaderAdapter(info.getSku(),recyclerView,R.layout.item_homegoodslist_item); //設置頭布局 adapter.setHeaderView(R.layout.item_homegoodslist_header); View headerView = adapter.getHeaderView();//獲取頭布局視圖對象 ImageView headerViewIv = (ImageView) headerView.findViewById(R.id.item_home_selectd_header); Picasso.with(context).load(info.getSku().get(0).getImages().get(0).getUrl()).into(headerViewIv); //設置尾布局 // adapter.setFooterView(R.layout.item_homegoodslist_header); //設置Adapter recyclerView.setAdapter(adapter); //設置增加或刪除條目的動畫 recyclerView.setItemAnimator( new DefaultItemAnimator());
什麼是ListView分頁加載數據功能呢?在現在的大數據時代,我們不可能把某些數據全部展示到界面,好比我們經常會看的QQ空間一樣,當你看動態的時候,系統不可能會把所有好友
本文實例為大家分享了Android九宮格圖片展示的具體代碼,供大家參考,具體內容如下MainActivity.java代碼:package siso.ninegridim
DatePickerDiaLog 在android 4.n的版本中,取消選擇日期時,會調用onDateSet ,確認選擇日期時會調用兩次onDateSet(但在高版本中不
首先明確流程 既然實現自動更新,我們首先必須讓我們的應用知道是否存在新版本的軟件,因此我們可以在自己的網站上放置配置文件,存放軟件的版本信息: 2