未连接适配器;跳过布局 只显示第一个内容

问题描述 投票:0回答:2

我是 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;
}
}

提前致谢!

android android-recyclerview cardview
2个回答
0
投票

在这里您可以进行以下更改以使其正确:

1)更新你的 onCreateView 方法

private RVAdapter rvAdapter;

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());

    // Added here
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    rv.setHasFixedSize(true); // Here we changed order you should call it first before setting LayoutManager
    rv.setLayoutManager(llm);    

    //Set empty adapter and pass only context if you will needed inside it
    rvAdapter = new RVAdapter(getActivity());
    rv.setAdapter(rvAdapter);

    initializeData();
    initializeAdapter();

    return rootView;
}

2)在适配器类中创建方法来更新内容列表

public void updateList(List<Heroi> herois){

   this.herois = herois;
   notifyDataSetChanged();

}

3)只在initializeAdapter方法中更新你的contentList(不需要再次设置适配器)

private void initializeAdapter(){

  rvAdapter.updateList(herois);

}

就是这样!

按照以下方式更新您的适配器:

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.HeroiViewHolder>{

List<Heroi> herois;

public static class HeroiViewHolder extends RecyclerView.ViewHolder{

    CardView cv;
    ImageView Icons;
    TextView Titulos;
    TextView Textos;
    Context context;


    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);
    }
}

RVAdapter(Context context){
    this.context = context;
}

@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 void updateList(List<Heroi> herois){

       this.herois = herois;
       notifyDataSetChanged();

  }

}

0
投票

问题解决了我刚刚删除了RecyclerView类并将其放入Fragment1类中。

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
   // return inflater.inflate(R.layout.frag1,container,false);
    View rootView = inflater.inflate(R.layout.frag1,container,false);
    rv = (RecyclerView)rootView.findViewById(R.id.rv_frag1);

    LinearLayoutManager llm = new LinearLayoutManager(getContext());
   // llm.setOrientation(LinearLayoutManager.VERTICAL);
   // rv.setHasFixedSize(true);
    rv.setLayoutManager(llm);

    initializeData();

    rvAdapter = new RVAdapter(herois);
    rv.setAdapter(rvAdapter);

  return rootView;
}

无论如何谢谢

© www.soinside.com 2019 - 2024. All rights reserved.