在我的自定义列表项中单击书签图像的侦听器,多次更改另一个 row_item 图像

Posted

技术标签:

【中文标题】在我的自定义列表项中单击书签图像的侦听器,多次更改另一个 row_item 图像【英文标题】:Click listener of Bookmark image in my Custom List item, changes another row_item image many times 【发布时间】:2016-01-03 01:00:48 【问题描述】:

我创建了一个自定义列表视图。在列表视图中,每个组件都有其单击事件,例如添加到书签图像、当前位置图像、距离位置图像等。

在书签图片中,例如假设我的屏幕上有 10 行可见,如果我单击第 4 行项目书签图像,那么有时它会在上方或下方更改书签图像。我在这里发布我的基本适配器代码。

提前谢谢你。

基础适配器

public class OffersBaseAdapter extends BaseAdapter 

Context context;
LayoutInflater mInflater;
OffersInformation offerList = new OffersInformation();
private MessageData msgData;
//String OfferId;

public OffersBaseAdapter(Context context, OffersInformation offerList) 
    this.context = context;
    this.offerList = offerList;
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);




@Override
public int getCount() 
    return offerList.offersInfo.size();


@Override
public Object getItem(int position) 
//        return offerList.offersInfo.get(position);
    return position;


@Override
public long getItemId(int position) 
//        return offerList.offersInfo.indexOf(offerList.offersInfo.get(position));
return position;


public class ViewHolder 
    TextView tv_HeaderOffer, tv_OfferDescr, tv_PostedBy, tv_Distance, tv_RemainingDays;
    ImageView img_AddtoWishBag, img_storeLocation, img_storeDirection, img_storeImage, img_vertical, img_horizontal;



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

    try 

        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) 

            convertView = mInflater.inflate(R.layout.custom_item_offers, null);
            holder.tv_HeaderOffer = (TextView) convertView.findViewById(R.id.tvC_ProdcutOffer);
            holder.tv_OfferDescr = (TextView) convertView.findViewById(R.id.tvC_ProductDescription);
            holder.tv_PostedBy = (TextView) convertView.findViewById(R.id.tvC_PostedBy);
            holder.tv_RemainingDays = (TextView) convertView.findViewById(R.id.tvC_remaingTime);
            holder.tv_Distance = (TextView) convertView.findViewById(R.id.tvC_distace);
            holder.img_vertical = (ImageView) convertView.findViewById(R.id.ivC_Vertical);
            holder.img_horizontal = (ImageView) convertView.findViewById(R.id.ivC_Horizonal);
            holder.img_AddtoWishBag = (ImageView) convertView.findViewById(R.id.ivC_addToWishBag);
            holder.img_storeImage = (ImageView) convertView.findViewById(R.id.ivC_Logo);
            holder.img_storeDirection = (ImageView) convertView.findViewById(R.id.ivC_mapView);
            holder.img_storeLocation = (ImageView) convertView.findViewById(R.id.ivC_Location);
            convertView.setTag(holder);

         else 

            holder = (ViewHolder) convertView.getTag();
        

        holder.img_AddtoWishBag.setTag(position);

        List<String> items = Arrays.asList(offerList.offersInfo.get(position).st_logo.split("\\s*,\\s*"));


        if (position == 0)

            if (position % 2 == 0) 

                holder.img_horizontal.setBackgroundColor(Color.parseColor("#019CD5"));
                holder.img_vertical.setBackgroundColor(Color.parseColor("#019CD5"));

             else 


                holder.img_horizontal.setBackgroundColor(Color.parseColor("#0A7B9B"));
                holder.img_vertical.setBackgroundColor(Color.parseColor("#0A7B9B"));

            

        holder.tv_HeaderOffer.setText(offerList.offersInfo.get(position).of_title);
        holder.tv_PostedBy.setText("Posted By : " + offerList.offersInfo.get(position).st_name);
        holder.tv_OfferDescr.setText(offerList.offersInfo.get(position).of_desc);


        String KM = String.format("%.02f", offerList.offersInfo.get(position).distance * 1.609344);
        holder.tv_Distance.setText(KM + " KM");
        holder.tv_RemainingDays.setText(offerList.offersInfo.get(position).of_end);


        holder.img_storeLocation.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                Intent reDirectMap = new Intent(context, androidGoogleMapsActivity.class);
                reDirectMap.putExtra("Latitude", offerList.offersInfo.get(position).st_lat);
                reDirectMap.putExtra("Longitude", offerList.offersInfo.get(position).st_lng);
                context.startActivity(reDirectMap);
            
        );

        if (!offerList.offersInfo.get(position).st_logo.equalsIgnoreCase("") || !offerList.offersInfo.get(position).st_logo.equals("no")) 

            Picasso.with(context)
                    .load(AppWSConstants.IMG_UPLOAD_OFFER_URL + items.get(0))
                    .into(holder.img_storeImage);
         else 
            // holder.img_storeImage.setBackgroundResource(R.drawable.home_icon);
        


        if (offerList.offersInfo.get(position).is_wishbag.equalsIgnoreCase("Y")) 

            holder.img_AddtoWishBag.setImageResource(R.mipmap.ic_removefromwishbag);

         else 

            holder.img_AddtoWishBag.setImageResource(R.drawable.ic_addtowishbag);
        


        final ViewHolder finalHolder = holder;
        holder.img_AddtoWishBag.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                if (offerList.offersInfo.get(position).is_wishbag.equalsIgnoreCase("N")) 

                    RequestParams params = new RequestParams();
                    params.put("loginid", AppGlobal.getFromUserDefaults(context, AppConstants.KEY_UID));
                    params.put("offerid", offerList.offersInfo.get(position).of_id);

                    AsyncHttpClient mClient = new AsyncHttpClient();
                    mClient.setTimeout(6 * 1000);
                    mClient.post(AppWSConstants.WS_ADDTO_WISHBAG, params, new AddRemoveOfferWishBag(finalHolder.img_AddtoWishBag, true));
                    notifyDataSetChanged();


                 else if (offerList.offersInfo.get(position).is_wishbag.equalsIgnoreCase("Y")) 

                    RequestParams params = new RequestParams();
                    params.put("loginid", AppGlobal.getFromUserDefaults(context, AppConstants.KEY_UID));
                    params.put("offerid", offerList.offersInfo.get(position).of_id);

                    AsyncHttpClient mClient = new AsyncHttpClient();
                    mClient.setTimeout(6 * 1000);
                    mClient.post(AppWSConstants.WS_REMOVEFROM_WISHBAG, params, new AddRemoveOfferWishBag(finalHolder.img_AddtoWishBag, false));
                    notifyDataSetChanged();
                
            
        );

        return convertView;

     catch (Exception ex) 

        Log.i("LogInfo->", ex.getMessage());
    


    return convertView;




public class AddRemoveOfferWishBag extends AsyncHttpResponseHandler 

    ImageView ivWishBag;
    boolean isAddWishtoBag;

    public AddRemoveOfferWishBag(ImageView ivWishBag, boolean isAddWishtoBag) 


        this.ivWishBag = ivWishBag;
        this.isAddWishtoBag = isAddWishtoBag;

    


    @Override
    public void onStart() 
        super.onStart();
    

    @Override
    public void onFinish() 
        super.onFinish();
    

    @Override
    public void onSuccess(String content) 
        super.onSuccess(content);

        msgData = new MessageData();
        Gson gson = new Gson();
        Type infoType = new TypeToken<MessageData>() 
        .getType();
        msgData = gson.fromJson(content, infoType);

        if (msgData.messageInfo.status == 1) 

            if (isAddWishtoBag) 
                ivWishBag.setImageResource(R.mipmap.ic_removefromwishbag);

             else 
                ivWishBag.setImageResource(R.drawable.ic_addtowishbag);

            


        

    

    @Override
    public void onFailure(Throwable error, String content) 
        super.onFailure(error, content);
        Toast.makeText(context, "Success", Toast.LENGTH_LONG).show();
    



【问题讨论】:

在添加所有视图后在 if 条件内使用 settag @Aayushi 这对我不起作用......它显示了同样的问题 【参考方案1】:

您现在在每次调用 getView() 时都设置新的点击侦听器。我认为这会破坏功能。我会将所有setOnClickListener() 调用放在if (convertView == null) ... 块内,以便为每个ViewHolder 实例设置一次侦听器:

...
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if (convertView == null) 

    // The code block for one time initializations:

    convertView = mInflater.inflate(R.layout.custom_item_offers, null);
    holder.tv_HeaderOffer = (TextView) convertView.findViewById(R.id.tvC_ProdcutOffer);
    holder.tv_OfferDescr = (TextView) convertView.findViewById(R.id.tvC_ProductDescription);
    holder.tv_PostedBy = (TextView) convertView.findViewById(R.id.tvC_PostedBy);
    holder.tv_RemainingDays = (TextView) convertView.findViewById(R.id.tvC_remaingTime);
    holder.tv_Distance = (TextView) convertView.findViewById(R.id.tvC_distace);
    holder.img_vertical = (ImageView) convertView.findViewById(R.id.ivC_Vertical);
    holder.img_horizontal = (ImageView) convertView.findViewById(R.id.ivC_Horizonal);
    holder.img_AddtoWishBag = (ImageView) convertView.findViewById(R.id.ivC_addToWishBag);
    holder.img_storeImage = (ImageView) convertView.findViewById(R.id.ivC_Logo);
    holder.img_storeDirection = (ImageView) convertView.findViewById(R.id.ivC_mapView);
    holder.img_storeLocation = (ImageView) convertView.findViewById(R.id.ivC_Location);

    // Set up all listeners here:
    // ...


    convertView.setTag(holder);

 else 

    holder = (ViewHolder) convertView.getTag();


holder.img_AddtoWishBag.setTag(position);

List<String> items = Arrays.asList(offerList.offersInfo.get(position).st_logo.split("\\s*,\\s*"));

// Set the latest content here. Don't do any initializations here as this code is always run.
// ...

...

【讨论】:

以上是关于在我的自定义列表项中单击书签图像的侦听器,多次更改另一个 row_item 图像的主要内容,如果未能解决你的问题,请参考以下文章

在我的自定义列表视图中添加一个复选框(一次只能选中一个)

“导航项中不支持普通样式”警告我的自定义条形按钮项

如何在自定义适配器上实现 onClickListener?

在android studio中删除一个带有按钮的listview项目

通过在 Adapter 的 getView() 中单击 imageView 更改图像

在我的WordPress菜单项中添加#无法正常工作