代码之家  ›  专栏  ›  技术社区  ›  Balvinder Singh

Android中滚动显示的网格视图文本

  •  1
  • Balvinder Singh  · 技术社区  · 10 年前

    我正在制作一个网格视图,它使用图库中的图像加载器概念显示图像。在网格视图项中有两个视图,一个是图像,另一个是文本视图。默认情况下,文本视图不可见。我的需要是,当我点击网格视图项目时,文本视图应该是可见的,它将显示在网格的特定项目上的点击次数。但问题是,当我单击网格项时,文本视图会显示点击次数,但当滚动网格视图时,文本窗口会消失。两天以来我一直在忙这个,请帮我解决这个问题。

    这是我的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 回复  |  直到 10 年前
        1
  •  1
  •   Hareshkumar Chhelana    10 年前

    试着这样做,希望这能帮助你解决问题。

    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;
        }
    }