1. 程式人生 > >RecycleView學習0 基本用法

RecycleView學習0 基本用法

1RecycleView Adapter 基本使用:

包括多種itemType,繫結事件,

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.
LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import com.bumptech.glide.Glide; import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.request.RequestOptions; import com.yiche.autoreport.R; import com.yiche.autoreport.model.business.industry.
IndustryNewsItem; import com.yiche.autoreport.utils.V; import java.util.ArrayList; /** * Created by lidongxiu on 2018/6/19. */ public class IndustryInsightNewsAdapter extends RecyclerView.Adapter<IndustryInsightNewsAdapter.NewsItemHolder> { private Context mContext; private LayoutInflater mInflater;
private OnItemClickListener mClickListener; private OnChildClickListener mChildClickListener; private ArrayList<IndustryNewsItem> mDataList; public interface OnItemClickListener { void onItemClicked(int position, View view); } public interface OnChildClickListener { void onChildClicked(int position, View view); } public void setClickListener(OnItemClickListener mClickListener) { this.mClickListener = mClickListener; } public void setChildClickListener(OnChildClickListener mChildClickListener) { this.mChildClickListener = mChildClickListener; } public IndustryInsightNewsAdapter(Context context, ArrayList<IndustryNewsItem> dataList) { mContext = context; mInflater = LayoutInflater.from(mContext); mDataList = dataList; } @Override public NewsItemHolder onCreateViewHolder(ViewGroup parent, int viewType) { NewsItemHolder viewHolder = null; switch (viewType) { case IndustryNewsItem.NOIMAGE: viewHolder = new TitleItemHolder(mInflater.inflate(R.layout.industryinsight_noimage_layout_white, parent, false)); break; case IndustryNewsItem.ONEIMAGE: viewHolder = new BodyItemHolder(mInflater.inflate(R.layout.industrynews_oneimage_layout_white, parent, false)); break; } return viewHolder; } @Override public void onBindViewHolder(NewsItemHolder holder, int position) { holder.setNewsItem(mDataList.get(position), position); } //多種型別itemView @Override public int getItemViewType(int position) { return mDataList.get(position).getType(); } @Override public int getItemCount() { return mDataList == null ? 0 : mDataList.size(); } public abstract class NewsItemHolder extends RecyclerView.ViewHolder { public NewsItemHolder(View itemView) { super(itemView); } public abstract void setNewsItem(IndustryNewsItem item, int position); } private class TitleItemHolder extends NewsItemHolder { public TextView titleDel = null; public ImageView notlike = null; public RelativeLayout mContainer = null; public TextView fromSource = null; public TextView publishData = null; public TitleItemHolder(View itemView) { super(itemView); titleDel = (TextView) V.$(itemView, R.id.industry_insight_news_tv); notlike = (ImageView) V.$(itemView, R.id.industry_insight_notlike_iv); mContainer = (RelativeLayout) V.$(itemView, R.id.industry_insight_noimg_container); fromSource = (TextView) V.$(itemView, R.id.bottom_left_original); publishData = (TextView) V.$(itemView, R.id.bottom_left_publishdata); } @Override public void setNewsItem(IndustryNewsItem item, final int position) { if(item != null){ if (!TextUtils.isEmpty(item.getTitle())) { titleDel.setText(item.getTitle()); } if (!TextUtils.isEmpty(item.getSource())) { fromSource.setText(item.getSource()); } if (!TextUtils.isEmpty(item.getPublish())) { publishData.setText(item.getPublish()); } } if (mClickListener != null && mContainer != null) { mContainer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mClickListener.onItemClicked(position, v); } }); } if (mChildClickListener != null && notlike != null) { notlike.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mChildClickListener.onChildClicked(position, v); } }); } } } private class BodyItemHolder extends NewsItemHolder { public TextView newsTitle = null; public ImageView newsPublisherAvatar = null; public ImageView notlike = null; public LinearLayout mContainer = null; public TextView fromSource = null; public TextView publishData = null; public BodyItemHolder(View itemView) { super(itemView); newsTitle = (TextView) V.$(itemView, R.id.industry_insight_news_tv); newsPublisherAvatar = (ImageView) V.$(itemView, R.id.industry_insight_news_iv); notlike = (ImageView) V.$(itemView, R.id.industry_insight_notlike_iv); mContainer = (LinearLayout) V.$(itemView, R.id.industry_insight_oneimgcontainer); fromSource = (TextView) V.$(itemView, R.id.bottom_left_original); publishData = (TextView) V.$(itemView, R.id.bottom_left_publishdata); } @Override public void setNewsItem(IndustryNewsItem item, final int position) { if (item != null){ if (!TextUtils.isEmpty(item.getTitle())) { newsTitle.setText(item.getTitle()); } if (!TextUtils.isEmpty(item.getSource())) { fromSource.setText(item.getSource()); } if (!TextUtils.isEmpty(item.getPublish())) { publishData.setText(item.getPublish()); } } if (item != null && item.getImage() != null && item.getImage().get(0) != null) { try { RequestOptions options = new RequestOptions() .placeholder(R.drawable.splash_icon) .dontAnimate() .diskCacheStrategy(DiskCacheStrategy.RESOURCE); Glide.with(mContext) .load(item.getImage().get(0).getImg()) .apply(options) .into(newsPublisherAvatar); } catch (Exception e) { e.printStackTrace(); } } if (mChildClickListener != null && notlike != null) { notlike.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mChildClickListener.onChildClicked(position, v); } }); } if (mClickListener != null && mContainer != null) { mContainer.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mClickListener.onItemClicked(position, v); } }); } } } }

2 itemview 預設動畫關閉和設定時常

public void openDefaultAnimator() {
    this.getItemAnimator().setAddDuration(120);
    this.getItemAnimator().setChangeDuration(250);
    this.getItemAnimator().setMoveDuration(250);
    this.getItemAnimator().setRemoveDuration(120);
    ((SimpleItemAnimator) this.getItemAnimator()).setSupportsChangeAnimations(true);
}

//很多時候載入圖片閃動和動畫有關
public void closeDefaultAnimator() {
    this.getItemAnimator().setAddDuration(0);
    this.getItemAnimator().setChangeDuration(0);
    this.getItemAnimator().setMoveDuration(0);
    this.getItemAnimator().setRemoveDuration(0);
    ((SimpleItemAnimator) this.getItemAnimator()).setSupportsChangeAnimations(false);
}

3 三種佈局管理器

LinearLayoutManager 線性佈局管理器 GridLayoutManager 網格佈局管理器 StaggeredGridLayoutManager 瀑布流式佈局管理器 mRecycleView.setLayoutManager();

4 Adapter的區域性重新整理

在這裡插入圖片描述 Adapter呼叫上面的函式完成區域性重新整理。

5 移動到特定position

mRecycleView.smoothScrollToPosition(); RecycleView的smoothScrollToPosition函式只要item可見就停止滑動,所以一般無法滿足需求。 mLinearLayoutManger.scrollToPositionWithOffset(xiposition, 0); layoutManager的scrollToPositionWithOffset函式可以很好的實現滑動到特定position的需求。