我有一個ListView,我使用setScrollY滾動到特定的位置。出於某種原因,如果我使用setScrollY滾動到應該顯示第七項的座標,它是空白的。列表中有6個以上的項目。使用setScrollY作爲ListView,適配器的setView不顯示正確的項目
我把一個日誌在getView上,它只處理第1個6項。
在屏幕上只有6個列表項可見。
什麼需要發生getView來處理其他項目?
如果我手動滾動ListView,getView會正確處理其他項目。
我的活動代碼如下:
public class SampleActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choose_bride_groom_or_guest);
lvImages = (ListView) findViewById(R.id.image_list);
lvImages.setEnabled(false);
drawableIds = new ArrayList<Integer>();
drawableIds.add(R.drawable.auth_background_row_1);
drawableIds.add(R.drawable.auth_background_row_2);
drawableIds.add(R.drawable.auth_background_row_3);
drawableIds.add(R.drawable.auth_background_row_4);
drawableIds.add(R.drawable.auth_background_row_5);
drawableIds.add(R.drawable.auth_background_row_6);
drawableIds.add(R.drawable.auth_background_row_7);
drawableIds.add(R.drawable.auth_background_row_8);
drawableIds.add(R.drawable.auth_background_row_9);
adapter = new AuthBackgroundImageAdapter(this, drawableIds);
lvImages.setAdapter(adapter);
int scrollY = //some number which is higher than the screen height.
lvImages.setScrollY(scrollY);
}
}
適配器
public class AuthBackgroundImageAdapter extends BaseAdapter {
private static final String TAG = "AuthBackgroundImageAdapter";
private final LayoutInflater inflater;
Context context;
List<Integer> drawableIds;
class Holder {
ImageView ivBackground;
}
public AuthBackgroundImageAdapter(Context context, List<Integer> drawableIds) {
this.drawableIds = drawableIds;
this.context = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(Integer drawableId) {
drawableIds.add(drawableId);
}
@Override
public int getCount() {
return drawableIds.size();
}
@Override
public Object getItem(int i) {
return drawableIds.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
Holder holder;
if (convertView == null) {
view = inflater.inflate(R.layout.item_auth_background_image, null);
holder = new Holder();
holder.ivBackground = (ImageView) view.findViewById(R.id.auth_background_image);
view.setTag(holder);
} else {
holder = (Holder) view.getTag();
}
Integer item = (Integer) this.getItem(position);
holder.ivBackground.setImageDrawable(context.getResources().getDrawable(item));
Log.d(TAG, "getView: position " + String.valueOf(position));
Log.d(TAG, "getView: drawable " + String.valueOf(item));
return view;
}
}
佈局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent" >
<ListView
android:id="@+id/image_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
你能添加在佈局contined activity_choose_bride_groom_or_guest – lory105
@ lory105列表視圖的XML代碼:我添加的佈局。謝謝! – dannyroa
嘗試使類持有者像這樣靜態:static class Holder –