gpt4 book ai didi

android - 将 RecyclerView 与来自 LoaderManager 的数据一起使用

转载 作者:行者123 更新时间:2023-11-29 14:31:00 26 4
gpt4 key购买 nike

我有一个 RecyclerView,用于显示使用 LoaderManager 从内容提供者获取的数据。它工作正常,但我注意到加载大数据时我的以下实现存在一些性能问题,显示数据时存在延迟,因为我使用 ViewPager 加载 Fragment 托管此方法

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
Favorites = new ArrayList<>();

if (cursor != null) {
while (cursor.moveToNext())
Favorites.add(FavoritesModel.fromCursor(cursor));
}
adapter = new FavoritesAdapter(getActivity(),Favorites);
favoritesRecyclerView.setAdapter(adapter);
}

所以我选择执行以下操作:

 @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
........
adapter = new FavoritesAdapter(getActivity(),Favorites);
favoritesRecyclerView.setAdapter(adapter);
........

然后在onLoadFinished上

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
Favorites = new ArrayList<>();

if (cursor != null) {
while (cursor.moveToNext())
Favorites.add(FavoritesModel.fromCursor(cursor));
}
adapter.notifyDataSetChanged();

}

使用这个什么都不会显示。问题是什么,因为 onLoadFinished 总是被调用,我知道它总是在 UI 线程上被调用

最佳答案

我希望你得到了正确的答案。好吧,我遇到了同样的问题并做了一些很好的搜索,最后想出了这个。

这是适配器

public class RecordAdapter extends RecyclerView.Adapter<RecordAdapter.ViewHolder> {
Cursor dataCursor;
Context context;

public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView time;
public TextView date;
public TextView distance;
public ImageView imageView;
public ViewHolder(View v) {
super(v);
time = (TextView) v.findViewById(R.id.time);
date = (TextView) v.findViewById(R.id.date);
distance = (TextView) v.findViewById(R.id.distance);
imageView = (ImageView) v.findViewById(R.id.image);
}
}

public RecordAdapter(Activity mContext, Cursor cursor) {
dataCursor = cursor;
context = mContext;
}

@Override
public RecordAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View cardview = LayoutInflater.from(parent.getContext())
.inflate(R.layout.record_row, parent, false);
return new ViewHolder(cardview);
}

public Cursor swapCursor(Cursor cursor) {
if (dataCursor == cursor) {
return null;
}
Cursor oldCursor = dataCursor;
this.dataCursor = cursor;
if (cursor != null) {
this.notifyDataSetChanged();
}
return oldCursor;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
dataCursor.moveToPosition(position);

String time = dataCursor.getString(dataCursor.getColumnIndex(FitContract.RecordData.TIME));
String date = dataCursor.getString(dataCursor.getColumnIndex(FitContract.RecordData.DATE));
String distance = dataCursor.getString(dataCursor.getColumnIndex(FitContract.RecordData.DISTANCE));

holder.time.setText(time);
holder.date.setText(date);
holder.distance.setText(distance);
}

@Override
public int getItemCount() {
return (dataCursor == null) ? 0 : dataCursor.getCount();
}
}

然后这是 fragment (你也可以使用一个 Activity )

public class RecordFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private RecyclerView recyclerView;
RecordAdapter adapter;
private int LOADER_ID = 1;

@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.record_layout, container, false);

recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view_record);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.fab_record);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("RECORD_FRAGMENT", "FAB");
startActivity(new Intent(getContext(), LineChartActivity.class));
}
});

adapter = new RecordAdapter(getActivity(), null);
recyclerView.setAdapter(adapter);
return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(LOADER_ID, null, this);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(getContext(), FitContract.RecordData.CONTENT_URI, null, null, null, null);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
}

关于android - 将 RecyclerView 与来自 LoaderManager 的数据一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32992239/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com