1

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

提前致谢!

4

2 回答 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();

  }

}
于 2016-10-06T13:16:45.710 回答
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;
}

不管怎么说,还是要谢谢你

于 2016-10-06T21:15:04.507 回答