簡體   English   中英

如何在特定的ViewPager頁面上隱藏視圖? 請查看詳細信息

[英]How to hide a view at a specific ViewPager page? Please see details

有一個ImageView實際上是一個右箭頭。 我正在用它瀏覽ViewPager的頁面。

我想要的是,一旦導航到ViewPager的最后一頁(第6頁),就想隱藏此ImageView (向右箭頭)

到目前為止,這是我已經完成的工作

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>

問題在於此代碼無法完成任務。

請讓我知道問題出在哪里!

提前致謝。

謝謝NigamPatro

您的回答給了我找出正確方法的線索。

這是我在mViewPager.addOnPageChangeListener()所做的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) {

            }
        });

不過,這很容易。

和平。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM