在这个自定义适配器中,在我的列表视图中只有一个imageview。我在这个列表视图中加载了一个图像列表。我想在listview的那一行上更改图像,用户停止滚动。我怎么能得到这个结果。感谢您提前解决任何问题。
class MyAdapter extends BaseAdapter {
private Context context;
private int images[];
// public boolean isScrolling;
public MyAdapter(Context context, int images[]) {
this.context = context;
this.images = images;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return images[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
class MyViewHolder {
ImageView imageView;
public MyViewHolder(View v) {
// TODO Auto-generated constructor stub
imageView = (ImageView) v.findViewById(R.id.imgView);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
MyViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.custom_list, parent, false);
holder = new MyViewHolder(row);
row.setTag(holder);
} else {
holder = (MyViewHolder) row.getTag();
row = convertView;
}
holder.imageView.setImageResource(images[position]);
return row;
}
}
这是我的MainActivity,我在调用MyAdapter来获取图像列表。
public class MainActivity extends Activity {
private ListView listView;
private MyAdapter myAdapter;
int[] images = { R.drawable.img001, R.drawable.img002, R.drawable.img003,
R.drawable.img004, R.drawable.img005, R.drawable.img006,
R.drawable.img007, R.drawable.img008, R.drawable.img009,
R.drawable.img010, R.drawable.img011, R.drawable.img012,
R.drawable.img013, R.drawable.img014, R.drawable.img015,
R.drawable.img016, R.drawable.img017, R.drawable.img018,
R.drawable.img019, R.drawable.img020, R.drawable.img021,
R.drawable.img022, R.drawable.img023, R.drawable.img024,
R.drawable.img025, R.drawable.img026, R.drawable.img027,
R.drawable.img028, R.drawable.img029, R.drawable.img030,
R.drawable.img031, R.drawable.img032, R.drawable.img033,
R.drawable.img034, R.drawable.img035, R.drawable.img036,
R.drawable.img037, R.drawable.img038, R.drawable.img039,
R.drawable.img040, R.drawable.img041, R.drawable.img042,
R.drawable.img043, R.drawable.img044, R.drawable.img045,
R.drawable.img046, R.drawable.img047, R.drawable.img048,
R.drawable.img049, R.drawable.img050, R.drawable.img051,
R.drawable.img052, R.drawable.img053, R.drawable.img054,
R.drawable.img055, R.drawable.img056, R.drawable.img057,
R.drawable.img058, R.drawable.img059, R.drawable.img060,
R.drawable.img061, R.drawable.img062, R.drawable.img063,
R.drawable.img064, R.drawable.img065, R.drawable.img066,
R.drawable.img067, R.drawable.img068, R.drawable.img069,
R.drawable.img070, R.drawable.img071, R.drawable.img072,
R.drawable.img073, R.drawable.img074, R.drawable.img075,
R.drawable.img076, R.drawable.img077, R.drawable.img078,
R.drawable.img079, R.drawable.img080, R.drawable.img081,
R.drawable.img082, R.drawable.img083, R.drawable.img084,
R.drawable.img085, R.drawable.img086, R.drawable.img087,
R.drawable.img088, R.drawable.img089, R.drawable.img090,
R.drawable.img091, R.drawable.img092, R.drawable.img093,
R.drawable.img094, R.drawable.img095, R.drawable.img096,
R.drawable.img097, R.drawable.img098, R.drawable.img099,
R.drawable.img100 };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myAdapter = new MyAdapter(this, images);
listView = (ListView) findViewById(R.id.imgList);
listView.setAdapter(myAdapter);
}
}
答案 0 :(得分:0)
尝试使用setOnScrollListener()
并使用onScrollStateChanged
实施scrollState == SCROLL_STATE_IDLE
现在您有firstVisibleItem
,visibleItemCount
,totalItemCount
setOnScrollListener(new OnScrollListener(){
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if(scrollState == SCROLL_STATE_IDLE){
Log.i("a", "scrolling stopped...");
}
});
}