如何在特定的 ViewPager 页面隐藏视图?请查看详情
How to hide a view at a specific ViewPager page? Please see details
有一个 ImageView
实际上是一个右箭头。我用它来浏览 ViewPager
的页面。
我想要的是在导航到 ViewPager
[=34 的最后(第 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>
问题是这段代码没有完成任务。
请告诉我问题出在哪里!
提前致谢。
使用以下代码修改您的代码:-
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()
方法中检查您的条件。
谢谢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) {
}
});
虽然这很简单。
和平。
有一个 ImageView
实际上是一个右箭头。我用它来浏览 ViewPager
的页面。
我想要的是在导航到 ViewPager
[=34 的最后(第 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>
问题是这段代码没有完成任务。
请告诉我问题出在哪里!
提前致谢。
使用以下代码修改您的代码:-
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()
方法中检查您的条件。
谢谢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) {
}
});
虽然这很简单。
和平。