如何解决 android 中 GridView 的滚动问题?
How to solve scroll issue in GridView in android?
我正在尝试开发一个简单的订单应用程序。
这里我想 add/cancel 每个 child(列表项)的产品。
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 个 Pizza 并向下滚动,则 Pizza 总价将降低并自动添加汉堡项目。
请帮忙。我哪里做错了。请让我有什么好的方法 add/cancel GridView 中的项目。
ListView/GridView 重用视图。在你的情况下,这意味着什么时候。你有 1 个 Pizza,向下滚动使 Pizza 消失,列表视图为出现的下一个视图重用该视图,因此你将自动拥有一个 Items: 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();
}
}
});
谢谢隆基..!!
我正在尝试开发一个简单的订单应用程序。
这里我想 add/cancel 每个 child(列表项)的产品。
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 个 Pizza 并向下滚动,则 Pizza 总价将降低并自动添加汉堡项目。
请帮忙。我哪里做错了。请让我有什么好的方法 add/cancel GridView 中的项目。
ListView/GridView 重用视图。在你的情况下,这意味着什么时候。你有 1 个 Pizza,向下滚动使 Pizza 消失,列表视图为出现的下一个视图重用该视图,因此你将自动拥有一个 Items: 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();
}
}
});
谢谢隆基..!!