Java 如何在特定的ViewPager页面上隐藏视图?请看详情

Java 如何在特定的ViewPager页面上隐藏视图?请看详情,java,android,android-viewpager,android-xml,Java,Android,Android Viewpager,Android Xml,有一个ImageView,实际上是一个右箭头。我正在使用它浏览ViewPager的页面 我想要的是在导航到查看页面的最后一(6)页时,立即隐藏此图像视图(右箭头) 以下是我迄今为止为实现这一目标所做的工作: protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowMan

有一个
ImageView
,实际上是一个右箭头。我正在使用它浏览
ViewPager
的页面

我想要的是在导航到
查看页面的最后一(6)页时,立即隐藏此
图像视图(右箭头)

以下是我迄今为止为实现这一目标所做的工作

protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_walkthroughs);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        ImageView imageView = (ImageView) findViewById(R.id.right_arrow);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
            }
        });

        **here is my code**
        if (mViewPager.getCurrentItem() == 6) {
            imageView.setVisibility(View.GONE);
        }

    } 
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical">

    <android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
                                                     xmlns:android="http://schemas.android.com/apk/res/android"
                                                     xmlns:app="http://schemas.android.com/apk/res-auto"
                                                     xmlns:tools="http://schemas.android.com/tools"
                                                     android:layout_width="match_parent"
                                                     android:layout_height="match_parent"
                                                     android:fitsSystemWindows="true"
                                                     tools:context="com.abc.xyz.Walkthroughs">

        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    </android.support.design.widget.CoordinatorLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/colorPrimary">

            <View
                android:layout_width="1dp"
                android:layout_height="@dimen/viewpagerindicator_height"
                android:background="@android:color/white"
                android:layout_alignParentTop="true"
                android:layout_alignLeft="@+id/right_arrow"
                android:layout_alignStart="@+id/right_arrow"/>

            <ImageView
                android:id="@+id/right_arrow"
                android:layout_width="wrap_content"
                android:layout_height="@dimen/viewpagerindicator_height"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:background="@drawable/btn_touch_feedback_impression"
                android:src="@drawable/ic_action_right_arrow"/>

        </RelativeLayout>

    </RelativeLayout>
这里是
活动\u walkthroughs.xml
文件代码

protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_walkthroughs);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        ImageView imageView = (ImageView) findViewById(R.id.right_arrow);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
            }
        });

        **here is my code**
        if (mViewPager.getCurrentItem() == 6) {
            imageView.setVisibility(View.GONE);
        }

    } 
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical">

    <android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
                                                     xmlns:android="http://schemas.android.com/apk/res/android"
                                                     xmlns:app="http://schemas.android.com/apk/res-auto"
                                                     xmlns:tools="http://schemas.android.com/tools"
                                                     android:layout_width="match_parent"
                                                     android:layout_height="match_parent"
                                                     android:fitsSystemWindows="true"
                                                     tools:context="com.abc.xyz.Walkthroughs">

        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    </android.support.design.widget.CoordinatorLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@color/colorPrimary">

            <View
                android:layout_width="1dp"
                android:layout_height="@dimen/viewpagerindicator_height"
                android:background="@android:color/white"
                android:layout_alignParentTop="true"
                android:layout_alignLeft="@+id/right_arrow"
                android:layout_alignStart="@+id/right_arrow"/>

            <ImageView
                android:id="@+id/right_arrow"
                android:layout_width="wrap_content"
                android:layout_height="@dimen/viewpagerindicator_height"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:background="@drawable/btn_touch_feedback_impression"
                android:src="@drawable/ic_action_right_arrow"/>

        </RelativeLayout>

    </RelativeLayout>

问题是这段代码不起作用

请让我知道问题出在哪里


提前感谢。

使用以下代码修改您的代码:-

mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
    public void onPageScrollStateChanged(int state) {}
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    public void onPageSelected(int position) {
        // Check if this is the page you want.
    }
});
并在
onPageSelected()
方法中检查您的情况。

谢谢

你的回答给了我找到正确方法的线索

以下是我在
mviewpage.addOnPageChangeListener()中所做的操作

不过这很容易


和平。

尝试此演示隐藏或显示您的页面找到下面的答案。只需将您的可见代码移到OnPageChangeListener中即可解决问题。如果您的案例仅在创建时执行
,Nigams答案正确,请将代码移到
OnPageChangeListener
@NigamPatro中,无需对您的答案发表评论,OP会被自动通知。此代码导致2个问题:-1-它隐藏了
ViewPager
的指示器。2-从上一页向后滚动到上一页时,
ImageView
不会再次出现。您需要在
onPageSelected()
中设置此隐藏、取消隐藏的条件。