从recyclelview android获取imageView?

时间:2015-11-29 13:11:54

标签: android android-recyclerview material-design

我使用recycleview来显示来自数据库的数据,Everythink工作得很好,但是当我尝试在imageView点击时更改我的imageView drawable时遇到问题,我的工作就像stackOverflow当我正在进行投票时这样做的价值投票是+1,我有这样的流量,我有2个imageView,当imageView1点击了imageView1改变drawable到activeImage.png,而另一边imageView2(在同一行)改变drawable到diactivatedImage.png,我试过许多代码但不能正常工作,这是我的代码

public static class ContactViewHolder extends RecyclerView.ViewHolder {

    protected  ImageView imgVoteUp,imgVoteDown;
    protected  TextView vote;

    public ContactViewHolder(View v) {
        super(v);

        imgVoteUp= (ImageView) v.findViewById(R.id.gf_imgVoteUp);
        imgVoteDown= (ImageView) v.findViewById(R.id.gf_imgVoteDown);
        vote = (TextView) v.findViewById(R.id.gf_tvjmlVote);
    }

}


@Override
    public void onBindViewHolder(final ContactViewHolder contactViewHolder, int i) {
        final GfContactInfo ci = contactList.get(i);
        Picasso.with(ctx)
                .load(ctx.getString(R.string.photo) + ci.pp)
                .placeholder(R.drawable.gada_photo)
                .error(R.drawable.gada_photo)
                .into(contactViewHolder.gambarPP);

        contactViewHolder.imgVoteUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vote("idPosid","up",12,v);
            }
        });

        contactViewHolder.imgVoteUp.setImageResource(R.drawable._icon_up);
        contactViewHolder.imgVoteDown.setImageResource(R.drawable._icon_down);

}

    private void vote(String posid,String sts,int jmlVote,View v){
        String votex="0";
        ImageView iVup=(ImageView)v.findViewById(R.id.gf_imgVoteUp);
        ImageView iVdown=(ImageView)v.findViewById(R.id.gf_imgVoteDown); //i get error here because this is null
        TextView tVoteCount=(TextView)v.findViewById(R.id.gf_tvjmlVote);
        if (sts.equalsIgnoreCase("up")) {
            if (iVup.getDrawable().getConstantState().equals(mActivity.getResources().getDrawable(R.drawable._icon_up).getConstantState())) {
                if (iVdown.getDrawable().getConstantState().equals(mActivity.getResources().getDrawable(R.drawable._icon_down_selected).getConstantState())) {
                    iVup.setImageResource(R.drawable._icon_up);
                } else {
                    iVup.setImageResource(R.drawable._icon_up_selected);
                }
                iVdown.setImageResource(R.drawable._icon_down);
                votex = "1";
                jmlVote+=1;

            } else {
                votex="-1";
                jmlVote-=1;
                iVup.setImageResource(R.drawable._icon_up);
                iVdown.setImageResource(R.drawable._icon_down);
            }
        }else{
            if (iVdown.getDrawable().getConstantState().equals(mActivity.getResources().getDrawable(R.drawable._icon_down).getConstantState())) {
                if (iVup.getDrawable().getConstantState().equals(mActivity.getResources().getDrawable(R.drawable._icon_up_selected).getConstantState())) {
                    iVdown.setImageResource(R.drawable._icon_down);
                } else {
                    iVdown.setImageResource(R.drawable._icon_down_selected);
                }
                iVup.setImageResource(R.drawable._icon_up);
                votex = "1";
                jmlVote+=1;

            } else {
                votex="-1";
                jmlVote-=1;
                iVup.setImageResource(R.drawable._icon_up);
                iVdown.setImageResource(R.drawable._icon_down);
            }
        }
        tVoteCount.setText(jmlVote+"");

    }

我该怎么办?请帮忙。

1 个答案:

答案 0 :(得分:0)

您当前的代码将imgVoteUp作为View参数传递给投票。当你调用findViewById时,它找不到imgVoteDown,因为它不是imgVoteUp的孩子!

您需要更改您的投票方法以接受ViewHolder而不是视图,以便您可以获得参考:

private void vote(String posid, String sts, int jmlVote, ContactViewHolder viewHolder) {

    ImageView iVup = viewholder.imgVoteUp;
    ImageView iVdown = viewholder.imgVoteDown;

    //...

}

然后在你的onClick方法中你会调用:

vote("idPosid", "up", 12, contactViewHolder);