2015-04-21 60 views
2

我有一個包含一些片段的尋呼機適配器的活動。我還在活動的頂部創建了一個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; 
    } 
} 

回答

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++); 
     } 

    }); 
1

您需要在您的活動添加OnPageChangeListenerViewPager這樣

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 
    } 

}); 
+0

我應該在onPageSelected後寫什麼? – Kaspis245

+0

你應該改變進度條的顏色。創建一個int顏色或顏色res ID的數組,並獲取該位置的顏色並將其設置爲pe進度欄。你可以做到嗎? –

+0

不是。但我會嘗試。 – Kaspis245

相關問題