gpt4 book ai didi

java - RecyclerView 需要时间在 Fragment 中加载 Adapter 值

转载 作者:行者123 更新时间:2023-12-01 19:30:05 26 4
gpt4 key购买 nike

我有一个带有 RecyclerView 的 fragment (HealFragment),它的适配器是一个不同的类,通过 FirestoreRecyclerAdapter 从 Firestore 检索数据,并且运行良好。但每当我切换到这个 HealFragment 时,它在尝试将适配器设置为 RecyclerView 时都会有点卡顿(减慢速度)。发生这种情况是因为我在 onStart() 方法中设置了 RecyclerView 的适配器。

So my question is, how do I avoid this stuttering when the Fragment is loading the Firestore data with the Adapter class?

How can I open the Fragment without stutters just with a ProgressBar and let the app get the Adapter data at it's own pace?

我曾考虑过在 HealFragment 类中实现 AsyncTask,但听说 Firestore 已经是异步的了。

正如您在 HealFragment 类的 onStart() 方法中看到的那样,我的临时解决方案是在将 Adapter 附加到 RecyclerView 之前添加 0.5 秒的延迟(Handler)。但我希望有更完整的方法来实现这一点。

上下文:

  • 治愈 fragment

public class HealFragment extends Fragment {
private View mView;
private ProgressBar heFrProgressBar;
private RecyclerView heFrRecyclerView;

private HealAdapter healAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mView = inflater.inflate(R.layout.fragment_heal, container, false);

initializeViews();

return mView;
}

//<--- Method to initialize and assign the views | Called in: onCreateView() --->
private void initializeViews() {

//Loading Firestore Data Process
final String mCurrentUser = mAuth.getCurrentUser().getUid();
Query query = db.collection("User").document(currentUser).collection("Heals").orderBy("healColor");
FirestoreRecyclerOptions options = new FirestoreRecyclerOptions.Builder<NewHeal>()
.setQuery(query, NewHeal.class)
.build();

healAdapter = new HealAdapter(options);
heFrRecyclerView.setHasFixedSize(true);
heFrRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
heFrRecyclerView.setAdapter(healAdapter);

}

@Override
public void onStart() {
super.onStart();

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//** Actions to do after 0.5 second **
healAdapter.startListening();
heFrProgressBar.setVisibility(View.INVISIBLE);
}
}, 500);
}

@Override
public void onStop() {
super.onStop();

healAdapter.stopListening();
}
}
  • HealAdapter
public class HealAdapter extends FirestoreRecyclerAdapter<NewHeal, HealAdapter.HealHolder> {

public HealAdapter(@NonNull FirestoreRecyclerOptions<NewHeal> options) {
super(options);
}

@Override
protected void onBindViewHolder(@NonNull final HealHolder holder, final int position, @NonNull final NewHeal model) {
holder.setTaskTitle(model.getTaskTitle());
//**Several setters like the one above**

}

@NonNull
@Override
public HealHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_heal, parent, false);
return new HealHolder(v);
}

class HealHolder extends RecyclerView.ViewHolder {

//**Declaring Variables**

HealHolder(@NonNull View itemView) {
super(itemView);
this.mView = itemView;

//**initializingViews + settingOnClickListeners + loadingSomeMethods**
}

private void setTaskTitle(String xTitle) {
singleHealTitleText.setText(xTitle);
}

//**Several setter methods more like the one above(setTaskTitle)**
}

}

最佳答案

不需要 AsyncTask,已弃用,正如您所说,Firebase 是异步的。

您应该做的是创建一个界面,在进行获取过程时通知您,然后显示一个 progressBar 直到您有数据要显示。

您可以将监听器附加到查询以了解查询何时完成,并在获取数据之前显示 progressBar

您可以将监听器附加到查询对象,获取数据后,您可以隐藏 progressBar

query.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
// data has been fetched, hide progress and update adapter data
heFrProgressBar.setVisibility(View.GONE);
// set the new data to the adapter
healAdapter.notifyDataSetChanged();
}
});





@Override
public void onStart() {
super.onStart();
healAdapter.startListening();
}

您不需要使用带有延迟的处理程序,这被认为是一种不好的做法,您需要做的是将监听器附加到异步查询,一旦获取数据,您需要隐藏 progressBar,更新适配器中的数据并通知数据已更改

关于java - RecyclerView 需要时间在 Fragment 中加载 Adapter 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60021668/

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