gpt4 book ai didi

android - 屏幕滚动时,GridView 中的项目会重复

转载 作者:IT王子 更新时间:2023-10-28 23:33:16 26 4
gpt4 key购买 nike

我正在使用 GridView 来显示一组用户可以选择的类别。网格的每个项目都由一个 ImageView 和一个 TextView 组成,两者都从服务器检索。当一个项目被触摸时,另一个 Activity 被启动。

我以为一切正常,直到我注意到滚动屏幕时有些项目重复出现。每当我在网格中向下滚动,然后返回时,它就会改变它的位置并被复制。但即使我触摸了困惑的项目,正确的值也会发送到下一个 Activity 。

在 LogCat 中,任何对服务器的重复请求都会发生。事实上,我在滚动时得到了这个:

06-28 12:36:38.554: D/dalvikvm(358): GC_EXTERNAL_ALLOC freed 2061 objects / 156024 bytes in 51ms
06-28 12:36:42.915: D/dalvikvm(358): GC_FOR_MALLOC freed 6590 objects / 737528 bytes in 57ms
06-28 12:38:26.725: D/dalvikvm(358): GC_EXTERNAL_ALLOC freed 5426 objects / 468176 bytes in 71ms
06-28 12:38:26.875: D/dalvikvm(358): GC_EXTERNAL_ALLOC freed 409 objects / 17480 bytes in 68ms

看起来每次我滚动时,都会重绘...

更新:它只会在我第一次向下滚动 GridView 时重绘。在此之后,所有的元素,包括重复的元素,都保持原位。

我的java类:

public void proccess(){
int qtdCategorias = json.length();
imagens = new Drawable[qtdCategorias];
categorias = new String[qtdCategorias];
for (int i=0; i<qtdCategorias; i++){
JSONArray c = json.optJSONArray(i);
String urlAmigavel = null;
String imagemSite = null;
String nomeCategoria = null;
try {
urlAmigavel = c.getString(6);
imagemSite = c.getString(3);
nomeCategoria = c.getString(2);
} catch (JSONException e) {
Log.e("CategoriasJogarActivity", e.toString());
e.printStackTrace();
}
categorias[i] = nomeCategoria;
imagens[i] = getImagem(urlAmigavel, imagemSite);
}


gridview = (GridView) findViewById(R.id.include3);

ImageAdapter imageAdapter = new ImageAdapter(ctx, imagens, categorias);

gridview.setAdapter(imageAdapter);

gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {

String name = null;
String idt = null;
try {
JSONArray c = json.optJSONArray(position);
name = c.getString(2);
idt = c.getString(0);
} catch (JSONException e) {
Log.e("CategoriasJogarActivity",
"JSONException" + e.toString());
}

Intent in = new Intent(getApplicationContext(),
JogarActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_ID, idt);
in.putExtra(TAG_PRIMEIRAPERGUNTA, true);
startActivity(in);

}
});
}


public Drawable getImagem(String urlAmigavel, String img) {
String url = "http://www.qranio.com/pergunta/" + urlAmigavel + "/"+ img;
InputStream is = null;
try {
URL urlImagem = new URL(url);
is = (InputStream) getObjeto(urlImagem);
} catch (MalformedURLException e1) {
Log.e("CategoriasJogarActivity", e1.toString());
e1.printStackTrace();
}
Drawable d = Drawable.createFromStream(is, "src");

return d;
}

private Object getObjeto(URL url) {
Object content = null;
try {
content = url.getContent();
} catch (IOException e) {
Log.e("CategoriasJogarActivity", e.toString());
e.printStackTrace();
}
return content;
}

imageAdapter 类

public class ImageAdapter extends BaseAdapter{
private Context mContext;
private final Drawable[] mThumbIds;
private final String[] mTextIds;


public ImageAdapter(Context c, Drawable[] d, String[] s) {
mContext = c;
mThumbIds = d;
mTextIds = s;
}

public int getCount() {
return mThumbIds.length;
}

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

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

//create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
//ImageView imageView;
View v;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
v = inflater.inflate(R.layout.gridview_item_layout, null);
TextView text = (TextView)v.findViewById(R.id.grid_item_text);
text.setText(mTextIds[position]);
ImageView image = (ImageView)v.findViewById(R.id.grid_item_image);
image.setImageDrawable(mThumbIds[position]);


} else {

v = (View) convertView;
}


return v;
}


}

gridview_item_layout xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview_item_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal" >

<ImageView android:id="@+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:minHeight="100dip"
android:minWidth="100dip"
>
</ImageView>

<TextView android:id="@+id/grid_item_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView"
android:gravity="center"
android:textColor="#F9A512"
android:textStyle="bold"
android:textSize="18dp"
>
</TextView>
</LinearLayout>

GridView xml

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dip"
android:horizontalSpacing="10dip"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="#FFFFFF"
android:padding="5dip"
/>

我看到了关于同一问题的其他问题,但没有一个回答。对正在发生的事情有任何想法吗?

最佳答案

向下滚动 GridView 时看到相同的项目是正常的,因为在 getView 方法中,您为 ImageView 设置了可绘制对象仅当 convertViewnull 时(例如,当 GridView 出现在屏幕上时看到的第一个元素)。如果 convertView 不是 null,这意味着你有一个回收的行 View ,你没有设置正确的图像,你仍然使用之前在这个回收的图像上设置的图像看法。尝试像这样修改 getView 方法:

public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
v = inflater.inflate(R.layout.gridview_item_layout, parent, false);
} else {
v = (View) convertView;
}
TextView text = (TextView)v.findViewById(R.id.grid_item_text);
text.setText(mTextIds[position]);
ImageView image = (ImageView)v.findViewById(R.id.grid_item_image);
image.setImageDrawable(mThumbIds[position]);
return v;
}

单击一个元素会显示正确的项目,因为您使用 position 参数来检索数据。

关于android - 屏幕滚动时,GridView 中的项目会重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11245620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com