【发布时间】:2021-03-23 18:33:35
【问题描述】:
上下文 大家好。我遇到了问题,我到处寻找,但我就是不明白。我正在关注关于 RecycleView 的 youtube 教程(流中编码),但是,我来自关于使用片段设置 MainActivity 的教程,所以我试图在片段中执行此操作。在 cmets 中,人们也遇到过类似的问题,他回答说基本上是一样的,只是在片段内部应该使用 getView() 但这不起作用。
到目前为止我为解决问题所做的工作 我一直在查看 SO 线程(即使是来自同一个视频的线程),但我只是不明白,他们的解决方案都不适合我,或者他们得到的响应(如果有的话)非常模糊和非有帮助。我还从该 youtube 视频中搜索了所有 300 多个 cmets,并查找了其他教程以查看是否有解决方案。与我遇到的问题无关,只是为了说明我不是出于懒惰才输入这个的。我花了一整天的时间来解决这个问题,但我没有什么可以展示的
我需要代码做什么 我需要代码按照找到的教程here 运行。我不相信 onCreateView 是我的代码的最佳位置,因为在运行之前肯定不会创建任何视图,因此 findViewById 是不可能的,但我现在能做什么?我可以把那个代码放在哪里让它运行。
我的代码
照片片段
public class PhotosFragment extends Fragment {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_photos, container, false);
ArrayList<ExampleItem> exampleList = new ArrayList<>();
exampleList.add(new ExampleItem(R.drawable.ic_baseline_one, "First Image Title", "Description"));
exampleList.add(new ExampleItem(R.drawable.ic_baseline_two, "Second Image Title", "Description"));
exampleList.add(new ExampleItem(R.drawable.ic_baseline_three, "Third Image Title", "Description"));
mRecyclerView = findViewByActivity(R.id.recyclerView2);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(super.getContext());
mAdapter = new ExampleAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
}
我会根据需要提供其他任何东西。我比你知道的更感激任何帮助。这彻底打败了我。
【问题讨论】:
-
@DanielNugent 你知道任何关于如何覆盖 onViewCreated() 的教程。我试图根据谷歌结果来做,但我不确定它是否正确。 pastebin.com/ZsJ4iEVe
标签: android android-layout android-fragments android-recyclerview