我试图findViewById以便将项目添加到回收站视图。我的代码如下。无论如何可以告诉我以下原因为何不起作用。

我确实有一个示例回收者视图,该视图应该出现在列表中,但事实并非如此。

感觉我缺少一些非常明显的东西

我的片段:

public class HolidayFragment extends Fragment {

    private HolidayViewModel holidayViewModel;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        holidayViewModel =
                ViewModelProviders.of(this).get(HolidayViewModel.class);
        View root = inflater.inflate(R.layout.fragment_holiday, container, false);
        FloatingActionButton fab = getActivity().findViewById (R.id.fab);
        fab.setImageResource (R.drawable.ic_add_black_24dp);
        fab.setOnClickListener (new View.OnClickListener() {
            @Override
            public void onClick (View view) {
                Snackbar.make (view , "Holiday Fragment Action Button", Snackbar.LENGTH_LONG)
                        .setAction (" Action ", null ).show();
            }
        });
    RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.recyclerview);
    final HolidayListAdapter adapter = new HolidayListAdapter(getActivity());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        return root;
    }
}


MyListAdapter:

public class HolidayListAdapter extends RecyclerView.Adapter<HolidayListAdapter.HolidayViewHolder> {

    private final LayoutInflater mInflater;
    private List<Holiday> mHolidays; // Cached copy of words

    HolidayListAdapter(Context context) { mInflater = LayoutInflater.from(context); }

    @Override
    public HolidayViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.recyclerview_item, parent, false);
        return new HolidayViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(HolidayViewHolder holder, int position) {
        if (mHolidays != null) {
            Holiday current = mHolidays.get(position);
            holder.holidayItemView.setText(current.getName());
        } else {
            // Covers the case of data not being ready yet.
            holder.holidayItemView.setText("No Holiday");
        }
    }

    void setHolidays(List<Holiday> holidays){
        mHolidays = holidays;
        notifyDataSetChanged();
    }

    // getItemCount() is called many times, and when it is first called,
    // mWords has not been updated (means initially, it's null, and we can't return null).
    @Override
    public int getItemCount() {
        if (mHolidays != null)
            return mHolidays.size();
        else return 0;
    }

    class HolidayViewHolder extends RecyclerView.ViewHolder {
        private final TextView holidayItemView;

        private HolidayViewHolder(View itemView) {
            super(itemView);
            holidayItemView = itemView.findViewById(R.id.textView);
        }
    }
}


我的主要活动:

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_holiday, R.id.nav_gallery, R.id.nav_slideshow,
                R.id.nav_tools, R.id.nav_share, R.id.nav_send)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}

最佳答案

我发现了我的问题!我很想念二传手,哎呀:(

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

    View root = inflater.inflate(R.layout.fragment_holiday, container, false);
    RecyclerView recyclerView = (RecyclerView) root.findViewById(R.id.recyclerview);
    final HolidayListAdapter adapter = new HolidayListAdapter(getActivity());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    holidayViewModel =
            ViewModelProviders.of(this).get(HolidayViewModel.class);

    holidayViewModel.getAllHolidays().observe(this, new Observer<List<Holiday>>() {
        @Override
        public void onChanged(@Nullable final List<Holiday> holidays) {
            // Update the cached copy of the words in the adapter.
            adapter.setHolidays(holidays);
        }
    });


我希望这可以帮助别人!

关于java - 在 fragment 中显示RecyclerView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60201157/

10-10 22:19