gpt4 book ai didi

java - fragment 中的 Recyclerview,跳过未附加适配器的布局

转载 作者:行者123 更新时间:2023-12-02 09:33:33 24 4
gpt4 key购买 nike

我正在尝试设置 Recyclerview,但在 initRecycleView() 上不断收到错误,提示没有连接适配器正在跳过布局。

我尝试为 RecyclerView 调用一个新的构造函数

RecyclerView recyclerView = new RecyclerView(getContext());

这将导致跳过布局,但如果我删除它并尝试这个

RecyclerView recyclerView = getView().findViewById(R.id.recyclerView);

这将导致调用虚拟方法空对象引用

public class HomeFragment extends Fragment {

private static final String TAG = "HomeFragment";

//Vars
private ArrayList<String> mImage = new ArrayList<>();
private ArrayList<String> mTitle = new ArrayList<>();
private ArrayList<String> mDesc = new ArrayList<>();

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

Log.d(TAG, "onCreateView: started");

initBitmapImages();
return inflater.inflate(R.layout.fragment_home, container, false);
}

private void initBitmapImages(){
Log.d(TAG, "initBitmapImages: preparing bitmaps");

mImage.add("https://i.redd.it/j6myfqglup501.jpg");
mTitle.add("Foto 1");
mDesc.add("This is foto 1");

mImage.add("https://i.redd.it/j6myfqglup501.jpg");
mTitle.add("Foto 2");
mDesc.add("This is foto 2");

mImage.add("https://i.redd.it/j6myfqglup501.jpg");
mTitle.add("Foto 3");
mDesc.add("This is foto 3");

mImage.add("https://i.redd.it/j6myfqglup501.jpg");
mTitle.add("Foto 4");
mDesc.add("This is foto 4");

mImage.add("https://i.redd.it/j6myfqglup501.jpg");
mTitle.add("Foto 5");
mDesc.add("This is foto 5");

mImage.add("https://i.redd.it/j6myfqglup501.jpg");
mTitle.add("Foto 6");
mDesc.add("This is foto 6");

mImage.add("https://i.redd.it/j6myfqglup501.jpg");
mTitle.add("Foto 7");
mDesc.add("This is foto 7");

mImage.add("https://i.redd.it/j6myfqglup501.jpg");
mTitle.add("Foto 8");
mDesc.add("This is foto 8");

initRecyclerView();
}

private void initRecyclerView(){
Log.d(TAG, "initRecyclerView: init recyclerView");

RecyclerView recyclerView = getView().findViewById(R.id.recyclerView);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(mImage,mTitle,mDesc,getContext());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
}

我期望的实际输出是在布局中我可以输出我创建的回收 View :(

最佳答案

尝试一下可能会有所帮助

public class HomeFragment extends Fragment {

private static final String TAG = "HomeFragment";

//Vars
private ArrayList<String> mImage = new ArrayList<>();
private ArrayList<String> mTitle = new ArrayList<>();
private ArrayList<String> mDesc = new ArrayList<>();

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

// initialize container view
View view = inflater.inflate(R.layout.fragment_home, container, false);

// pass container view to bitmap images so that will use to initialize recyclerView
initBitmapImages(view);

return view;
}



private void initBitmapImages(View view){
Log.d(TAG, "initBitmapImages: preparing bitmaps");

mImage.add("https://i.redd.it/j6myfqglup501.jpg");
mTitle.add("Foto 1");
mDesc.add("This is foto 1");

mImage.add("https://i.redd.it/j6myfqglup501.jpg");
mTitle.add("Foto 2");
mDesc.add("This is foto 2");

mImage.add("https://i.redd.it/j6myfqglup501.jpg");
mTitle.add("Foto 3");
mDesc.add("This is foto 3");

mImage.add("https://i.redd.it/j6myfqglup501.jpg");
mTitle.add("Foto 4");
mDesc.add("This is foto 4");

mImage.add("https://i.redd.it/j6myfqglup501.jpg");
mTitle.add("Foto 5");
mDesc.add("This is foto 5");

mImage.add("https://i.redd.it/j6myfqglup501.jpg");
mTitle.add("Foto 6");
mDesc.add("This is foto 6");

mImage.add("https://i.redd.it/j6myfqglup501.jpg");
mTitle.add("Foto 7");
mDesc.add("This is foto 7");

mImage.add("https://i.redd.it/j6myfqglup501.jpg");
mTitle.add("Foto 8");
mDesc.add("This is foto 8");

// container view uses to initialize recycle view
initRecyclerView(view);
}
private void initRecyclerView(View view){
Log.d(TAG, "initRecyclerView: init recyclerView");

// Initialize recycle view which exist in parent container view so it found and initialize
RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(mImage,mTitle,mDesc,getContext());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
}
}

关于java - fragment 中的 Recyclerview,跳过未附加适配器的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57747449/

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