RecyclerView 實現輪播圖
阿新 • • 發佈:2019-01-08
RecyclerView
mHeaderView = LayoutInflater.from(getContext()).inflate(R.layout.item_first_fragment_header, null); listviewLayout.addHeaderView(mHeaderView); mViewHolder = new ViewHolder(mHeaderView); coverRecyclerViewAdapter = new CoverRecyclerViewAdapter(getContext(), headListBean, new CoverRecyclerViewAdapter.onItemClick() { @Override public void clickItem(int position) { if (count == 0) return; GuanerBean.DataBean dataBean = headListBean.get(position % count); if (dataBean == null) return; String groupId = dataBean.getId(); DetailAcivity.launch(activity, groupId); } }); mViewHolder.coverResyclerview.setHasFixedSize(true); mViewHolder.coverResyclerview.setAdapter(coverRecyclerViewAdapter); mViewHolder.coverResyclerview.setAlphaItem(true); //設定灰度漸變 mViewHolder.coverResyclerview.scrollToPosition(1000);
定時
private void doStart() { isStart = true; if (mTimer == null) { mTimer = new Timer(); } mTimer.schedule(new AutoTask(),TIME,TIME); } private class AutoTask extends TimerTask { @Override public void run() { mHandler.post(runnable); } } private final static class AutoHandler extends android.os.Handler {} private Runnable runnable = new Runnable() { @Override public void run() { currentPage = mViewHolder.coverResyclerview.getSelectedPos(); currentPage++; mViewHolder.coverResyclerview.smoothScrollToPosition(currentPage); } }; public void stop() { isStart = false; if (mTimer != null) {//先取消定時器 mTimer.cancel(); mTimer = null; } }
Adapter
public class CoverRecyclerViewAdapter extends RecyclerView.Adapter<CoverRecyclerViewAdapter.ViewHolder> { private Context mContext; List<GuanerBean.DataBean> mDataBeans; private onItemClick clickCb; public CoverRecyclerViewAdapter(Context c, List<GuanerBean.DataBean> dataBeans, onItemClick cb) { mDataBeans = dataBeans; mContext = c; clickCb = cb; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(mContext).inflate(R.layout.coverflow_recyclerview_item, parent, false); return new ViewHolder(v); } @Override public long getItemId(int position) { if (mDataBeans != null && mDataBeans.size() > 0) { return Long.parseLong(mDataBeans.get(position).getId()); } return 0; } @Override public int getItemCount() { if (mDataBeans == null || mDataBeans.size() < 0) { return 0; } return Integer.MAX_VALUE; } @Override public void onBindViewHolder(ViewHolder holder, final int position) { if (null == mDataBeans || mDataBeans.size() <= 0) return; GuanerBean.DataBean dataBean = mDataBeans.get(position % mDataBeans.size()); System.out.println("position % mDataBeans.size() : position="+ position +" % "+mDataBeans.size()+" = "+position % mDataBeans.size()); if (dataBean != null) { String[] pics; if (!TextUtils.isEmpty(dataBean.getBanner_pics())) { pics = dataBean.getBanner_pics().split("\\,"); if (null != pics && pics.length > 0) { String picUrl = DubPreferenceUtils.getString(mContext, Url.domain) + pics[0]; if (!TextUtils.isEmpty(picUrl)) { /*DisplayImageOptions option=new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_launcher)//設定圖片下載期間顯示的圖片 .showImageForEmptyUri(R.drawable.data_error)//設定圖片uri為空或是錯誤的時候顯示的圖片 .showImageOnFail(R.drawable.add_pic).build();//設定圖片載入或解碼過程中發生錯誤顯示的圖片*/ ImageLoader.getInstance().displayImage(picUrl, holder.img, new ImageLoadingListener() { @Override public void onLoadingStarted(String s, View view) {} @Override public void onLoadingFailed(String s, View view, FailReason failReason) { ((ImageView) view).setImageDrawable(mContext.getResources().getDrawable(R.drawable.add_pic)); } @Override public void onLoadingComplete(String s, View view, Bitmap bitmap) {} @Override public void onLoadingCancelled(String s, View view) {} }); } } } } holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Toast.makeText(mContext, "點選了:"+position, Toast.LENGTH_SHORT).show(); if (clickCb != null) { clickCb.clickItem(position % mDataBeans.size()); } } }); } public void setListItems(List<GuanerBean.DataBean> dataBeans) { this.mDataBeans = dataBeans; } public int getDataCount() { if (mDataBeans != null) { return mDataBeans.size(); } return 0; } class ViewHolder extends RecyclerView.ViewHolder { RoundImage img; public ViewHolder(View itemView) { super(itemView); img = itemView.findViewById(R.id.img); } } public interface onItemClick { void clickItem(int pos); } }