我是 android 编程新手,我收到错误“未连接适配器;跳过布局”。我的应用程序运行但是当我点击打开我的片段时,里面有一个卡片视图,只显示我的 ArrayList 的第一个内容。我一直在关注本教程:https ://code.tutsplus.com/tutorials/getting-started-with-recyclerview-and-cardview-on-android--cms-23465 ,但我以不同的方式做。我使用的是片段而不是活动,但我认为这不是问题。所以,在这里我有我的活动:
public class Av_ninja extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_av_ninja);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomAdapter(getSupportFragmentManager(), getApplicationContext()));
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private class CustomAdapter extends FragmentPagerAdapter{
private String fragmentes [] = {"Info","Abilitities"};
public CustomAdapter(FragmentManager suppFragmentManager, Context applicaContext){
super(suppFragmentManager);
}
@Override
public Fragment getItem(int position){
switch (position){
case 0:
return new Fragment1();
case 1:
return new Fragment2();
default:
return null;
}
}
@Override
public int getCount(){
return fragmentes.length;
}
@Override
public CharSequence getPageTitle(int position) {
return fragmentes[position];
}
}
}
分段
public class Fragment1 extends Fragment {
ImageView Icons;
TextView Titulos;
TextView Textos;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
// return inflater.inflate(R.layout.frag1,container,false);
View rootView = inflater.inflate(R.layout.card_ninja_info,container,false);
Icons = (ImageView) rootView.findViewById(R.id.Icons);
Titulos = (TextView) rootView.findViewById(R.id.Titulos);
Textos = (TextView) rootView.findViewById(R.id.Textos);
Icons.setImageResource(R.drawable.offenseicon);
Titulos.setText("Role");
Textos.setText("Scout");
return rootView;
}
}
回收站视图
public class RecyclerViewAv extends Fragment{
private List<Heroi> herois;
private RecyclerView rv;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//return inflater.inflate(R.layout.frag2,container,false);
View rootView = inflater.inflate(R.layout.card_ninja_info,container,false);
rv = (RecyclerView)rootView.findViewById(R.id.rv_frag1);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
rv.setHasFixedSize(true);
initializeData();
initializeAdapter();
return rootView;
}
private void initializeData(){
herois = new ArrayList<>();
herois.add(new Heroi(R.drawable.offenseicon, "Role", "Scout"));
herois.add(new Heroi(R.drawable.identityicon, "Identity", "Genji Shimada"));
herois.add(new Heroi(R.drawable.workicon, "Work", "Adventurer"));
herois.add(new Heroi(R.drawable.localizationicon, "Base of Operation", "Nepal"));
herois.add(new Heroi(R.drawable.empresaicon, "Affiliation", "Shiamda Clan"));
herois.add(new Heroi(R.drawable.ageicon, "Age", "27"));
herois.add(new Heroi(R.drawable.healthicon, "Health", "100"));
}
private void initializeAdapter(){
RVAdapter adapter = new RVAdapter(herois);
rv.setAdapter(adapter);
}
}
适配器
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.HeroiViewHolder>{
public static class HeroiViewHolder extends RecyclerView.ViewHolder{
CardView cv;
ImageView Icons;
TextView Titulos;
TextView Textos;
public HeroiViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
Icons = (ImageView)itemView.findViewById(R.id.Icons);
Titulos = (TextView)itemView.findViewById(R.id.Titulos);
Textos = (TextView)itemView.findViewById(R.id.Textos);
}
}
List<Heroi> herois;
RVAdapter(List<Heroi> herois){
this.herois=herois;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView){
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public RVAdapter.HeroiViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
HeroiViewHolder hvh = new HeroiViewHolder(v);
return hvh;
}
@Override
public void onBindViewHolder(HeroiViewHolder holder, int position) {
holder.Icons.setImageResource(herois.get(position).photoId);
holder.Titulos.setText(herois.get(position).tit);
holder.Textos.setText(herois.get(position).res);
}
@Override
public int getItemCount() {
return herois.size();
}
}
类英雄
public class Heroi {
int photoId;
String tit;
String res;
public Heroi(int photoId, String tit, String res) {
this.photoId = photoId;
this.tit = tit;
this.res = res;
}
}
提前致谢!