以下是我用来启动Circlepageindicator的MainActivity,
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
ViewPager pager;
Button prev,next;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter adapter=new MyPagerAdapter();
pager = (ViewPager)findViewById(R.id.view_pager);
pager.setAdapter(adapter);
pager.setCurrentItem(0);
//Bind the title indicator to the adapter
CirclePageIndicator titleIndicator = (CirclePageIndicator)findViewById(R.id.titles);
titleIndicator.setViewPager(pager);
prev = (Button) findViewById(R.id.prev);
prev.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
pager.setCurrentItem(pager.getCurrentItem() - 1);
}
});
next = (Button) findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
pager.setCurrentItem(pager.getCurrentItem() + 1);
}
});
}
@Override
public void onBackPressed() {
if (pager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
pager.setCurrentItem(pager.getCurrentItem() - 1);
}
}
}
在Circlepageindicator中,前进(previous)和后退(next)键都可以正常使用,但我想在第一个fragment中隐藏后退键,在最后一个fragment中隐藏前进键,具体应该如何操作呢?
原文:How to hide previous button on first fragment and next button on last fragment in viewpager?
答:Aditya Kamath
你可以在ViewPager pager.setOnPageChangeListener中,启用OnPageChangeListener的onPageScrolled方法,实现这种效果:
答:Ganesh Katikar
这段代码和简单,我希望能对你有所帮助,只要编写:
和
即可实现上述效果。
更多详情可以登录:
http://dwij.co.in/greendroid-library-implementation-in-android-project/