gpt4 book ai didi

java - 在 fragment 中显示 RecyclerView

转载 作者:行者123 更新时间:2023-12-01 18:28:14 24 4
gpt4 key购买 nike

我正在尝试 findViewById 以便将项目添加到回收器 View 中。我的代码如下。无论如何可以告诉我为什么以下不起作用。

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

感觉我错过了一些非常明显的东西

我的 fragment :

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;
}
}

我的列表适配器:

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);
}
}
}

我的主要 Activity :

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/

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