gpt4 book ai didi

java - 在 fragment 之间滑动时如何显示进度?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:20:39 28 4
gpt4 key购买 nike

我有一个带有寻呼机适配器的 Activity ,其中包含一些 fragment 。我还在 Activity 顶部创建了一个进度条(水平)。我需要在浏览 fragment 时栏会显示进度。例如,当我在第一个 fragment 中时它应该是灰色的,然后当我滑动 fragment 时它会逐渐变大,而当我在最后一个 fragment 中时它应该是全绿色。

ma​​inActivity.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;
}
}

最佳答案

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

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

});

关于java - 在 fragment 之间滑动时如何显示进度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29776249/

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