簡體   English   中英

如何在android中的GridView中解決滾動問題?

[英]How to solve scroll issue in GridView in android?

我正在嘗試開發一個簡單的訂單應用程序。

這是我的示例應用

在這里,我想為每個孩子添加/取消產品(列表項)。

public class CustomAdapter extends BaseAdapter implements OnClickListener {

    private Activity activity;
    private ArrayList data;
    private static LayoutInflater inflater = null;
    public Resources res;
    ListModel tempValues = null;

    int i = 0;
    static int count = 0;
    static int tot_amt = 0;

    ImageButton btn;
    ViewHolder holder;
    int pos;

    public CustomAdapter(Activity a, ArrayList d, Resources resLocal) {

        activity = a;
        data = d;
        res = resLocal;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() {

        if (data.size() <= 0)
            return 1;
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

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

    public static class ViewHolder {

        public TextView text;
        public TextView text1;
        public TextView textWide;
        public TextView tvCounter;

        public ImageView image;
        ImageButton button;
        ImageButton decreesButton2;

    }

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

        View vi = convertView;
        pos = position;
        if (convertView == null) {

            vi = inflater.inflate(R.layout.tabitem, null);
            btn = (ImageButton) vi.findViewById(R.id.button1);

            holder = new ViewHolder();
            holder.text = (TextView) vi.findViewById(R.id.text);
            holder.text1 = (TextView) vi.findViewById(R.id.text1);
            holder.image = (ImageView) vi.findViewById(R.id.list_image);
            holder.tvCounter = (TextView) vi.findViewById(R.id.counter);
            holder.button = (ImageButton) vi.findViewById(R.id.button1);
            holder.decreesButton2 = (ImageButton) vi.findViewById(R.id.button2);

            vi.setTag(holder);
        } else
            holder = (ViewHolder) vi.getTag();

        if (data.size() <= 0) {
            holder.text.setText("No Data");

        } else {

            tempValues = null;
            tempValues = (ListModel) data.get(position);

            holder.text.setText(tempValues.getProductName());
            holder.text1.setText(tempValues.getPrice());
            holder.image.setScaleType(ScaleType.FIT_XY);
            byte[] outImage=tempValues.getImage();
            ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
            Bitmap theImage = BitmapFactory.decodeStream(imageStream);
            holder.image.setImageBitmap(theImage);



            /*holder.image.setImageResource(res.getIdentifier(
                    "com.list.listviewexample:drawable/"
                            + tempValues.getImage(), null, null));*/


            vi.setOnClickListener(new OnItemClickListener(position));

            holder.button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    View parentView = (View) v.getParent();

                    int totAmount = Integer.parseInt(((TextView) parentView.findViewById(R.id.text1)).getText().toString());

                    count = Integer.parseInt(((TextView) parentView.findViewById(R.id.counter)).getText().toString());
                    count++;
                    tot_amt = totAmount * count;

                    ((TextView) parentView.findViewById(R.id.counter)).setText(String.valueOf(count));

                    ((TextView) parentView.findViewById(R.id.totalPrice)).setText(String.valueOf(tot_amt));

                    ListViewExample sct = (ListViewExample) activity;
                    sct.getAllValues();

                }

            });

            holder.decreesButton2.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    View parentView = (View) v.getParent();
                    int totAmount = Integer.parseInt(((TextView) parentView.findViewById(R.id.text1)).getText().toString());

                    count = Integer.parseInt(((TextView) parentView.findViewById(R.id.counter)).getText().toString());
                    if (count > 0)
                        count--;

                    tot_amt = totAmount * count;

                    ((TextView) parentView.findViewById(R.id.totalPrice)).setText(String.valueOf(tot_amt));
                    ((TextView) parentView.findViewById(R.id.counter)).setText(String.valueOf(count));
                    ListViewExample sct = (ListViewExample) activity;
                    sct.getAllValues();
                }

            });
        }
        return vi;
    }

    @Override
    public void onClick(View v) {

    }

    private class OnItemClickListener implements OnClickListener {
        private int mPosition;

        OnItemClickListener(int position) {
            mPosition = position;
        }

        @Override
        public void onClick(View arg0) {

            ListViewExample sct = (ListViewExample) activity;
            sct.onItemClick(mPosition);
        }
    }
}

現在,一切正常。 我的問題是當我滾動GridView時,總項目和總價格會自動增加和減少。 如果我添加1個比薩餅,然后向下滾動,則該比薩餅的總價會降低,並自動添加漢堡包項目。

請幫忙。 我錯了。 請允許我在GridView中添加/取消項目的任何好方法。

ListView / GridView重用視圖。 在你的情況下,這意味着當你有1個比薩時,你向下滾動以便比薩消失,列表視圖會重新使用該視圖以顯示下一個視圖,因此你將自動擁有一個項目:1披薩的觀點。

你需要的是存儲每個項目的計數,並在getView中相應地設置tvCounter。

所以,最后我找到了滾動問題的解決方案..

vi.setOnClickListener(new OnItemClickListener(position));
            final int pos = position;
            holder.button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    View parentView = (View) v.getParent();

                    int totAmount = Integer.parseInt(((TextView) parentView
                            .findViewById(R.id.text1)).getText()
                            .toString());

                    count = Integer.parseInt(((TextView) parentView
                            .findViewById(R.id.counter)).getText().toString());
                    count++;
                    tot_amt = totAmount * count;

                    ((TextView) parentView.findViewById(R.id.counter))
                            .setText(String.valueOf(count));

                    ((TextView) parentView.findViewById(R.id.totalPrice))
                            .setText(String.valueOf(tot_amt));

                    //Here I am saving the total amount and quantity
                    tempValues = null;
                    tempValues = (ListModel) data.get(pos);
                    tempValues.setProductName(tempValues.getProductName());
                    tempValues.setPrice(tempValues.getPrice());
                    tempValues.setImage(tempValues.getImage());
                    tempValues.setQuantity(String.valueOf(count));
                    tempValues.setTotalAmount(String.valueOf(tot_amt));
                    data.set(pos, tempValues);
                    ListViewExample sct = (ListViewExample) activity;
                    sct.getAllValues();
                }

            });

            holder.decreesButton2.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

                    View parentView = (View) v.getParent();
                    int totAmount = Integer.parseInt(((TextView) parentView
                            .findViewById(R.id.text1)).getText()
                            .toString());

                    count = Integer.parseInt(((TextView) parentView
                            .findViewById(R.id.counter)).getText().toString());
                    if (count > 0) {
                        count--;

                        tot_amt = totAmount * count;

                        ((TextView) parentView.findViewById(R.id.totalPrice))
                                .setText(String.valueOf(tot_amt));
                        ((TextView) parentView.findViewById(R.id.counter))
                                .setText(String.valueOf(count));

                    //Here I am saving the total amount and quantity
                        tempValues = null;
                        tempValues = (ListModel) data.get(pos);
                        tempValues.setProductName(tempValues
                                .getProductName());
                        tempValues.setPrice(tempValues.getPrice());
                        tempValues.setImage(tempValues.getImage());
                        tempValues.setQuantity(String.valueOf(count));
                        tempValues.setTotalAmount(String.valueOf(tot_amt));
                        data.set(pos, tempValues);
                        ListViewExample sct = (ListViewExample) activity;
                        sct.getAllValues();
                    }
                }

            });

謝謝Longi .. !!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM