回收站视图在 Fragment 中不起作用
Posted
技术标签:
【中文标题】回收站视图在 Fragment 中不起作用【英文标题】:Recycler View Does not works in Fragment 【发布时间】:2021-04-16 13:21:16 【问题描述】:我一直在制作一个在Fragment
中使用recyclerView
的应用程序,但recyclerView
的内容尚未显示。它显示:
'No adapter attached; skipping layout'.
特色助手类:
public class FeaturedHelperClass
int image;
String title;
public FeaturedHelperClass(int image, String title)
this.image = image;
this.title = title;
public int getImage()
return image;
public String getTitle()
return title;
片段:
RecyclerView.Adapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
featuredRecycle = inflater.inflate(R.layout.fragment_dashboard, container, false).findViewById(R.id.explore_recycle);
recycle();
return inflater.inflate(R.layout.fragment_dashboard, container, false);
private void recycle()
featuredRecycle.setHasFixedSize(true);
featuredRecycle.setLayoutManager(new LinearLayoutManager(getActivity()));
ArrayList<FeaturedHelperClass> featuredLocations = new ArrayList<>();
featuredLocations.add(new FeaturedHelperClass(R.drawable.mcdonald, "Mcdonald's"));
featuredLocations.add(new FeaturedHelperClass(R.drawable.kfc, "KFC"));
featuredLocations.add(new FeaturedHelperClass(R.drawable.starbucks, "Starbucks"));
adapter = new FeaturedAdapter(featuredLocations);
featuredRecycle.setAdapter(adapter);
【问题讨论】:
您给定的数据和代码是不够的。发布您的 fragment_dashboard.xml。并解释为什么你的特点回收和你的片段分开膨胀? 【参考方案1】:未附加视图膨胀回收器视图适配器后出现'No adapter attached; skipping layout'.
的原因。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
featuredRecycle = inflater.inflate(R.layout.fragment_dashboard, container, false).findViewById(R.id.explore_recycle);
recycle();
return inflater.inflate(R.layout.fragment_dashboard, container, false);
因为 recycle() 方法会在视图膨胀之前执行。
我们可以把它改成
//inflate the view and return
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
return view;
//this callback is just after once the view get inflated in the screen
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
featuredRecycle = view.findViewById(R.id.explore_recycle);
recycle();
现在我们得到featuredRecycle
视图并执行recycle() 逻辑,将绘制recycler。
【讨论】:
【参考方案2】:您的通货膨胀逻辑似乎有缺陷,请尝试在您的 onCreateView
中更改以下内容
featuredRecycle = inflater.inflate(R.layout.fragment_dashboard, container, false).findViewById(R.id.explore_recycle);
recycle();
return inflater.inflate(R.layout.fragment_dashboard, container, false);
进入
View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
featuredRecycle = view.findViewById(R.id.explore_recycle);
recycle();
return view;
【讨论】:
【参考方案3】:以下内容对您有用,但它不是延迟片段中视图膨胀的好方法。由于您在返回视图之前执行了一些任务,因此会延迟视图膨胀。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
featuredRecycle = view.findViewById(R.id.explore_recycle);
recycle()
return view;
执行以下操作将是最佳方法,
//inflate the view and return
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_dashboard, container, false);
return view;
//this callback is just after the view inflation, you set your ui here
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
featuredRecycle = view.findViewById(R.id.explore_recycle);
recycle()
【讨论】:
代替getView(),我们可以使用视图本身。基本上,两者都只会给出膨胀的视图。以上是关于回收站视图在 Fragment 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章
Webview 在 Fragment Android Studio 中不起作用
DiffUtil 在嵌套的 recyclerview Kotlin 中不起作用
ViewModelProviders 在我的 Fragment 中不起作用
Constraintlayout Vertical Bias 在Android的两个视图中不起作用