获取ViewPager的当前位置

时间:2015-04-23 04:38:24

标签: android android-viewpager

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

在此我想知道,我如何显示总位置的当前位置,就像我们通常在图片库中看到的那样

例如:我想在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);
    }
}

3 个答案:

答案 0 :(得分:4)

你应该在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


        }
    });

答案 1 :(得分:1)

试试这个:

viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                System.out.println("onPageSelected");

            }

            @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
                System.out.println("onPageScrollStateChanged");
                System.out.println("Current position=="+num);


            }
        });

答案 2 :(得分:1)

使用此

String s= (pager.getCurrentItem()+1) +" of "+ images.size()
textView.setText(s);