如何隐藏特定ViewPager页面的视图?请看细节

时间:2015-12-11 13:00:28

标签: java android android-viewpager android-xml

ImageView实际上是右箭头。我正在使用它来浏览ViewPager页面。

我希望在导航到ImageView 的最后一页(第6页)时隐藏此ViewPager(右箭头)。

以下是我迄今为止所做的工作

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

    } 

此处为activity_walkthroughs.xml文件代码

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

问题是这段代码没有完成这项工作。

请告诉我问题在哪里!

提前致谢。

2 个答案:

答案 0 :(得分:1)

感谢NigamPatro

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

以下是我在mViewPager.addOnPageChangeListener()中所做的事情:

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        imageView.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        imageView.setVisibility(View.VISIBLE);
                        break;
                    case 2:
                        imageView.setVisibility(View.VISIBLE);
                        break;
                    case 3:
                        imageView.setVisibility(View.VISIBLE);
                        break;
                    case 4:
                        imageView.setVisibility(View.VISIBLE);
                        break;
                    case 5:
                        imageView.setVisibility(View.INVISIBLE);
                        break;
                    default:
                        imageView.setVisibility(View.VISIBLE);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

这很容易。

和平。

答案 1 :(得分:0)

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

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()方法中检查您的条件。