Android - GridView中的不同项目未显示

时间:2012-10-15 01:20:42

标签: android button gridview recycle

我正在使用具有GridView图像的应用程序,但GridView中的最后一项是一个将更多项目加载到Grid中的按钮。但是我面临一个奇怪的问题,那就是Button会在某个时候显示出来,有时当它在GridView上快速向上和向下滚动时它并不特别。我认为这是因为回收问题,但我不知道如何解决它。

这是我的代码: -

public class ButtonGridAdapter extends BaseAdapter implements OnCheckedChangeListener, OnClickListener {


    private ArrayList<String> thumbUrls;
    private ArrayList<String> mainUrls;
    private LayoutInflater layoutInflater;
    private Context context;
    private Intent intent;
    private ArrayList<String> positions = new ArrayList<String>();
    private static ArrayList<String> urlsToUpload = new ArrayList<String>(); 
    private int width;

    public ButtonGridAdapter(Context context, ArrayList<String> thumbUrls, ArrayList<String> mainUrls, String clicked)
    {
        this.context = context;
        this.thumbUrls = thumbUrls;
        this.mainUrls = mainUrls;
        intent = new Intent(context, GalleryView.class);
        this.layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        DisplayMetrics displaymetrics = new DisplayMetrics();
        ((Activity) context).getWindow().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        this.width = displaymetrics.widthPixels;
        System.out.println(width+" width");
    }

    @Override
    public int getCount() {
        return thumbUrls.size()+1;
    }

    @Override
    public Object getItem(int arg0) {
        return thumbUrls.get(arg0);
    }

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

    public static class ViewHolder
    {
        ImageView mainImage;
        CheckBox checkBox;
        Button button;
    }

    public static class ButtonHolder
    {

    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;
        if(row==null || row.getTag().equals("button"))
        {
                row = layoutInflater.inflate(R.layout.griditem, parent, false);
                holder = new ViewHolder();
                holder.mainImage = (ImageView)row.findViewById(R.id.itemgrid);
                holder.checkBox = (CheckBox)row.findViewById(R.id.itemselected);
                holder.button = null;
                row.setTag(holder);
        }
        else 
        {
            holder = (ViewHolder)row.getTag();
        }

        if(position==thumbUrls.size())
        {
            int width = row.getWidth();
            int height = row.getHeight()-70;
            row = layoutInflater.inflate(R.layout.mybutton, parent, false);
            holder.button = (Button)row.findViewById(R.id.gridbutton);
            holder.button.setText("Load More");
            LayoutParams params = holder.button.getLayoutParams();
            params.height = height;
            params.width = width;
            holder.button.requestLayout();
            holder.button.setOnClickListener(this);
            System.out.println("Called");
            row.setTag("button");
            return row;
        }
        else
        {
            setImageSize(holder.mainImage);
            System.out.println(position);
            holder.mainImage.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    intent.putExtra("position", position);
                    intent.putStringArrayListExtra("MainUrls", mainUrls);
                    intent.putStringArrayListExtra("Thumbnails", thumbUrls);
                    context.startActivity(intent);
                }
            });

            holder.checkBox.setOnCheckedChangeListener(null);
            holder.checkBox.setTag(position);

            holder.checkBox.setChecked(positions.contains(thumbUrls.get(position)));
            holder.checkBox.setOnCheckedChangeListener(this);

            UrlImageViewHelper.setUrlDrawable(holder.mainImage, thumbUrls.get(position), R.drawable.extras_load);
            return row;
        }


    }

    private void setImageSize(ImageView imageView) {
        LayoutParams params = imageView.getLayoutParams();
        int leftWidth = width - 80;
        params.width = leftWidth/3;
        params.height = leftWidth/3;
        imageView.requestLayout();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked)
        {
            positions.add(thumbUrls.get((Integer)buttonView.getTag()));
            urlsToUpload.add(mainUrls.get((Integer)buttonView.getTag()));
        }

        else
        {
            positions.remove(thumbUrls.get((Integer)buttonView.getTag()));
            urlsToUpload.remove(mainUrls.get((Integer)buttonView.getTag()));
        }
    }

    public static ArrayList<String> getUrlsToUpload()
    {
        return urlsToUpload;
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.gridbutton)
        {
            InstagramImpl.getInstance().fetchMorePhotos();
        }

    }
}

任何帮助都会非常感激。

感谢。

2 个答案:

答案 0 :(得分:0)

也许您可以尝试delete row==nullholder = (ViewHolder)row.getTag();因为回收问题,您可以先尝试一下。但这会影响效率。我想说的另一件事,position==thumbUrls.size();怎么办?也许它是position==thumbUrls.size()-1;。好的,这是我的观点,我帮助它发挥作用。

答案 1 :(得分:0)

我在其他答案中发布了完整的getView方法。很简单,先检查一下位置。如果它是最后一项(加载更多按钮)立即返回布局。

private final Media special = new Media("-1", "", "", "", ""); 


    public MediaGridAdapter(Context context, int imageID, ArrayList<Media> array, int type, boolean isGetMore) {

        list = array;
        mediaType = type;
        this.context = context;

            if(list != null) {
                list.add(special);
            }


    }       



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

                RelativeLayout item;
                ViewHolder  holder;
//this part check it's the last item. -1 is my special item id in contructor.
                if(list.get(position).getId().equals("-1")) {

                        item = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.item_special_more, null);
                        item.setTag(MORE_BUTTON);
                    return item;
                }
//

                if(convertView == null || convertView.getTag().toString().equals(MORE_BUTTON)) {

                    Context context = parent.getContext();
                    item = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.item_with_icon_vertical, null);
                    holder = new ViewHolder();
                    holder.thumbnail = (ImageView) item.findViewById(R.id.iv_icon_media);

                    item.setTag(holder);

                } else {

                    item = (RelativeLayout) convertView;
                    holder = (ViewHolder) item.getTag();
                }
            }
// set data.
    holder.thumbnail.setBitmap(...) 

    return item;