Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

button - Android - Different items in GridView not showing up

I am working on app which has a GridView of images but the last item in GridView is a button that loads more item into Grid. But i am facing a strange issue and that is Button shows up sometime and sometime it doesn't specially when i scroll up and down really fast on GridView. I think it's because of recycle issue but i don't know how to fix it.

Here is my code:-

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

    }
}

Any help would be really really appreciate.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Maybe you can try delete row==null and holder = (ViewHolder)row.getTag(); it's because of recycle issue,you can have a try first.But it will affect efficiency. Another thing i want to say,how do position==thumbUrls.size();? Maybe it's position==thumbUrls.size()-1;.Ok,that's my view,i help it work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...