3

我正在开发具有图像 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();
        }

    }
}

任何帮助都会非常感激。

谢谢。

4

2 回答 2

0

也许你可以试试delete row==nullholder = (ViewHolder)row.getTag(); 因为回收问题,你可以先试一试。但这会影响效率。还有一点我想说,怎么办position==thumbUrls.size();?也许它是。position==thumbUrls.size()-1;好吧,这是我的观点,我帮助它工作。

于 2012-10-15T02:15:58.583 回答
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;
于 2012-10-15T06:10:03.490 回答