没有适配器;跳过布局仅显示第一个内容

时间:2016-10-06 12:52:32

标签: android android-recyclerview cardview

我是android编程的新手,我收到错误“没有附加适配器;跳过布局”。我的应用运行但是当我点击打开我的片段时,里面有一个cardview,只显示我的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;
}
} 

RecyclerView

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

类Heroe

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

提前致谢!

2 个答案:

答案 0 :(得分: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();

  }

}

答案 1 :(得分:0)

解决问题我刚刚删除了RecyclerView Class并将其推入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;
}

非常感谢