gpt4 book ai didi

android - 获取 ViewPager 的当前位置

转载 作者:行者123 更新时间:2023-11-29 14:47:36 26 4
gpt4 key购买 nike

在我的程序中,我正在显示 GridView,当用户点击任何显示 ViewPager 的网格项目时

这里我想知道,我怎样才能显示总位置的当前位置,就像我们通常在图片库中看到的那样

例如:我想在 textView 中显示 ViewPager 的当前位置和网格项目的总数

 3 of 11

这就是我想要得到的:

enter image description here

MainActivity.java: GridView

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set title for the GridView
setTitle("GridView");
// Get the view from grid_view.xml
setContentView(R.layout.grid_view);

// Set the images from ImageAdapter.java to GridView
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

// Listening to GridView item click
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

// Launch ImageViewPager.java on selecting GridView Item
Intent i = new Intent(getApplicationContext(), ImageViewPager.class);

// Show a simple toast message for the item position
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();

// Send the click position to ImageViewPager.java using intent
i.putExtra("id", position);

// Start ImageViewPager
startActivity(i);
}
});
}

ImageViewPager.java:

public class ImageViewPager extends Activity {
// Declare Variable
int position;
TextView textView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set title for the ViewPager
setTitle("ViewPager");
// Get the view from view_pager.xml
setContentView(R.layout.view_pager);

// Retrieve data from MainActivity on item click event
Intent p = getIntent();
position = p.getExtras().getInt("id");

textView = (TextView) findViewById(R.id.textView1);

ImageAdapter imageAdapter = new ImageAdapter(this);
List<ImageView> images = new ArrayList<ImageView>();

// Retrieve all the images
for (int i = 0; i < imageAdapter.getCount(); i++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(imageAdapter.mThumbIds[i]);
imageView.setScaleType(ImageView.ScaleType.CENTER);
images.add(imageView);
}

// Set the images into ViewPager
ImagePagerAdapter pageradapter = new ImagePagerAdapter(images);
ViewPager viewpager = (ViewPager) findViewById(R.id.pager);
viewpager.setAdapter(pageradapter);
// Show images following the position
viewpager.setCurrentItem(position);
}
}

最佳答案

你应该在PageSelected上设置文本。

  viewPager.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
System.out.println("Current position=="+num);
textView.setText((arg0+1)+" of "+ images.size());
}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

System.out.println("onPageScrolled");
}

@Override
public void onPageScrollStateChanged(int num) {
// TODO Auto-generated method stub


}
});

关于android - 获取 ViewPager 的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29813804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com