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 {
private Context mContext;
List mDataBeans;
private onItemClick clickCb;
public CoverRecyclerViewAdapter(Context c, List 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 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);
}
}