我创建了包含 20 个项目的 gridview。我已经使用自定义适配器来绑定这个 gridview。当我在 gridview 中滚动时,项目的索引已更改,或者有时某些项目已不可见。N 有时整个网格视图是不可见的。不知道它是怎么回事。
public class MyAdapter extends BaseAdapter {
final int NumberOfItem = 90;
private Bitmap[] bitmap = new Bitmap[NumberOfItem];
View grid;
private Context context;
private LayoutInflater layoutInflater;
MyAdapter(Context c){
context = c;
layoutInflater = LayoutInflater.from(context);
//init dummy bitmap,
//using R.drawable.icon for all items
/* for(int i = 1; i < NumberOfItem; i++){
bitmap[i] = BitmapFactory.decodeResource(context.getResources(), R.drawable.image1);
}*/
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageIDs.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null){
grid = new View(context);
grid = layoutInflater.inflate(R.layout.main, null);
ImageView imageView = (ImageView)grid.findViewById(R.id.image);
imageView.setBackgroundResource(imageIDs[position]);
TextView textView = (TextView)grid.findViewById(R.id.text);
textView.setText(names[position]);
}else{
grid = (View)convertView;
}
return grid;
}
Integer[] imageIDs = {
R.drawable.attorneys_180, R.drawable.auto_repair_180, R.drawable.coffee_180, R.drawable.gas_stations_180,
R.drawable.grocery_180, R.drawable.hotels_180, R.drawable.locksmith_180, R.drawable.nightclubs_180,
R.drawable.plumbers_180
};
String[] names = {"Attorneys","Auto Repair","Coffee","Gas Stations","Grocery","Hotels","Locksmith","NightClubs","Plumbers"};
}
main.xml 的内容
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="center">
<ImageView
android:id="@+id/image"
android:layout_width="67dp"
android:layout_height="67dp" android:background="@drawable/ic_launcher"/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/image"
android:layout_below="@+id/image"
android:text="gsdfgsd"
android:textColor="#000000" android:gravity="center"/>
</RelativeLayout>
给我正确的解决方案。
谢谢,杰伊帕特尔