网格视图文本在Android中滚动时消失

时间:2014-08-30 05:45:19

标签: android gridview

我正在制作一个gridview,它使用来自图库的Image Loader概念显示图像。在网格视图项中有两个视图,一个是Image,另一个是textview。默认情况下,textview不可见。我需要的是当我点击gridview项目时,textview应该是可见的,它将显示特定网格项目的点击次数。但问题是当我点击网格项目然后Textview显示点击计数,但是当滚动网格视图时,textview会消失。我两天都坚持这个,请帮我解决问题。

这是我的GetView方法;

        public class ImageAdapter extends BaseAdapter {
         View view;

         ArrayList<String> mList;
        LayoutInflater mInflater;
        Context mContext;



    public ImageAdapter(Context context, ArrayList<String> imageList) {
        // TODO Auto-generated constructor stub
        mContext = context;
        mInflater = LayoutInflater.from(mContext);

        mList = new ArrayList<String>();

        this.mList = imageList;

    }

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

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

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


public View getView(final int position, View convertView, ViewGroup parent) {

    view=convertView;           
    LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = vi.inflate(R.layout.row_multiphoto_item, null);

    final ImageView imageView = (ImageView) view.findViewById(R.id.imageView1);
    final TextView click_count=(TextView)view.findViewById(R.id.count);
    imageLoader.displayImage("file://" + imageUrls.get(position), imageView, options, new SimpleImageLoadingListener() {
        @Override
        public void onLoadingComplete(Bitmap loadedImage) {
            Animation anim = AnimationUtils.loadAnimation(ImageGridActivity.this, R.anim.fade_in);
            imageView.setAnimation(anim);
            anim.start();
        }
    });

    // click_count.setVisibility(View.VISIBLE);
    view.setOnClickListener(new OnClickListener() {

        int c=0;

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            image_url.add(imageUrls.get(position));
            count.setText("("+image_url.size()+")");
            c=c+1;
            click_count.setTag(view);
            click_count.setVisibility(View.VISIBLE);
            click_count.setText(String.valueOf(c));
            count.setText("("+image_url.size()+")");
            Toast.makeText(getApplicationContext(), "cuy;;; "+position, 89).show();
        }
    });
//}
    return view;
}
}

1 个答案:

答案 0 :(得分:1)

请尝试这种方式,希望这有助于您解决问题。

public class ImageAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<HashMap<String,String>> list;
    private Animation anim;

    public ImageAdapter(Context context, ArrayList<String> imageList){
        this.context = context;
        list = new ArrayList<HashMap<String, String>>();
        for (String image : imageList){
            HashMap<String,String> row =  new HashMap<String, String>();
            row.put("image",image);
            row.put("count","0");
            list.add(row);
        }
        anim = AnimationUtils.loadAnimation(context, R.anim.fade_in);
    }

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

    @Override
    public Object getItem(int position){
        return list.get(position);
    }

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

    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null){
            holder =new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.row_multiphoto_item, null);
            holder.imageView = (ImageView)convertView.findViewById(R.id.imageView1);
            holder.click_count = (TextView)convertView.findViewById(R.id.count);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        holder.imageView.setAnimation(anim);
        imageLoader.displayImage("file://"+list.get(position).get("image").toString(), holder.imageView, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(Bitmap loadedImage) {
                anim.start();
            }
        });
        holder.click_count.setText(String.valueOf(list.get(position).get("count")));
        if(list.get(position).get("count").equals("0")){
            holder.click_count.setVisibility(View.INVISIBLE);
        }else{
            holder.click_count.setVisibility(View.VISIBLE);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.get(position).put("count",String.valueOf(Integer.parseInt(list.get(position).get("count"))+1));
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    class ViewHolder{
        ImageView imageView;
        TextView click_count;
    }
}