1. 实现思路
思路很简单,就是判断最后一个item的位置是否大于或等于总数据的数量;如果是,那么说明没铺满;如果不是,那么说明已经超过一个屏幕了,不用继续请求
2. 测试代码
/**
* Desc : 验证RecyclerView是否满一屏幕
* version : v1.0
*/
public class RecyclerViewTestActivity extends AppCompatActivity{
private LinearLayoutManager mLinearLayoutManager;
private SimpleTextAdapter mSimpleTextAdapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recyclerview);
final RecyclerView rv = findViewById(R.id.rv);
mSimpleTextAdapter = new SimpleTextAdapter(null);
rv.setAdapter(mSimpleTextAdapter);
mLinearLayoutManager = new LinearLayoutManager(this);
rv.setLayoutManager(mLinearLayoutManager);
//下面代码是重点
rv.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int lastCompletelyVisibleItemPosition = mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
Log.e("test", "lastCompletelyVisibleItemPosition is : " + lastCompletelyVisibleItemPosition);
boolean b = lastCompletelyVisibleItemPosition < mSimpleTextAdapter.getItemCount() - 1;
if (b){
Log.e("test" ,"超过一屏幕,移除啦");
rv.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}else {
Log.e("test" ,"没有超出超过一屏幕,继续请求");
mSimpleTextAdapter.addData(6);
}
}
});
}
}
复制代码
public class SimpleTextAdapter extends RecyclerView.Adapter<SimpleTextAdapter.ViewHolder> {
private CharSequence[] characters;
private int size = 10;
public SimpleTextAdapter(CharSequence[] character) {
this.characters = character;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_slidingcard, parent, false));
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if (characters != null){
holder.title.setText(characters[position]);
}else {
holder.title.setText(position + ":" + "测试数据");
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addData(4);
}
});
}
@Override
public int getItemCount() {
if (characters == null){
return size;
}
return characters.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView title;
public ViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title);
}
}
public void addData(int addSize){
int prev = size;
this.size = size + addSize;
notifyItemRangeInserted(prev,addSize);
}
}
复制代码
布局文件: activity的:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.constraint.ConstraintLayout>
复制代码
adapter的:
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
app:cardCornerRadius="4dp"
app:cardElevation="2dp"
android:foreground="?android:attr/selectableItemBackground">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="我是测试数据"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</android.support.v7.widget.CardView>
复制代码