如何隐藏第一个片段上的上一个按钮和viewpager中最后一个片段的下一个按钮?

时间:2014-01-24 05:43:30

标签: android android-fragments android-viewpager

这是我的MainActivity,它启动了Circlepageindicator。

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可以正常使用上一个和下一个按钮。但我想隐藏第一个片段上的前一个按钮和最后一个片段上的下一个按钮。这个新手。任何建议都被接受。谢谢。

enter image description here

更新

找到解决方案。

CirclePageIndicator titleIndicator = (CirclePageIndicator)findViewById(R.id.titles);
     titleIndicator.setViewPager(pager);

     titleIndicator.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            if(pager.getCurrentItem() == 0)
            {
                prev.setVisibility(View.GONE);
                next.setVisibility(View.VISIBLE);
            }
            if(pager.getCurrentItem() == 1)
            {
                prev.setVisibility(View.VISIBLE);
                next.setVisibility(View.VISIBLE);
            }
            if(pager.getCurrentItem() == 2)
            {
                prev.setVisibility(View.VISIBLE);
                next.setVisibility(View.GONE);
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }
    });

现在,在滑动和按下按钮时,按钮会在各个页面显示/隐藏。

3 个答案:

答案 0 :(得分:3)

在ViewPager的OnPageChangeListener的onPageScrolled方法中 titleIndicator.setOnPageChangeListener(new PagerChangeListener());

class PagerChangeListener implements OnPageChangeListener {
    @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

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

            }
}

答案 1 :(得分:0)

非常简单......我希望它对你有用

找到

// 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/

答案 2 :(得分:0)

  This is my **MainActivity.java**

 final ViewPager pager = (ViewPager) findViewById(R.id.pager);

     **pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener()** 
    {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                 if (position == 0) {
                    ivprvs.setVisibility(View.GONE);
                    ivnxt.setVisibility(View.VISIBLE);
                }
                if (position == 1) {
                    ivprvs.setVisibility(View.VISIBLE);
                    ivnxt.setVisibility(View.VISIBLE);
                }
                if (position == 2) {
                    ivprvs.setVisibility(View.VISIBLE);
                    ivnxt.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });