本次demo写的较为简单,主要是使用了RecycleView自带的addOnScrollListener方法
代码如下:
MainActivity中:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecommendView;
private ArrayList<VideoEntity> videoList = new ArrayList<VideoEntity>();
private RecommendAdapter recommendAdapter = new RecommendAdapter(MainActivity.this, videoList);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
//如果是横屏
setContentView(R.layout.activity_main_horizontal);
} else {
setContentView(R.layout.activity_main_vertical);
for (int i = 0; i < 20; i++) {
VideoEntity videoEntity = new VideoEntity(i + "", i + "hhhhhhhhhh");
videoList.add(videoEntity);
}
mRecommendView = findViewById(R.id.recommend_list);
LinearLayoutManager linearLayoutManagerNew = new LinearLayoutManager(MainActivity.this);
mRecommendView.setLayoutManager(linearLayoutManagerNew);
mRecommendView.setAdapter(recommendAdapter);
initRvCommendNew(linearLayoutManagerNew);
}
}
protected void initRvCommendNew(LinearLayoutManager linearLayoutManagerNew) {
mRecommendView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
//获取当前页面第一个item的posion值
int visibleFirst = linearLayoutManagerNew.findFirstVisibleItemPosition();
//获取当前页面最后一个item的posion值
int visibleLast = linearLayoutManagerNew.findLastVisibleItemPosition();
if (visibleFirst <= 0) {
recommendAdapter.loadPreData();
}else if(visibleLast >= recommendAdapter.getItemCount() -1) {
recommendAdapter.loadNextData();
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
}
}
在Adapter中要写好上滑和下拉时数据的处理更新
public class RecommendAdapter extends RecyclerView.Adapter<RecommendHolder> {
private Context mContext;
private ArrayList<VideoEntity> videoList;
public RecommendAdapter(Context mContext, ArrayList<VideoEntity> videoList) {
this.mContext = mContext;
this.videoList = videoList;
}
@Override
public RecommendHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(mContext, R.layout.item_recommend, null);
RecommendHolder myViewHoder = new RecommendHolder(view);
return myViewHoder;
}
@Override
public void onBindViewHolder(RecommendHolder holder, int position) {
if (videoList.get(position) != null) {
holder.bindData(videoList.get(position));
}
}
@Override
public int getItemCount() {
return videoList.size();
}
public void loadNextData() {
ArrayList<VideoEntity> nextVideoList = new ArrayList<VideoEntity>();
for (int i = 0; i < 5; i++) {
VideoEntity vEntity = new VideoEntity(i+"",i+"xxxxxxxxx");
videoList.add(vEntity);
}
notifyItemInserted(this.getItemCount());
}
public void loadPreData() {
ArrayList<VideoEntity> nextVideoList = new ArrayList<VideoEntity>();
for (int i = 0; i < 5; i++) {
VideoEntity vEntity = new VideoEntity(i+"",i+"xxxxxxxxx");
videoList.add(0,vEntity);
}
notifyItemChanged(0);
}
}
最后 该写法在下拉刷新的时候会出现bug 应该是刷新时机有一些问题。
置顶
protected LinearLayoutManager linearLayoutManagerNew;
linearLayoutManagerNew = new LinearLayoutManager(rootView.getContext());
linearLayoutManagerNew.scrollToPosition(3);// 列表滚动到第三个item
通过LinearLayoutManager的scrollToPosition方法 让列表滚动到第几个,由此实现置顶