如何在片段之间滑动时显示进度?

时间:2015-04-21 15:12:08

标签: java android android-fragments android-progressbar

我有一个包含一些片段的寻呼机适配器的活动。我还在活动的顶部创建了一个progressBar(水平)。当我翻阅片段时,我需要条形图显示进度。例如,当我在第一个片段时它应该是灰色的,然后当我在片段中滑动时逐渐生长,当我在最后一个片段时完全变绿。

mainActivity.java

public class testActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pager_adapter);
        initialisePaging();
    }

    private  void initialisePaging() {
        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this,fragment1.class.getName()));
        fragments.add(Fragment.instantiate(this,fragment2.class.getName()));
        fragments.add(Fragment.instantiate(this,fragment3.class.getName()));
        PagerAdapter mPagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), fragments);

        ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
        pager.setAdapter(mPagerAdapter);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_test, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

PagerAdapter.java

public class PagerAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int arg0) {
        return this.fragments.get(arg0);
    }

    @Override
    public int getCount() {
        return this.fragments.size();
    }

    public void setFragments(List<Fragment> fragments) {
        this.fragments = fragments;
    }
}

2 个答案:

答案 0 :(得分:1)

您需要在此活动中为OnPageChangeListener添加ViewPager

pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

    @Override
    public void onPageScrollStateChanged(int arg0) {

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {

    }

    @Override
    public void onPageSelected(int pos) {
       // Update the progress bar according to pos
       // Also note that you reach the end when pos == mPagerAdapter.getCount() - 1
       // and that pos starts at 0 - this is the first fragment
    }

});

答案 1 :(得分:1)

其实我找到了一个非常简单的答案。这就是我所做的:

  pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int pos) {
          //This is because progress is 0 at the start of the program
          progress++;
          ProgressBar progress = (ProgressBar) findViewById(R.id.progressBar);
                progress.setProgress(position++);
        }

    });