Recycler View 在 Fragment 中不起作用
Recycler View Does not works in Fragment
我一直在制作一个在 Fragment
中使用 recyclerView
的应用程序,但 recyclerView
的内容没有显示出来。它显示:
'No adapter attached; skipping layout'.
特色助手Class:
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);
}
您的 inflation 逻辑似乎有问题,请尝试更改 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;
下面的方法对您有用,但延迟片段中视图的 inflation 并不是一个好方法。由于您在返回视图之前正在执行一些任务,因此它会延迟视图 inflation.
@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()
}
未附加视图 inflation 回收器视图适配器后 '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()方法会在view的inflation之前执行。
我们可以把它改成
//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() 逻辑,将绘制回收器。
我一直在制作一个在 Fragment
中使用 recyclerView
的应用程序,但 recyclerView
的内容没有显示出来。它显示:
'No adapter attached; skipping layout'.
特色助手Class:
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);
}
您的 inflation 逻辑似乎有问题,请尝试更改 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;
下面的方法对您有用,但延迟片段中视图的 inflation 并不是一个好方法。由于您在返回视图之前正在执行一些任务,因此它会延迟视图 inflation.
@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()
}
未附加视图 inflation 回收器视图适配器后 '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()方法会在view的inflation之前执行。
我们可以把它改成
//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() 逻辑,将绘制回收器。