Android 中 SwipeRefreshLayout与RecyclerView
用RecyclerView结合SwipeRefreshLayout,想做一个下拉刷新的列表。
问题1:
在界面初始化时调用 swipeRefresh.setRefreshing(true);
无法进入监听器的 onRefresh方法.只能手工滑动界面才能进入。
问题2:
initData方法用来加载数据,数据加载完毕后调用RecyclerView的 adapter.notifyDataSetChanged();
却没有去执行Adapter中的onBindViewHolder方法。
贴一下关键代码,HomeFragment相关代码
private ArrayList<Integer> dietIds = new ArrayList<Integer>();
List<Diet> dietList = new ArrayList<>();
boolean isLoading;
RecyclerViewAdapter<Diet> adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
initBanner(view);
initGridView(view);
initSwipeRefreshLayout(view);
initTextView(view);
initRecyclerView(view);
return view;
}
...
private void initSwipeRefreshLayout(View view) {
swipeRefreshLayout = view.findViewById(R.id.home_swipeRefresh);
swipeRefreshLayout.setColorSchemeResources(R.color.appColor);
swipeRefreshLayout.setOnRefreshListener(() -> {
LogUtil.d("swipe onRefresh initData");
initData();
});
swipeRefreshLayout.setRefreshing(true); //此处不会进入监听器的onRefresh方法
}
private void initRecyclerView(View view) {
recyclerView = view.findViewById(R.id.home_recyclerView);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
adapter = new RecyclerViewAdapter<Diet>(R.layout.item_listview_common, dietList) {
@Override
public void bindView(TViewHolder holder, Diet item) { // 这是一个回调函数,但不会执行
LogUtil.d("aabbcc" + item.getDietId());
}
};
recyclerView.setAdapter(adapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (!isLoading) {
isLoading = true;
new Thread() {
@Override
public void run() {
initData();
isLoading = false;
}
}.start();
}
}
});
}
private void initData() {
dietIds.clear();
RequestParams params = new RequestParams(SysParameter.URL_GetDailyRecommend);
x.http().get(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
dietList = ...; // 得到数据
adapter.notifyDataSetChanged(); //不会调用上面的bindView()
swipeRefreshLayout.setRefreshing(false);
}
});
}
这是Adapter
public abstract class RecyclerViewAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<T> data;
private int layoutId;
public RecyclerViewAdapter(int layoutId, List<T> data) {
this.layoutId = layoutId;
this.data = data;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
TViewHolder viewHolder = new TViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
LogUtil.d("aabbcc_here"); //这句话不会打印
if (holder instanceof TViewHolder) {
LogUtil.d("aabbcc_position" + position);
bindView((TViewHolder) holder, data.get(position));
}
}
public abstract void bindView(TViewHolder holder, T item);
@Override
public int getItemCount() {
return data.size();
}
public static class TViewHolder extends RecyclerView.ViewHolder {
private SparseArray<View> mViews; //存放每个Item子项中的控件
private View item;
public TViewHolder(View itemView) {
super(itemView);
item = itemView;
mViews = new SparseArray<>();
}
... //其他操作
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)