如何在Android的解决GridView的滚动发行? [英] How to solve scroll issue in GridView in android?
问题描述
我想开发一个简单的命令应用程序。
I am trying to develop a simple order app.
在这里,我想添加/取消产品每一个孩子(列表项)。
Here I want to add/cancel the product for every child(List Item).
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比萨,如果我向下滚动,比萨饼总价格将下降,并自动汉堡添加项目。
Now every thing is working fine. My problem is when I scroll the GridView, the total Items and total price is automatically increased and decreased. If I add 1 Pizza and if I scroll down, that Pizza total price will decreased and automatically burger item is added.
请帮忙。我在那里做了错误。请让我在GridView中添加/取消的项目没有什么好办法。
Please help. Where I did mistake. Please let me any good way to add/cancel the items in the GridView.
推荐答案
的ListView / GridView的重用意见。这意味着,你的情况是,当如你有1比萨,你向下滚动所以比萨消失,列表视图重用的视图出现的下一个视图,所以你会自动获得一个的产品:1 的被遗留下来的比萨的看法
ListView/GridView reuses views. What this means in your case is that when e.g. you have 1 Pizza, you scroll down so the Pizza disappears, the listview reuses that view for the next view that appears, so you'll automatically have a Items: 1 that was left over from the Pizza's view.
您有什么需要的就是将计为每个项目,并设置相应的tvCounter在getView。
What you need to to is store the count for every item and set tvCounter accordingly in getView.
这篇关于如何在Android的解决GridView的滚动发行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!