在导航栏中使用片段时不显示回收器视图

问题描述 投票:0回答:1

我在使用导航栏时遇到回收站视图不起作用的问题。

最初我编写了代码,将我的 4 个片段添加到导航栏配置中,导航工作正常,但回收器视图没有出现。

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications, R.id.navigation_account)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);

        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }

    }

}

在此之后,我找到了一个通过 FragmentTransaction 设置主页片段的解决方案:回收器视图开始工作,但导航当然不再起作用。

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        replaceFragment(new HomeFragment());

        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
    }

    private void replaceFragment(HomeFragment homeFragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.nav_host_fragment_activity_main, homeFragment);
        fragmentTransaction.commit();
    }
}

我不知道如何组合这段代码以使两个部分都能工作

适配器:

public class EventAdapter extends RecyclerView.Adapter<EventAdapter.ViewHolder> {

    Context context;
    ArrayList<EventData> eventsList;

    public EventAdapter(Context context, ArrayList<EventData> eventsList) {
        this.context = context;
        this.eventsList = eventsList;
    }

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

    @Override
    public void onBindViewHolder(@NonNull EventAdapter.ViewHolder holder, int position) {
        EventData event = eventsList.get(position);
        holder.eventName.setText(event.name);
        holder.eventDescription.setText(event.description);
        holder.eventDate.setText(event.date);
        holder.eventTime.setText(event.time);
        holder.eventLocation.setText(event.location);
    }

    @Override
    public int getItemCount() {
        return eventsList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView eventName;
        TextView eventDescription;
        TextView eventDate;
        TextView eventTime;
        TextView eventLocation;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            eventName = itemView.findViewById(R.id.eventName);
            eventDescription = itemView.findViewById(R.id.eventDescription);
            eventDate = itemView.findViewById(R.id.eventDate);
            eventTime = itemView.findViewById(R.id.eventTime);
            eventLocation = itemView.findViewById(R.id.eventLocation);
        }
    }
}

首页片段:

package com.example.inriga;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private ArrayList<EventData> eventsList;
    private RecyclerView recyclerView;

    public HomeFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

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

    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        dataInitialize();
        recyclerView = view.findViewById(R.id.eventsRecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setHasFixedSize(true);
        EventAdapter adapter = new EventAdapter(getContext(), eventsList);
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    private void dataInitialize() {
        eventsList = new ArrayList<>();
        eventsList.add(new EventData("Hokejs", "Latvija vs Kanāda", "01.02.2024", "18:00", "Riga"));
        eventsList.add(new EventData("Hokejs", "Latvija vs Kanāda", "01.02.2024", "18:00", "Riga"));
        eventsList.add(new EventData("Hokejs", "Latvija vs Kanāda", "01.02.2024", "18:00", "Riga"));
        eventsList.add(new EventData("Hokejs", "Latvija vs Kanāda", "01.02.2024", "18:00", "Riga"));
        eventsList.add(new EventData("Hokejs", "Latvija vs Kanāda", "01.02.2024", "18:00", "Riga"));
        eventsList.add(new EventData("Hokejs", "Latvija vs Kanāda", "01.02.2024", "18:00", "Riga"));
        eventsList.add(new EventData("Hokejs", "Latvija vs Kanāda", "01.02.2024", "18:00", "Riga"));
        eventsList.add(new EventData("Hokejs", "Latvija vs Kanāda", "01.02.2024", "18:00", "Riga"));
        eventsList.add(new EventData("Hokejs", "Latvija vs Kanāda", "01.02.2024", "18:00", "Riga"));
        eventsList.add(new EventData("Hokejs", "Latvija vs Kanāda", "01.02.2024", "18:00", "Riga"));
        eventsList.add(new EventData("Hokejs", "Latvija vs Kanāda", "01.02.2024", "18:00", "Riga"));
        eventsList.add(new EventData("Hokejs", "Latvija vs Kanāda", "01.02.2024", "18:00", "Riga"));
    }
}
java android android-recyclerview android-navigation
1个回答
0
投票

您面临着将导航组件与底部导航栏集成并在其中一个片段中显示回收器视图的挑战。让我们将这两个功能集成到您的主要活动中。

您可以这样做:

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }

        setupNavigation();
        replaceFragment(new HomeFragment());
    }

    private void setupNavigation() {
        BottomNavigationView navView = findViewById(R.id.nav_view);
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications, R.id.navigation_account)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);
    }

    private void replaceFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.nav_host_fragment_activity_main, fragment)
                .commit();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.