我有一个listView,我需要当用户点击某个项目时显示5个图像,当他点击另一个项目时,它会显示5个不同的图像与ViewPager ..我不知道如何更改图像数组这会随listView的每个项目而改变吗?有什么想法吗?
我的代码:
public class SwipeActivity extends Activity{
ViewPager vp;
Table_customSwipeAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_swipe);
vp=(ViewPager)findViewById(R.id.view_pager);
adapter=new Table_customSwipeAdapter(this);
vp.setAdapter(adapter);
}
public void imageSwipe(View view){
Toast toast = Toast.makeText(getApplicationContext(), "Open Link!", Toast.LENGTH_SHORT);
toast.show();
}
}
class Table_customSwipeAdapter extends PagerAdapter{
int[] image_resources={R.drawable.i1,R.drawable.i2,R.drawable.i3,R.drawable.i4,R.drawable.i5};
private Context ctx;
private LayoutInflater layoutInflater;
public Table_customSwipeAdapter(Context ctx) {
this.ctx=ctx;
}
@Override
public int getCount() {
return image_resources.length;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return (arg0==(LinearLayout)arg1);
}
public Object instantiateItem(ViewGroup container, int position){
layoutInflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view=layoutInflater.inflate(R.layout.activity_table_custom_swipe_adapter, container,false);
ImageView imageView=(ImageView)item_view.findViewById(R.id.image_view);
TextView textView=(TextView)item_view.findViewById(R.id.image_count);
imageView.setImageResource(image_resources[position]);
textView.setText("Image "+(position+1));
container.addView(item_view);
return item_view;
}
public void destroyItem(ViewGroup container, int position, Object object){
container.removeView((LinearLayout)object);
}
}
在这里,ViewPager显示了相同的5张图片..
答案 0 :(得分:0)
您可以在ViewPager
中定义一个Activity
可以实现的回调接口。在instantiateItem
,ViewPager
可以通过此回调调用Activity
中的方法,以获取点击的ListView
位置。根据该信息,它可以显示图像。
答案 1 :(得分:0)
//In your Table_customSwipeAdapter make these chages in contructor get the image array e.g
class Table_customSwipeAdapter extends PagerAdapter{
int[] image_resources;
private Context ctx;
private LayoutInflater layoutInflater;
public Table_customSwipeAdapter(Context ctx, int[] image_resources) {
this.ctx=ctx;
this.image_resources=image_resources
}
// In your SwipeActivity while calling the constructor pass the int array
adapter=new Table_customSwipeAdapter(this,image_resources);
答案 2 :(得分:0)
它显示相同的图像,因为这里
imageView.setImageResource(image_resources[position]);
您正在设置相同的图像
首先,您需要创建不同的Lists
图像,以便在“活动”中的每次不同点击中显示这些图像
public class SwipeActivity extends Activity{
ViewPager vp;
Table_customSwipeAdapter adapter;
int[] image_resources1 ={R.drawable.i1,R.drawable.i2,R.drawable.i3};
int[] image_resources2 ={R.drawable.i4,R.drawable.i5};
然后在你的适配器
class Table_customSwipeAdapter extends PagerAdapter{
int[] image_resources;
private Context ctx;
private LayoutInflater layoutInflater;
public Table_customSwipeAdapter(Context ctx, int[] image_resources) {
this.ctx=ctx;
this.image_resources = image_resources;
}
然后在每次点击时将不同的列表传递给您的适配器,如此
vp=(ViewPager)findViewById(R.id.view_pager);
//HERE YOU WILL PASS WHICHEVER LIST YOU WANT TO DISPLAY e.g image_resources1 or image_resources2 or ANY OTHER
adapter=new Table_customSwipeAdapter(this,image_resources);
vp.setAdapter(adapter);
希望有所帮助