我正在尝试隐藏并显示一个View,当我正在点击imageView时,我在我的ViewPager中有3个片段,所以问题是当我在我的第一个View上时我点击了imageView它工作正常(我的视图被隐藏)但是什么时候片段没有。 2或3,然后点击imageView,它永远不会隐藏View只能处理我的第一个片段,这是我的代码:
public class MainActivity extends AppCompatActivity {
private static int[] imageArray;
static ImageLoader imageLoader;
static ArrayList<String> urlArray;
static DisplayImageOptions options;
static ProgressBar spinner;
static PhotoViewAttacher mAttacher;
static Boolean isToolBarShown = true;
static Button nextButton , previousButton;
static ViewPager viewPager ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
urlArray = new ArrayList<>();
urlArray.add("http://i.imgur.com/uLRgvM8.png");
urlArray.add("http://i.imgur.com/LUuJ4FO.png");
urlArray.add("http://i.imgur.com/7SSBNBA.jpg");
// Create global configuration and initialize ImageLoader with this config
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.imageScaleType(ImageScaleType.EXACTLY)
.displayer(new FadeInBitmapDisplayer(300)).build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext())
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new WeakMemoryCache()).build();
ImageLoader.getInstance().init(config);
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder().cacheInMemory(true)
.cacheOnDisk(true)
.resetViewBeforeLoading(true).build();
//initialize image view
ImageView imageView = (ImageView) findViewById(R.id.imageView);
ImageFragmentPagerAdapter imageFragmentPagerAdapter = new ImageFragmentPagerAdapter(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setOffscreenPageLimit(1);
viewPager.setAdapter(imageFragmentPagerAdapter);
}
public static class ImageFragmentPagerAdapter extends FragmentPagerAdapter {
public ImageFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return urlArray.size();
}
@Override
public Fragment getItem(int position) {
//SwipeFragment fragment = new SwipeFragment();
return SwipeFragment.newInstance(position);
}
}
public static class SwipeFragment extends Fragment {
private int position;
private ImageView imageView;
private RelativeLayout relativeLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View swipeView = inflater.inflate(R.layout.swipe_fragment, container, false);
imageView = (ImageView) swipeView.findViewById(R.id.imageView);
spinner = (ProgressBar) swipeView.findViewById(R.id.spinner);
relativeLayout = (RelativeLayout) swipeView.findViewById(R.id.tool_bar);
Bundle bundle = getArguments();
nextButton = (Button)swipeView.findViewById(R.id.next_button);
previousButton = (Button)swipeView.findViewById(R.id.button_previous);
position = bundle.getInt("position");
Toast.makeText(getContext(),"NEW PAGE CALLED",Toast.LENGTH_SHORT).show();
if (isToolBarShown){
relativeLayout.setVisibility(View.VISIBLE);
}else {
relativeLayout.setVisibility(View.GONE);
}
mAttacher = new PhotoViewAttacher(imageView);
// viewPager.OnPageChangeListener(new ViewPager.OnPageChangeListener() {
// @Override
// public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//
// }
//
// @Override
// public void onPageSelected(int position) {
//
// loadImageView(position,imageView);
//
// }
//
// @Override
// public void onPageScrollStateChanged(int state) {
//
// }
// });
previousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(viewPager.getCurrentItem()-1,true);
}
});
nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
viewPager.setCurrentItem(viewPager.getCurrentItem()+1,true);
}
});
return swipeView;
}
public void loadImageView(int position, final ImageView imageView ){
imageLoader.displayImage(urlArray.get(position), imageView, options, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
mAttacher = new PhotoViewAttacher(imageView);
mAttacher.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
@Override
public void onViewTap(View view, float x, float y) {
if (view == imageView){
if (relativeLayout.getVisibility() == View.VISIBLE){
isToolBarShown = false;
relativeLayout.setVisibility(View.GONE);
}else {
isToolBarShown = true;
relativeLayout.setVisibility(View.VISIBLE);
}
Log.d("OOPS","WHAT THE.., WHAT IS WRONG HERE");
}
}
});
if(mAttacher!=null){
mAttacher.update();
}else{
mAttacher = new PhotoViewAttacher(imageView);
}
spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
spinner.setVisibility(View.GONE);
}
}, new ImageLoadingProgressListener() {
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
spinner.setVisibility(View.VISIBLE);
spinner.setProgress((current*100)/total);
}
});
}
static SwipeFragment newInstance(int position) {
SwipeFragment swipeFragment = new SwipeFragment();
Bundle bundle = new Bundle();
bundle.putInt("position", position);
swipeFragment.setArguments(bundle);
return swipeFragment;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// load data here
// if (getView() != null) {
// isViewShown = true;
// // fetchdata() contains logic to show data when page is selected mostly asynctask to fill the data
// loadImageView(position , imageView);
// } else {
// isViewShown = false;
// }
}
}
}}
我试图隐藏并显示RelativeView
,如果有人知道我的代码中有什么错误或缺失那么请纠正我
答案 0 :(得分:1)
我认为你必须尝试使用fragmentstatepageradapter而不是FragmentPagerAdapter。它可能会帮助你。你可以在这里看到它们之间的区别: Difference between FragmentPagerAdapter and FragmentStatePagerAdapter
答案 1 :(得分:1)
您没有提供所有代码,但正如您在评论中所说,mAttacher是来自SwipeFragment外部的静态变量。我想那个
也是来自SwipeFragment外部的静态成员。如果它们来自您的R.layout.swipe_fragment
布局,那么您应该为每个SwipeFragment使用每个relativeLayout,spinner和imageLoader实例。
现在你正在刷另一个片段而你看到了它。但我上面提到的类的实例来自另一个片段。这就是为什么它不适合你的原因。试着这样做。并且您可以提供更多代码。
祝你好运!答案 2 :(得分:0)
设置viewpager offscreenpagelimit可能会帮助你:)
mViewPager.setoffscreenpagelimit(2)
//2 is important over here.
答案 3 :(得分:0)
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
//使用上面的函数设置片段