ListView向上和向下滚动可重置其中的数据

时间:2015-12-30 02:32:35

标签: android listview

我开发了一个应用程序,当用户启动它打开的应用程序并使用异步任务将数据下载到listview时。每行都有一个黑色的按钮。用户可以通过单击心形按钮来改变项目,并且它变为红色。当用户向下滚动并返回到他/她喜欢的项目时,喜欢的按钮将返回到黑色。即使用户向下/向上滚动并返回喜欢的项目,我也希望红色保持不变。

这是我的适配器

public class ListViewAdapterSup extends BaseAdapter {

// Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
HashMap<String, String> resultp = new HashMap<String, String>();



public ListViewAdapterSup(Context context,
        ArrayList<HashMap<String, String>> arraylist) {
    this.context = context;
    data = arraylist;
}

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


@Override
public Object getItem(int position) {
    return null;
}

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

static class ViewHolder {

    // Declare Variables
    TextView artisteTV;
    TextView likesCountTV;
    ImageButton artisteLike;

}

ViewHolder holder;



public View getView(final int position, View itemView, ViewGroup parent) {
    itemView = null;

    if (itemView == null) {


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

    itemView = inflater.inflate(R.layout.sups_list_item, parent, false);
    // Get the position
    resultp = data.get(position);
    holder = new ViewHolder();



    // Locate the TextViews in listview_item.xml
    holder.artisteTV = (TextView) itemView.findViewById(R.id.artiste_name);
    holder.likesCountTV = (TextView) itemView.findViewById(R.id.likes_count);

    holder.artisteLike= (ImageButton) itemView.findViewById(R.id.art_like);

    itemView.setTag(holder);

    }

     else{

         holder = (ViewHolder) itemView.getTag();

     }



    // Capture position and set results to the TextViews
    holder.artisteTV.setText(resultp.get(Artiste.NAME));
    holder.likesCountTV.setText(resultp.get(Sups.LIKES_COUNT));


        holder.artisteLike.setTag(holder);
            holder.artisteLike.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                     holder = (ViewHolder)view.getTag();
                                resultp = data.get(position);

                                if(holder.artisteLike.getDrawable().getConstantState().equals(holder.artisteLike.getContext().getDrawable(R.drawable.ic_favorite_black_24dp).getConstantState())){
                                resultp = data.get(position);

                                    ConnectionDetector cd = new ConnectionDetector(context);
                                    if (!cd.isConnectingToInternet()) {

                                        Toast.makeText(context,"No internet connection",Toast.LENGTH_SHORT).show();


                                        }
                                    else
                                    {

                                    new LikeSup().execute();
                                    holder.artisteLike.setImageResource(R.drawable.ic_favorite_red_24dp);

                                    }

                            }

                                else if(holder.artisteLike.getDrawable().getConstantState().equals(holder.artisteLike.getContext().getDrawable(R.drawable.ic_favorite_red_24dp).getConstantState())){
                                    resultp = data.get(position);

                                        ConnectionDetector cd = new ConnectionDetector(context);
                                        if (!cd.isConnectingToInternet()) {

                                            Toast.makeText(context,"No internet connection",Toast.LENGTH_SHORT).show();


                                            }
                                        else
                                        {

                                        new UnLikeSup().execute();
                                       holder.artisteLike.setImageResource(R.drawable.ic_favorite_black_24dp);

                                        }
                                    }

                            }



                }
            });


    return itemView;
}

}

2 个答案:

答案 0 :(得分:0)

首先,如果你使用普通的ListView,那么请确保你&#34; pack&#34;对象中的列表项,当用户单击like按钮时,在这种情况下更改该对象的状态(喜欢或不喜欢)。

如果你使用的是可循环使用的ListView,那么就像评论中提到的其他人一样,无论你在每一行中获得相同的对象,你的视图都会被回收。

到这里学习ViewHolder模式,其中列表可以具有唯一元素,但同时回收它们以实现快速滚动和更好的资源管理。 https://developer.android.com/training/improving-layouts/smooth-scrolling.html

在这里,你可以学习如何实现它,它有一个正在发生的图片,所以你可以想象它。 http://www.rogcg.com/blog/2013/03/12/recycling-views-listview

答案 1 :(得分:0)

首先,在getview()方法中,你不应该将itemView设置为null;你应该理解循环机制,&#34; itemView&#34;(称为convertView)是当前显示视图,它保持几个项目视图;

第二

  

用户可以通过单击心形按钮来改变项目,然后它变为红色,当用户向下滚动并返回到他/她喜欢的项目时,喜欢的按钮会变回黑色。即使用户向下/向上滚动并返回喜欢的项目,我也希望红色保持不变。

1,about ArrayList<HashMap<String, String>> data;

您应该更改arraylist包含数据库的数据结构,例如:

public class AlbumBean implements Serializable{
private static final long serialVersionUID = 1L;
private String likedCount;
private boolean isLiked; 
private String picUrl;
// get() set()  method
...................

}

2,in your ListViewAdapterSup ,you should define a var  boolean isLiked;

3,the example as i do

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null || !(convertView.getTag() instanceof ViewHolder)) {

            convertView = LayoutInflater.from(context).inflate(R.layout.buyah_chosen_album_detail_list_item, parent, false);
            holder = new ViewHolder();
            holder.productImg = (ScaledRemoteImageView) convertView.findViewById(R.id.product_img);
            holder.productDesc = (TextView) convertView.findViewById(R.id.product_desc);
            holder.productTitle = (TextView) convertView.findViewById(R.id.product_title);
            holder.favoriteCount = (TextView) convertView.findViewById(R.id.favorite_count);
            holder.multiple = (TextView) convertView.findViewById(R.id.multiple_content);
            holder.priceAndScoreView = (BuyahPriceAndScoreView) convertView.findViewById(R.id.price_score_view);
            holder.favoriteCount.setOnClickListener(this);
            convertView.setTag(holder);


    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    if (holder != null) {
        final ChosenAlbumDetailBean product = getItem(position);
        holder.favoriteCount.setTag(R.id.price, holder);
        holder.favoriteCount.setTag(R.id.favorite_count, product);
        holder.productImg.setTag(R.id.product_img, product);
        holder.priceAndScoreView.setPriceAndScore(product, 16);
        ImageLoader.getInstance().displayImage(product.getPic(), holder.productImg);
        holder.productTitle.setText(product.getTitle());

        holder.favoriteCount.setText(product.getLiked());
        if (product.getIs_liked()) {
            isFavorited = true;
            holder.favoriteCount.setCompoundDrawablesWithIntrinsicBounds(R.drawable.buyah_middle_favorite_selected, 0, 0, 0);
        } else {
            isFavorited = false;
            holder.favoriteCount.setCompoundDrawablesWithIntrinsicBounds(R.drawable.buyah_middle_favorite_unselect, 0, 0, 0);
        }

    }

    return convertView;
}

@Override
public void onClick(final View v) {
    ChosenAlbumDetailBean product = null;
    switch (v.getId()) {
        case R.id.favorite_count:
            ClickDurationController.controlClickDuration(v);
            if (!UserInfoCommon.getInstance().isLogined()) {
                if (buyahChosenAdapterNeedLoginListener != null) {
                    buyahChosenAdapterNeedLoginListener.onBuyahChosenAdapterClick();
                }
            } else {
                ViewHolder holder = (ViewHolder) v.getTag(R.id.price);
                product = (ChosenAlbumDetailBean) v.getTag(R.id.favorite_count);
                boolean isFavorite = product.getIs_liked();
                int currentFavoriteCount = Integer.valueOf(holder.favoriteCount.getText().toString());
                if (isFavorite) {
                    isFavorite = false;
                    isFavorited = false;
                    holder.favoriteCount.setText(currentFavoriteCount - 1 + "");
                    holder.favoriteCount.setCompoundDrawablesWithIntrinsicBounds(R.drawable.buyah_middle_favorite_unselect, 0, 0, 0);
                } else {
                    isFavorite = true;
                    isFavorited = true;
                    holder.favoriteCount.setCompoundDrawablesWithIntrinsicBounds(R.drawable.buyah_middle_favorite_selected, 0, 0, 0);
                    holder.favoriteCount.setText(currentFavoriteCount + 1 + "");
                }
                product.setIs_liked(isFavorite);
                product.setLiked(holder.favoriteCount.getText().toString());

            break;
        default:
            break;
    }
}
希望能帮到你!