Android StaggeredGridLayoutManager布局RecyclerView在滚动状态可见范围刷新数据
之所以把StaggeredGridLayoutManager布局的RecyclerView单列出来处理滚动状态下可见(可视)范围内的数据更新问题,是因为在StaggeredGridLayoutManager布局下的RecyclerView,对于第一个可视item和最后一个可视item,即第一个可视item位置和最后一个可视item位置,不像在LinearLayoutManager布局下直接获取,需要进行一个简单计算。关键的计算过程我写在了getOutRange()方法内。
package zhangphil.recyclerview;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
public class RecyclerViewActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerViewAdapter mAdapter;
private StaggeredGridLayoutManager layoutManager;
private ArrayList<String> mItems;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycler_view_activity);
mItems = new ArrayList<>();
for (int i = 0; i < 110; i++) {
mItems.add(i + "");
}
initRecyclerView();
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_IDLE) {
int[] outRange = getOutRange();
int len = outRange[1] - outRange[0] + 1;
int num = (int) (Math.random() * 10);
for (int i = 0; i < len; i++) {
mItems.set(outRange[0] + i, num + "");
}
mAdapter.notifyDataSetChanged();
}
}
});
}
private int[] getOutRange() {
int[] range = new int[2];
int[] first = new int[layoutManager.getSpanCount()];
layoutManager.findFirstVisibleItemPositions(first);
int[] last = new int[layoutManager.getSpanCount()];
layoutManager.findLastVisibleItemPositions(last);
range[0] = first[0];
Arrays.sort(last);
range[1] = last[last.length - 1];
return range;
}
private void initRecyclerView() {
mRecyclerView = findViewById(R.id.recycler_view);
layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
mAdapter = new RecyclerViewAdapter();
mRecyclerView.setAdapter(mAdapter);
}
private class RecyclerViewAdapter extends RecyclerView.Adapter<MyVH> {
@NonNull
@Override
public MyVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(getApplicationContext()).inflate(android.R.layout.simple_list_item_2, parent, false);
return new MyVH(view);
}
@Override
public void onBindViewHolder(@NonNull MyVH holder, int position) {
holder.text1.setText("" + position);
holder.text2.setText(mItems.get(position));
}
@Override
public int getItemCount() {
return mItems.size();
}
}
private class MyVH extends RecyclerView.ViewHolder {
public TextView text1;
public TextView text2;
public MyVH(View itemView) {
super(itemView);
text1 = itemView.findViewById(android.R.id.text1);
text1.setTextColor(Color.WHITE);
text1.setBackgroundColor(Color.RED);
text2 = itemView.findViewById(android.R.id.text2);
text2.setTextColor(Color.DKGRAY);
}
}
}
代码实现一个简单意图,用StaggeredGridLayoutManager控制RecyclerView,当RecyclerView滚动停止后,更新可可范围内的数据到最新。
如图: