如何在首尾页分别隐藏前进和后退键?

以下是我用来启动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中隐藏前进键,具体应该如何操作呢?
a

原文:How to hide previous button on first fragment and next button on last fragment in viewpager?

阅读 4.2k
1 个回答

答:Aditya Kamath
你可以在ViewPager pager.setOnPageChangeListener中,启用OnPageChangeListener的onPageScrolled方法,实现这种效果:

class PagerChangeListener implements OnPageChangeListener {
    @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                IciciLogger.info(classname, "onPageScrolled");
                pageSelected = arg0;

                switch (pageSelected) {
                case 0: {

                    preButton.setVisibility(View.INVISIBLE);
                }

                    break;

                case 2: {

                    nexButton.setVisibility(View.INVISIBLE);
                }

                    break;

                default: {
                    nexButton.setVisibility(View.VISIBLE);
                    preButton.setVisibility(View.VISIBLE);
                }
                    break;
                }

            }
}

答:Ganesh Katikar
这段代码和简单,我希望能对你有所帮助,只要编写:

// For previous button
 if(pager.getCurrentItem() == 0)
{
previousBtn.setVisibility(View.GONE);
}

// For Next Button
  if(pager.getCurrentItem() == YOUR LAST ITEM INDEX)
      {
      nextBtn.setVisibility(View.GONE);
      }

即可实现上述效果。
更多详情可以登录:
http://dwij.co.in/greendroid-library-implementation-in-android-project/

推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏