gpt4 book ai didi

android - 从其他 fragment 返回时, fragment 内的 Recyclerview 为空

转载 作者:行者123 更新时间:2023-12-05 00:16:38 28 4
gpt4 key购买 nike

我正在开发一个包含 fragment 的应用(一个只有 Activity 但有多个 fragment 的应用)。

我遇到的问题是,当我转到一个 fragment 并返回(系统后退按钮)到初始 fragment 时,它的 RecycleView 现在是空的。

奇怪的是,当我从不同的 fragment (也使用 RecycleView)访问相同的 fragment 时,问题不会发生。

下面我发布了 RecycleView 和 MainActivity 的两个 fragment 。

有人知道为什么会这样吗?

确实有问题的 fragment :

public class FavEventFragment extends Fragment implements RecyclerView.OnItemTouchListener {

private static final String PAGE_NAME = "Saved Events";

private TextView noEventsTextView;

FavEventAdapter adapter;
RecyclerView recyclerView;
private EventViewModel eventViewModel;
private List<Event> favEvents;

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

MainActivity.changeLayoutWeight(true);
MainActivity.setPageName(PAGE_NAME);

favEvents = new ArrayList<>();

recyclerView = rootView.findViewById(R.id.favEvent_list);
noEventsTextView = rootView.findViewById(R.id.noFavEventsText);

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

eventViewModel = ViewModelProviders.of(this).get(EventViewModel.class);
eventViewModel.getFavEvents().observe(this, new Observer<List<Event>>() {

@Override
public void onChanged(@Nullable List<Event> games) {
favEvents = games;
updateUI();
}
});

setRecycleViewGestures();

return rootView;
}

private void updateUI() {
if (adapter == null) {
adapter = new FavEventAdapter(favEvents);
recyclerView.setAdapter(adapter);
} else {
adapter.swapList(favEvents);
}
}

void setRecycleViewGestures() {

recyclerView.addOnItemTouchListener(new RecycleGestures(getActivity(), recyclerView, new RecycleGestures.ClickListener() {
@Override
public void onClick(View view, int position) {

EventDetailsFragment fragment = new EventDetailsFragment();

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();

transaction.setCustomAnimations(R.anim.enter_from_top, R.anim.exit_to_top, R.anim.enter_from_top, R.anim.exit_to_top);

fragment.setObject(favEvents.get(position));
transaction.addToBackStack(null);
transaction.replace(R.id.fragment_container, fragment, "FRAGMENT").commit();
}

@Override
public void onLongClick(View view, int position) { }
}));

ItemTouchHelper.SimpleCallback simpleItemTouchCallback =
new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
int position = (viewHolder.getAdapterPosition());
eventViewModel.delete(favEvents.get(position));
favEvents.remove(position);
adapter.notifyItemRemoved(position);
}
};

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
itemTouchHelper.attachToRecyclerView(recyclerView);
recyclerView.addOnItemTouchListener(this);
}

@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) { return false; }

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) { }

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { }
}```


The fragment that **doesn't** have the problem:


public class EventsFragment extends Fragment {

private static final String PAGE_NAME = "Upcoming Events";

private RecyclerView eventList;
private EventAdapter adapter;

private AirsoftRepository airsoftRepository;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_shop, container, false);

MainActivity.changeLayoutWeight(true);
MainActivity.setPageName(PAGE_NAME);

airsoftRepository = AirsoftRepository.getInstance();

eventList = rootView.findViewById(R.id.item_list);

eventList.setLayoutManager(new LinearLayoutManager(getActivity()));

getEvents();

return rootView;
}

private void getEvents() {
airsoftRepository.getEvents(new OnGetEventsCallback() {
@Override
public void onSuccess(List<Event> events) {
adapter = new EventAdapter(events, onClickCallback);
eventList.setAdapter(adapter);
}

@Override
public void onError() {
Toast.makeText(getActivity(), "Please check your internet connection.", Toast.LENGTH_SHORT).show();
}
});
}

OnEventsClickCallback onClickCallback = new OnEventsClickCallback() {
@Override
public void onClick(Event event) {

EventDetailsFragment fragment = new EventDetailsFragment();

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();

transaction.setCustomAnimations(R.anim.enter_from_top, R.anim.exit_to_top, R.anim.enter_from_top, R.anim.exit_to_top);

fragment.setObject(event);
transaction.addToBackStack(null);
transaction.replace(R.id.fragment_container, fragment, "FRAGMENT").commit();
}
};
}

最佳答案

替换 fragment 会导致其 View 层次结构被破坏。当前,您在重新进入 fragment 时正在泄漏适配器。

将覆盖添加到您的 fragment 以清理当前适配器和查看引用:

@Override
public void onDestroyView(){
recyclerView.setAdapter(null);
adapter = null;
recyclerView = null;
}

理想情况下,您的适配器应该在 onCreate 期间创建,并且无论 View 状态如何都有效。

关于android - 从其他 fragment 返回时, fragment 内的 Recyclerview 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56305568/

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