问题
I'm using navigation component with bottom navigation view in my app. Problem was when I return back to top-level destinations from child destination, recyclerView not showing any elements.
nav_graph_main:
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph_main"
app:startDestination="@id/navigation_stats">
<fragment
android:id="@id/navigation_stats"
android:name="com.example.view.fragments.StatsFragment"
android:label="@string/statistika"
tools:layout="@layout/fragment_stats" />
<fragment
android:id="@+id/navigation_customers"
android:name="com.example.view.fragments.CustomersFragment"
android:label="@string/alyjylar"
tools:layout="@layout/fragment_customers" />
<fragment
android:id="@+id/navigation_menus"
android:name="com.example.view.fragments.MenusFragment"
android:label="@string/bolumler"
tools:layout="@layout/fragment_menus">
<action
android:id="@+id/action_navigation_menus_to_navigation_foods"
app:destination="@id/navigation_foods"
app:launchSingleTop="true"/>
</fragment>
<fragment
android:id="@+id/navigation_foods"
android:name="com.example.view.fragments.FoodsFragment"
android:label="@string/menular"
tools:layout="@layout/fragment_foods">
</fragment>
</navigation>
mainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
BottomNavigationView bottomNavigationView = findViewById(R.id.nav_view);
navController = Navigation.findNavController(this, R.id.nav_host_fragment);
AppBarConfiguration appBarConfiguration =
new AppBarConfiguration.Builder(R.id.navigation_customers, R.id.navigation_stats, R.id.navigation_menus).build();
NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
}
adapter:
public class MenusAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Context mContext;
private List<Menus> mList;
private static final int TYPE_ITEM = 0;
private static final int TYPE_PROGRESS = 1;
private View.OnClickListener mOnItemClickListener;
public MenusAdapter(Context mContext, List<Menus> mList) {
this.mContext = mContext;
this.mList = mList;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_menu, parent, false);
return new ItemViewHolder(view);
} else if (viewType == TYPE_PROGRESS) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_loading, parent, false);
return new LoadViewHolder(view);
}
throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ItemViewHolder) {
final ItemViewHolder itemVH = (ItemViewHolder) holder;
final Menus data = mList.get(position);
itemVH.textViewMenuName.setText(data.getName());
String path = Utils.menu_small + data.getImage();
Picasso.get().load(path).into(itemVH.imageViewMenu);
}
}
public void setOnItemClickListener(View.OnClickListener itemClickListener) {
mOnItemClickListener = itemClickListener;
}
@Override
public int getItemCount() {
return mList.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return mList.get(position) != null ? TYPE_ITEM : TYPE_PROGRESS;
}
private class ItemViewHolder extends RecyclerView.ViewHolder {
TextView textViewMenuName;
ImageView imageViewMenu;
ItemViewHolder(View itemView) {
super(itemView);
itemView.setTag(this);
itemView.setOnClickListener(mOnItemClickListener);
textViewMenuName = itemView.findViewById(R.id.textViewFoodName);
imageViewMenu = itemView.findViewById(R.id.imageViewFood);
}
}
class LoadViewHolder extends RecyclerView.ViewHolder {
LoadViewHolder(View v) {
super(v);
}
}
}
MenusFragment:
public class MenusFragment extends Fragment {
private RecyclerView recyclerView;
private ProgressBar progressBar;
private MenusAdapter mAdapter;
private List<Menus> mResMenus = new ArrayList<>();
private MainActivityViewModel mViewModel;
private View.OnClickListener onItemClickListener = view -> {
RecyclerView.ViewHolder viewHolder = (RecyclerView.ViewHolder) view.getTag();
int position = viewHolder.getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
Menus thisItem = mResMenus.get(position);
Bundle bundle = new Bundle();
bundle.putInt("menu_id", thisItem.getMenuId());
Navigation.findNavController(view).navigate(R.id.action_navigation_menus_to_navigation_foods, bundle);
}
};
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initialize(view);
observeData();
}
@Override
public void onResume() {
super.onResume();
if (mViewModel != null)
mViewModel.reloadMenu();
}
private void initialize(View view) {
recyclerView = view.findViewById(R.id.recyclerView);
mViewModel = ViewModelProviders.of(Objects.requireNonNull(getActivity())).get(MainActivityViewModel.class);
if (mAdapter == null) {
mAdapter = new MenusAdapter(getContext(), mResMenus);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
recyclerView.setAdapter(mAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setNestedScrollingEnabled(true);
recyclerView.setHasFixedSize(true);
mAdapter.setOnItemClickListener(onItemClickListener);
} else {
mAdapter.notifyDataSetChanged();
}
}
private void observeData() {
List<Menus> list = new ArrayList<>();
mViewModel.getMenus().removeObservers(this);
mViewModel.getMenus().observe(this, response -> {
if (response != null) {
list.addAll(response.getMenus());
for (Menus it : list) {
if (it.getMenuId() != 0)
mResMenus.add(it);
}
mAdapter.notifyDataSetChanged();
}
});
}
}
I am using viewModel in MenusFragment and when select any item opens FoodsFragment. When return back MenusFragment's recyclerview shows empty item. When debugging all functions working but recyclerview is empty. Where is the problem?
来源:https://stackoverflow.com/questions/56952180/recyclerview-shows-nothing-when-navigating-back-navigation-component