我有一个ListView,我使用setScrollY滚动到一个特定的位置。出于某种原因,如果我使用setScrollY滚动到应该显示第7个项目的坐标,则它是空白的。我的名单上有6个以上的项目。
我在getView上放了一个日志,它只处理前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"
/>
答案 0 :(得分:0)
尝试使用android:layout_height
值更改wrap_content
参数:
<ListView
android:id="@+id/image_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>