如何在按钮单击时将图像的第一个位置移动到ListView中的第二个位置,以便图像的最后位置到达第一个位置。
我在下面尝试过的代码;
public class MainActivity extends AppCompatActivity {
private int[] images = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5};
ArrayList<ImageModel> imageModels;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
imageModels = new ArrayList<ImageModel> ();
for (int i = 0; i < images.length; i++) {
ImageModel imgMode = new ImageModel ();
imgMode.setImageId (images[i]);
imageModels.add (imgMode);
Log.d ("chk",""+images[i]);
}
ListView lstView = (ListView)findViewById (R.id.lst_view);
lstView.setAdapter (new BaseAdapterr(MainActivity.this,imageModels));
Button btnClick = (Button)findViewById (R.id.btn_click);
btnClick.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View v) {
for (int i=0 ;i < imageModels.size ();i++){
}
}
});
}}
答案 0 :(得分:0)
尝试使用获取对象索引,然后使用setIndex或sth来更改索引。
拉茶
答案 1 :(得分:0)
直接滚动:
lstView.setSelection(0);
顺畅滚动:
lstView.smoothScrollToPosition(0);
因此您的代码应该遵循
btnClick.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View v) {
lstView.clearFocus();
lstView.post(new Runnable() {
@Override
public void run() {
//int index=0; set your position
lstView.setSelection(index);
}
});
}
});
答案 2 :(得分:0)
这是你的 btnClick听众,试一试:
Button btnClick = (Button)findViewById (R.id.btn_click);
btnClick.setOnClickListener (new View.OnClickListener () {
@Override
public void onClick (View v) {
...
ImageView firstImage= listView.getChiledAt(0);// get the firstimage
ImageView secnodImage= listView.getChiledAt(1);// get the second image
listView.addView(firstImage,2);// we added the first image to the second index
listView.addView(secondImage,1);// we added the second image to the first index
myAdapter.notifyDataSetChanged();// to refresh the view
...
}
});