如何在按钮单击时更新回收器视图中的textview?

时间:2018-06-19 10:30:32

标签: java android android-recyclerview recycler-adapter

我想为列表项创建一个类似的按钮,每当我点击like按钮,然后textview应该动态更新。但是当我点击按钮时,只有最后一行得到更新,其余的不会改变。请指导我完成这个在listitems中制作按钮的任务。(我已经使用了likebutton库来获得类似按钮)。

RecyclerViewAdapter.java

lScan = MapVirtualKey(eKeys, 1); //flag 1: returns virtual key code of scan code eKeys.

recycler_view_items.xml

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{

private static final String TAG = "hh";
private String[] names;
private String[] desc;
private Integer[] imageid;
private Context context1;
ImageButton sare;
public ImageView img;
LikeButton likeButton;
TextView tx1;
TextView tx2;
TextView tx3;
TextView tx4;
public RecyclerViewAdapter(Context context2,String[] names,String[] desc,Integer[] imageid){
    this.names = names;
    this.desc = desc;
    this.imageid = imageid;
    context1 = context2;
}

public class ViewHolder extends RecyclerView.ViewHolder{

    public LikeButton likeButton;

    public ViewHolder(View v){

        super(v);


    }
}

@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){

    View view1 = LayoutInflater.from(context1).inflate(R.layout.recycler_view_items,parent,false);

    ViewHolder viewHolder1 = new ViewHolder(view1);
    img = (ImageView) view1.findViewById(R.id.imgview1);
    likeButton = (LikeButton) view1.findViewById(R.id.cvcv);
    tx3 = (TextView)view1.findViewById(R.id.counttxt);
    likeButton.setOnLikeListener(new OnLikeListener() {
        @Override
        public void liked(LikeButton likeButton) {

            int count = Integer.parseInt(tx3.getText().toString());
            Log.d("fgfgfgfgf", String.valueOf(count));
            tx3.setText(String.valueOf(++count));
            //tx3.setText(String.valueOf(Homefragment.countt));
        }
        @Override
        public void unLiked(LikeButton likeButton) {

        }
    });
    return viewHolder1;
}

@Override
public void onBindViewHolder(ViewHolder Vholder,final int position){
    if(imageid[position] != null) {
       // img.setImageBitmap(imageid[position]);
    }
    else{
    }
    img.setImageResource(imageid[position]);
    tx1.setText(names[position]);

    Vholder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

}



@Override
public int getItemViewType(int position) {
    return position;
}

@Override
public int getItemCount(){

    return desc.length;
}

}

2 个答案:

答案 0 :(得分:1)

替换

tx3 = (TextView)view1.findViewById(R.id.counttxt);

TextView tx3 = (TextView)view1.findViewById(R.id.counttxt);

likeButton = (LikeButton) view1.findViewById(R.id.cvcv);

LikeButton likeButton = (LikeButton) view1.findViewById(R.id.cvcv);

在您的代码中,您使用instance tx3likebutton .modalDialog { position: fixed; font-family: Arial, Helvetica, sans-serif; top: 0; right: 0; bottom: 0; left: 0; background: rgba(0, 0, 0, 0.8); z-index: 99999; opacity: 0; -webkit-transition: opacity 400ms ease-in; -moz-transition: opacity 400ms ease-in; transition: opacity 400ms ease-in; pointer-events: none; } .modalDialog:target,.modalDialog:hover { opacity: 1; pointer-events: auto; } .modalDialog>#__spookyPopup { width: 400px; position: relative; margin: 10% auto; padding: 5px 20px 13px 20px; border-radius: 10px; background: #fff; background: -moz-linear-gradient(#fff, #999); background: -webkit-linear-gradient(#fff, #fff); background: -o-linear-gradient(#fff, #999); } .profile_container { height: 160px; width: 400px; background: #ccc; display: inline-block; } .profile_div { height: 120px; width: 130px; margin-top: 20px; margin-left: 130px; background: #ddd; border: 1px solid grey; } .head_div { min-height: 12px; width: 100%; } .media_layer { margin-top: 20px; min-height: 12px; width: 400px; background: #; display: inline-block; } .manual { height: 50px; width: 197px; float: right; background: #; display: inline-block; } .manual:hover { border-bottom: 2px solid #ab0a72; } .social { height: 50px; width: 197px; background: #; display: inline-block; } .social:hover { border-bottom: 2px solid #ab0a72; } .social_link_container { height: 160px; width: 400px; background: #ccc; display: inline-block; } .fb_container { height: 50px; width: 340px; margin-top: 20px; margin-left: 30px; background: #ddd; border: 1px solid grey; } .clicker { display: inline-block; width: 100px; height: 35px; padding-top: 10px; padding-left: 15px; background-color: blue; color: #FFF; } .clicker.hidden { display: none; } .hiddendiv { height: 0px; background-color: green; overflow: hidden; transition: height 0.5s; } .hiddendiv.nr2 { background-color: red; } #showdiv1:target~div a[href="#showdiv1"], #showdiv2:target~div a[href="#showdiv2"] { display: none; } #showdiv1:target~div a[href="#hidediv1"], #showdiv2:target~div a[href="#hidediv2"] { display: inline-block; } #showdiv1:target~div .hiddendiv.nr1, #showdiv2:target~div .hiddendiv.nr2 { height: 150px; },并保存最后一个值,希望这会有所帮助

答案 1 :(得分:1)

public class ViewHolder extends RecyclerView.ViewHolder{

    LikeButton likeButton;
    TextView tx1;
    TextView tx2;
    TextView tx3;
    TextView tx4;

    public ViewHolder(View v){
    super(v);
    img = (ImageView) v.findViewById(R.id.imgview1);
    likeButton = (LikeButton) v.findViewById(R.id.cvcv);
    tx3 = (TextView)v.findViewById(R.id.counttxt);



    }
}

将您的OnCreateviewHolder更改为:-

@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){

    View view1 = LayoutInflater.from(context1).inflate(R.layout.recycler_view_items,parent,false);
    return new ViewHolder(view1);
}

OnBindViewHolder到:-

@Override
public void onBindViewHolder(ViewHolder Vholder,final int position){
    Vholder.img.setImageResource(imageid[position]);
    Vholder.tx1.setText(names[position]);

    Vholder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
    Vholder.likeButton.setOnLikeListener(new OnLikeListener() {
    @Override
    public void liked(LikeButton likeButton) {

        //Logic
    }
    @Override
    public void unLiked(LikeButton likeButton) {

    }
    });
    }
相关问题