在适配器

时间:2017-11-19 08:58:23

标签: android android-recyclerview adapter

这可能有点令人困惑,但请留在那里并留下评论以澄清。我在适配器中使用RecyclerView。这是因为我希望ListView中有多行可以水平滚动。行数完全正常,但每行中的项目数是错误的。第一行显示正确的行数,但之后的行显示每行中的多个项目,当我只需要一行时。第1行显示1项(这是我想要的),第2行显示2项,第3行显示3项等。这是我的片段文件调用第一个适配器:

public class featured_fragment extends android.support.v4.app.Fragment {

    public  ArrayList<featured_post> featured_post_list = new ArrayList<>();
    private RecyclerView recyclerView;

    public featured_fragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {



        View view = inflater.inflate(R.layout.featured_activity, container, false);
        ListView listView = view.findViewById(R.id.featured_list);
        featuredAdapter adapter = new featuredAdapter(getContext(),featured_post_list);
        listView.setAdapter(adapter);

        //Row 1
        featured_post post = new featured_post();
        adapter.add(post);

        //Row 2
        featured_post post1 = new featured_post();
        adapter.add(post1);

        //Row 3
        featured_post post2 = new featured_post();
        adapter.add(post2);

        return view;

    }
}

我的第一个调用回收器适配器的适配器:

public class featuredAdapter extends ArrayAdapter<featured_post>{

private List<featured_post> featured_postList = new ArrayList<>();
private RecyclerView recyclerView;
private recycler_adapter adapter;
private Context context;



public featuredAdapter(Context context, ArrayList<featured_post> users){

    super(context,0,users);
    this.context=context;

}

public View getView(int position, View convertView, ViewGroup parent) {


    featured_post post_item = getItem(position);

    if(convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.featured_fragment_activity, parent, false);


    recyclerView = convertView.findViewById(R.id.featured_recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(context));

    adapter = new recycler_adapter(context,featured_postList);
    recyclerView.setAdapter(adapter);

    featured_post post = new featured_post(Integer.toString(position));

    featured_postList.add(post);
    }
  return convertView
  }
}

然后,最后一个名为recycler_adapter的适配器(请原谅这个适配器,创建它时我很困惑):

public class recycler_adapter extends RecyclerView.Adapter<recycler_adapter.MyViewHolder> {
private List<featured_post> featuredList;
private Context context;

public class MyViewHolder extends RecyclerView.ViewHolder {

    public TextView text;
    public MyViewHolder(View view) {

        super(view);
        this.text = view.findViewById(R.id.my_text);

    }
}

public recycler_adapter(Context context,List<featured_post> featuredList) {
    this.featuredList = featuredList;
    this.context = context;

}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View itemView = LayoutInflater.from(context).inflate(R.layout.recycler_activity,null);
    MyViewHolder viewHolder = new MyViewHolder(itemView);

    return viewHolder;
}

@Override
public void onBindViewHolder(MyViewHolder holder,int position) {

    featured_post featured_post = featuredList.get(position);
    holder.text.setText(featured_post.string);
}

@Override
public int getItemCount() {
    return featuredList.size();
}

任何有关为何添加多个项目的帮助都将非常感激。

编辑: 我通过清除getViewfeaturedAdapter方法开头的列表来解决此问题。但是,我收到一个错误说:

java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 2(offset:2).state:4

为什么会突然发生这种情况?

1 个答案:

答案 0 :(得分:0)

我终于想出了如何解决我的问题。首先,为了确保它不会多次放置相同的元素,我添加了一个语句来清除featureAdapater中getView方法的列表:

featured_postList.clear();

然后,我得到了一个IndexOutOfBoundsException,因为clear语句位于方法的开头。相反,我通过将语句放在if(convertView == null)语句中来解决这个问题。这解决了我的问题,现在它按预期工作。