我正在尝试创建循环视图,我也在我的应用中使用标签布局。我一步一步地按照教程,但我不知道为什么它会崩溃。这是代码。我猜这个部分有问题:
hostname -s
每当我更改返回0时,应用程序都会运行,但@Override
public int getItemCount() {
return herosList.size();
}
recycleview
RecycleAdapter.java
public class Heros {
public String name;
public int img;
}
答案 0 :(得分:0)
尝试更改
manager=new LinearLayoutManager(G.context);
要
manager=new LinearLayoutManager(getActivity());
答案 1 :(得分:0)
检查它可能会对你有所帮助 public class PageFragment extends Fragment { public static final String ARG_PAGE =" ARG_PAGE";
private int mPage;
RecyclerView recyclerView;
LinearLayoutManager manager;
String[] names={"abs","vline","chest"};
ArrayList<Heros> passName;
RecycleAdapter adapter;
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
passName=new ArrayList<>();
View view = inflater.inflate(R.layout.fragment_page, container,
false);
recyclerView=(RecyclerView)view.findViewById(R.id.rec);
manager=new LinearLayoutManager(G.context);
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
for (int i=0;i<3;i++){
Heros heros=new Heros();
heros.name=names[i];
passName.add(heros);
}
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayoutManager);
adapter = new RecycleAdapter(this,passName);
recyclerView.setAdapter(adapter);
return view;
}
}
答案 2 :(得分:0)
当你在getItemCount()
中返回0时,它表示RecyclerView中有0个项目。这就是你无法在RecyclerView
中看到任何内容的原因。
然而,您遇到的问题可能是由于适配器接收到错误的数据......