Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 快速滚动时,RecyclerAdapter行正在洗牌_Java_Android_Android Recyclerview - Fatal编程技术网

Java 快速滚动时,RecyclerAdapter行正在洗牌

Java 快速滚动时,RecyclerAdapter行正在洗牌,java,android,android-recyclerview,Java,Android,Android Recyclerview,你好:)我看过很多关于这个主题的帖子,但我还没有解决我的问题 在我的应用程序(Android)中,我有一个RecyclerView,有我自己的适配器和ViewHolder 当我快速滚动时,每一行的数据都在乱序。(每行包含“x”个按钮。按钮的数量会改变。按钮的标签也会改变。) 我的适配器,像这样 public class PostRecyclerAdapter extends RecyclerView.Adapter<PostRecyclerHolders> { private

你好:)我看过很多关于这个主题的帖子,但我还没有解决我的问题

在我的应用程序(Android)中,我有一个RecyclerView,有我自己的适配器和ViewHolder

当我快速滚动时,每一行的数据都在乱序。(每行包含“x”个按钮。按钮的数量会改变。按钮的标签也会改变。)

我的适配器,像这样

public class PostRecyclerAdapter extends RecyclerView.Adapter<PostRecyclerHolders> {
    private List<Post> posts;
    protected Context context;
    private String userId;
    public PostRecyclerAdapter(Context context, List<Post> posts,String userId) {
        this.posts = posts;
        this.context = context;
        this.userId = userId;
        setHasStableIds(true);
    }
    @Override
    public PostRecyclerHolders onCreateViewHolder(ViewGroup parent, int viewType) {
        PostRecyclerHolders viewHolder = null;
        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_post_row, parent, false);
        viewHolder = new PostRecyclerHolders(layoutView, posts, context);
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(PostsRecyclerHolders holder, int position) {
        Post post = posts.get(position);
        List<But> buts = post.getButs(); //list of But (Contains label and date) max size = 6
        holder.label.setText(post.getLabel());
        //initialize all button
        for(i=0; i<buts.size();i++){
            holder.listButs.get(i).setText(buts.get(i).getLabel());
        }
        //hide other buts
        for(int j = i ;j < 6 ; j++){
            holder.listsButs.get(j).setVisibility(View.GONE);
        }
        holder.userId = userId;
    }
    @Override
    public int getItemCount() {
        return this.posts.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
}
公共类PostRecyclerAdapter扩展了RecyclerView.Adapter{
私人名单员额;
受保护的语境;
私有字符串用户标识;
公共PostRecyclerAdapter(上下文上下文、列表帖子、字符串用户ID){
这个.posts=posts;
this.context=上下文;
this.userId=userId;
setHasStableIds(true);
}
@凌驾
public PostRecyclerHolders onCreateViewHolder(视图组父级,int-viewType){
PostRecyclerHolders viewHolder=null;
View layoutView=LayoutInflater.from(parent.getContext()).flate(R.layout.fragment\u post\u行,parent,false);
viewHolder=新的PostRecyclerHolder(布局视图、帖子、上下文);
返回视图持有者;
}
@凌驾
BindViewHolder上的公共无效(PostsRecyclerHolders holder,内部位置){
Post Post=posts.get(位置);
List buts=post.getButs();//但是列表(包含标签和日期)最大大小=6
holder.label.setText(post.getLabel());
//初始化所有按钮

对于(i=0;i如果您已经知道需要多少按钮,为什么要通过编程生成按钮?为什么不在XML中声明所有按钮?我这样做是因为我只想根据项目生成数量可变的按钮。但我没有成功。但我不认为这是导致
onBindViewHolde中循环出现问题的原因r()
,以及
PostRecyclerHolders
构造函数中的一个,以及
setOnClickListener()
内部的工作,对我的口味来说也非常不好。谢谢你的回答,你可以简单地解释一下原因吗?
public class PostRecyclerHolders extends RecyclerView.ViewHolder{
    public TextView libelle;
    public LinearLayout butLayout;
    public ArrayList<Button> listButs;
    public String userId;
    public Context context;
    private List<Post> postsObj;

    public PostRecyclerHolders(final View itemView, final List<Post> postsObj, Context context) {
        super(itemView);
        this.context=context;
        this.postsObj = postsObj;
        //list button init
        listButs = new ArrayList<Button>();
        //initialisation des
        libelle = (TextView)itemView.findViewById(R.id.libelle);
        butLayout= (LinearLayout)itemView.findViewById(R.id.but_list);
        for(int i=0; i<6;i++){
            final int butN = i;
            Button btn = createButton(context, i);
             btnRep.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                 /* Do something */
                 }
             });

            listBut.add(btn);
            reponsesLayout.addView(btn);
        }
    }

    //Crate a button
    public Button createButton(Context c, int id){
        Button b = new Button(c);
        b.setText("");
        b.setId(getIdButton(id));
        LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        lp.setMargins(0,50,0,0);
        b.setVisibility(View.VISIBLE);
        b.setLayoutParams(lp);
        b.setBackgroundResource(R.drawable.button_blue);
        b.setClickable(true);
        b.setTextSize(18);
        return b;
    }
}