如何检查 viewpager 中的页面是否在 java 中可见
How to check if a page in viewpager is visible in java
我已经在互联网上搜索了一个小时,并为 viewPager 创建了自定义视图。我看到的每个解决方案都是针对 Fragments 的。我的问题是如何使用 if 语句检查我的页面是否可见。我想这样做是出于一个原因。我有一些位于相机屏幕顶部的视图,我想根据视图保存每张照片。例如:如果正在使用"TOPS"衣服对象和"tops_view",我想将它保存到android中的"tops"文件夹中。我已经知道如何保存到不同的文件夹,所以我想在我的 cameraAPI 文件中进行 if 语句。 if语句就是我要实现的伪代码:
if (ClothesPagerView == TOPS)
final File file = new File(Environment.getExternalStorageDirectory() + "/OutfitMatcher/tops/img" + System.currentTimeMillis () +".jpg");
else if (ClothesPagerView == BOTTOMS)
final File file = new File(Environment.getExternalStorageDirectory() + "/OutfitMatcher/bottoms/img" + System.currentTimeMillis () +".jpg");
CustomPagerAdapater.java
public class CustomPagerAdapter1 extends PagerAdapter{
private Context mContext;
public CustomPagerAdapter1(Context context) {
mContext = context;
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
ClothesObject clothesObject = ClothesObject.values()[position];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(clothesObject.getLayoutResId(), collection, false);
collection.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
@Override
public int getCount() {
return ClothesObject.values().length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public CharSequence getPageTitle(int position) {
ClothesObject customPagerEnum = ClothesObject.values()[position];
return mContext.getString(customPagerEnum.getTitleResId());
}
}
ClothesObject.jaca(我与适配器一起使用)
public enum ClothesObject {
TOPS(R.string.tops_camera, R.layout.tops_view),
BOTTOMS(R.string.bottoms_camera, R.layout.bottoms_view),
ACCESSORIES(R.string.accessories_camera, R.layout.accessories_view),
SHOES(R.string.shoes_camera, R.layout.shoes_view),
OUTERWEAR(R.string.outerwear_camera, R.layout.outerwear_view),
DRESSES(R.string.dresses_camera, R.layout.dresses_view);
private int mTitleResId;
private int mLayoutResId;
ClothesObject(int titleResId, int layoutResId) {
mTitleResId = titleResId;
mLayoutResId = layoutResId;
}
public int getTitleResId() {
return mTitleResId;
}
public int getLayoutResId() {
return mLayoutResId;
}
}
为此,您必须将 fragment 与 Viewpager 一起使用。那么您可以在片段中使用 getUserVisibleHint()
来检查页面是否处于活动状态
即
if(getUserVisibleHint()){
//Your fragment is visible
}else{
// Not visible
}
我已经在互联网上搜索了一个小时,并为 viewPager 创建了自定义视图。我看到的每个解决方案都是针对 Fragments 的。我的问题是如何使用 if 语句检查我的页面是否可见。我想这样做是出于一个原因。我有一些位于相机屏幕顶部的视图,我想根据视图保存每张照片。例如:如果正在使用"TOPS"衣服对象和"tops_view",我想将它保存到android中的"tops"文件夹中。我已经知道如何保存到不同的文件夹,所以我想在我的 cameraAPI 文件中进行 if 语句。 if语句就是我要实现的伪代码:
if (ClothesPagerView == TOPS)
final File file = new File(Environment.getExternalStorageDirectory() + "/OutfitMatcher/tops/img" + System.currentTimeMillis () +".jpg");
else if (ClothesPagerView == BOTTOMS)
final File file = new File(Environment.getExternalStorageDirectory() + "/OutfitMatcher/bottoms/img" + System.currentTimeMillis () +".jpg");
CustomPagerAdapater.java
public class CustomPagerAdapter1 extends PagerAdapter{
private Context mContext;
public CustomPagerAdapter1(Context context) {
mContext = context;
}
@Override
public Object instantiateItem(ViewGroup collection, int position) {
ClothesObject clothesObject = ClothesObject.values()[position];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(clothesObject.getLayoutResId(), collection, false);
collection.addView(layout);
return layout;
}
@Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
@Override
public int getCount() {
return ClothesObject.values().length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public CharSequence getPageTitle(int position) {
ClothesObject customPagerEnum = ClothesObject.values()[position];
return mContext.getString(customPagerEnum.getTitleResId());
}
}
ClothesObject.jaca(我与适配器一起使用)
public enum ClothesObject {
TOPS(R.string.tops_camera, R.layout.tops_view),
BOTTOMS(R.string.bottoms_camera, R.layout.bottoms_view),
ACCESSORIES(R.string.accessories_camera, R.layout.accessories_view),
SHOES(R.string.shoes_camera, R.layout.shoes_view),
OUTERWEAR(R.string.outerwear_camera, R.layout.outerwear_view),
DRESSES(R.string.dresses_camera, R.layout.dresses_view);
private int mTitleResId;
private int mLayoutResId;
ClothesObject(int titleResId, int layoutResId) {
mTitleResId = titleResId;
mLayoutResId = layoutResId;
}
public int getTitleResId() {
return mTitleResId;
}
public int getLayoutResId() {
return mLayoutResId;
}
}
为此,您必须将 fragment 与 Viewpager 一起使用。那么您可以在片段中使用 getUserVisibleHint()
来检查页面是否处于活动状态
即
if(getUserVisibleHint()){
//Your fragment is visible
}else{
// Not visible
}