在Listview中动态更改clickevent上的ImageView(切换)

时间:2015-11-06 09:24:29

标签: android listview baseadapter android-viewholder

我实现了类似于Facebook的Likening的类似功能。如果您已经喜欢帖子,则会显示特定的imageview。单击该按钮将减少相似数量或增加,并显示另一个imageview而不是前一个(此处执行切换)。单击图像,状态将在服务器上发布,新计数将收到从服务器作为响应,它将显示。它没有正常运行。有时如果我在列表视图中向上和向下滚动,那么喜欢功能可能会工作一段时间后它会卡住直到再次向上和向下滚动。任何人都可以帮助我?请问我是否理解它。

public class FeedListAdapter extends BaseAdapter implements APIConstants, JsonTagConstants {

    private Activity mContext;
    private ArrayList<FeedListModel> feedModelArrayList;
    int likestatus, pos;
    private LayoutInflater mInflater;
    private String feedId, usrId, like_count;


    private int[] images = {R.drawable.feed_like_inactive, R.drawable.feed_like_active};

    public FeedListAdapter(Activity mContext, ArrayList<FeedListModel> feedModelArrayList
    ) {
        this.mContext = mContext;
        this.feedModelArrayList = feedModelArrayList;
        mInflater = (LayoutInflater) mContext.getLayoutInflater();
        System.out.println("03112015:InitialLikeStatus feedModelArrayList size:" + feedModelArrayList.size());

    }

    @Override
    public int getCount() {
        return feedModelArrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return feedModelArrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder = new ViewHolder();
//
       View view;

        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.custom_feeds_listview, parent, false);

        } else {
            view = convertView;
        }
            holder.imageView1 = (ImageView) convertView.findViewById(R.id.feed_iv_thumb);
            holder.imageView2 = (ImageView) convertView.findViewById(R.id.feed_iv_banner);
            holder.imageView3 = (ImageView) convertView.findViewById(R.id.feed_iv_likecount);
            holder.imageView4 = (ImageView) convertView.findViewById(R.id.feed_iv_share);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.feed_tv_title);
            holder.txtSub = (TextView) convertView.findViewById(R.id.feed_tv_postdate);
            holder.txtContent = (TextView) convertView.findViewById(R.id.feed_tv_desc);
            holder.txtCount = (TextView) convertView.findViewById(R.id.feed_tv_txt_count);
            holder. progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);

        Utils.setImageFromUrl(mContext, API_IMAGE_URL + feedModelArrayList.get(position).getmBannerImage()
                , holder.imageView1,
                holder.progressBar);
        Utils.setImageFromUrl(mContext, API_IMAGE_URL + feedModelArrayList.get(position).getmBannerImage()
                , holder.imageView2,
                holder.progressBar);


        holder.txtTitle.setText(feedModelArrayList.get(position).getmTitle());


        try {
            ;
            holder.txtSub.setText(gmodifyDateLayout(feedModelArrayList.get(position).getmCreatedDate()));

        } catch (ParseException e) {
            e.printStackTrace();
        }
        likestatus = Integer.parseInt(feedModelArrayList.get(position).getmlikestatus());


        feedId = feedModelArrayList.get(position).getId();
        usrId = "2";
        System.out.println("03112015:InitialLikeStatus of " + feedId + ":" + likestatus);
        changeBackground(holder);



        holder.txtContent.setText(feedModelArrayList.get(position).

                        getmDescription()

        );
        holder.txtCount.setText(feedModelArrayList.get(position).

                        getmFeedLikeCount()

        );
        pos = position;

        holder.imageView3.setOnClickListener(new View.OnClickListener()

                                      {
                                          @Override
                                          public void onClick(View view) {


                                              if (likestatus == 0) {


                                                  likestatus = 1;




                                              } else if (likestatus == 1) {
                                                  likestatus = 0;
                                                    }
                                              System.out.println("04112015:onClick:LikeStatus" + likestatus);

                                              changeBackground(holder);

                                              addFeedLikeAPI(feedId, usrId,holder);

                                              System.out.println("03112015:FinalLikeStatus" + likestatus);

                                          }
                                      }

        );

        holder.imageView4.setOnClickListener(new View.OnClickListener()

                                      {
                                          @Override
                                          public void onClick(View view) {


                                              Toast.makeText(mContext, "Share", Toast.LENGTH_LONG).show();

                                          }
                                      }

        );
        return convertView;
    }



    private void addFeedLikeAPI(String feedId, String usrId,final ViewHolder holder ) {


        InternetManager manager = new InternetManager(API_FEEDS_POST_FEED_LIKE + feedId + "&usrId=" + usrId +
                "&like=" + likestatus);
        System.out.println("03102015:usrId:" + usrId);
        System.out.println("03102015:feedId:" + feedId);
        System.out.println("03102015:like:" + likestatus);


        String[] name = {""};
        String[] value = {""};
        manager.getResponsePOST(mContext, name, value, new InternetManager.ResponseListener() {
            @Override
            public void responseSuccess(String successResponse) {

                System.out.println("06112015:API Response::" + successResponse);
                //                API Response::{"Message":"Success"}
                try {

                    JSONObject jsonObject = new JSONObject(successResponse);
                    like_count = jsonObject.optString(JSON_FEED_LIKE_STATUS_MESSAGE);
                    System.out.println("06112015:like_count::" + like_count);


                    holder.txtCount.setText(like_count);

                } catch (Exception e) {

                }


            }

            @Override
            public void responseFailure(String failureResponse) {
                Toast.makeText(mContext, mContext.getResources().getString(R.string.feeds_error_msg), Toast.LENGTH_LONG).show();
            }
        });
    }


    public void changeBackground(final ViewHolder holder) {
        System.out.println("03112015:Inside changeBackground :LikeStatus" + likestatus);

        if (likestatus == 0) {
            System.out.println("03112015:Inside changeBackground likestatus == 0:LikeStatus" + likestatus);

            holder.imageView3.setBackgroundResource(images[0]);


        } else if (likestatus == 1) {
            System.out.println("03112015:Inside changeBackground likestatus == 1:LikeStatus" + likestatus);

            holder.imageView3.setBackgroundResource(images[1]);

        }
    }

    public String gmodifyDateLayout(String inputDate) throws ParseException {
        Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(inputDate);

        return new SimpleDateFormat("dd MMM yyyy").format(date);


    }


    public class ViewHolder {
        ImageView imageView1;
        ImageView imageView2;
        ProgressBar progressBar;
        TextView txtTitle, txtSub, txtContent, txtCount;
        ImageView imageView3;
        ImageView imageView4;
    }
}

模型类

public class FeedListModel extends ParentModel implements Serializable
{
    private String mTitle;
    private String mDescription;
    private String mCreatedDate;
    private String mBannerImage;
    private String mStatus;
    private String mFeedLikeCount;
    private String mlikestatus;

    public String getmTitle() {
        return mTitle;
    }

    public void setmTitle(String mTitle) {
        this.mTitle = mTitle;
    }

    public String getmDescription() {
        return mDescription;
    }

    public void setmDescription(String mDescription) {
        this.mDescription = mDescription;
    }

    public String getmCreatedDate() {
        return mCreatedDate;
    }

    public void setmCreatedDate(String mCreatedDate) {
        this.mCreatedDate = mCreatedDate;
    }

    public String getmBannerImage() {
        return mBannerImage;
    }

    public void setmBannerImage(String mBannerImage) {
        this.mBannerImage = mBannerImage;
    }

    public String getmStatus() {
        return mStatus;
    }

    public void setmStatus(String mStatus) {
        this.mStatus = mStatus;
    }

    public String getmFeedLikeCount() {
        return mFeedLikeCount;
    }

    public void setmFeedLikeCount(String mFeedLikeCount) {
        this.mFeedLikeCount = mFeedLikeCount;
    }

    public String getmlikestatus() {
        return mlikestatus;
    }

    public void setmlikestatus(String mlikestatus) {
        this.mlikestatus = mlikestatus;
    }
}

1 个答案:

答案 0 :(得分:0)

将全局变量声明为 private boolean ischecked = true;

    img.setOnClickListener(new view.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (ischecked) {
                img.setBackgroundResource(R.drawable.check_true);
                ischecked = false;
            } else {
                img.setBackgroundResource(R.drawable.check_false);
                ischecked = true;
            }
        }
    });