Android setVisibility无法在PagerAdapter中运行

【Android setVisibility无法在PagerAdapter中运行】要须心地收汗马,孔孟行世目杲杲。这篇文章主要讲述Android setVisibility无法在PagerAdapter中运行相关的知识,希望能为你提供帮助。
我试图隐藏并在我的PagerAdapter项目中显示我的部分视图,但它没有做任何事情,没有GONE,VISIBLE或INVISIBLE。

public class FeedPagerAdapter extends PagerAdapter { private final String KEY_GLOBAL_PREFS = "global"; @BindView(R.id.item) ImageView image; @BindView(R.id.progress_bar) ProgressBar progressBar; private final SlideActivity host; private List< Thing> items; private SharedPreferences sharedPreferences; private int postition; public FeedPagerAdapter(SlideActivity host, List< Thing> items){ this.host = host; this.items = items; sharedPreferences = host.getSharedPreferences(KEY_GLOBAL_PREFS, Context.MODE_PRIVATE); }@Override public Object instantiateItem(final ViewGroup parent, int position) { final View view = LayoutInflater.from(host).inflate(R.layout.item_slide_reddit_post, parent, false); ButterKnife.bind(this, view); progressBar.setVisibility(View.GONE); return view; }@Override public int getCount() { return items.size(); }@Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View)object); }@Override public boolean isViewFromObject(View view, Object object) { return view == object; } }

这是项目xml:
< ?xml version="1.0" encoding="utf-8"?> < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> < ImageView android:id="@+id/item" android:layout_width="match_parent" android:layout_height="match_parent" android:src:"..."/> < ProgressBar android:id="@+id/progress_bar" android:layout_width="wrap_content" android:layout_height="wrap_content" /> < /RelativeLayout>

以下是从主机活动设置适配器的方式:
adapter = new FeedPagerAdapter(SlideActivity.this, List< > of items); viewPager.setAdapter(adapter); viewPager.setCurrentItem(position); adapter.notifyDataSetChanged();

任何帮助都是极好的
答案再看看那个item.xml,它找我,你的图像是匹配父,你的进度条是换行内容。将图像更改为wrap_contet并确保显示进度条!
另一答案在设置progressBar可见性之前,在notifyDataSetChanged()之后添加progressBar.setVisibility(View.GONE); 可能会发生adapter.notifyDataSetChanged(); 行。
另一答案似乎使用Butterknife或标准findViewById来设置ProgressBar项目不能更新视图,不知道为什么,也许有人可以解释。每当需要在PagerAdapter中更改其可见性时,我通过寻找实际项目来解决,如下所示:
@Override public Object instantiateItem(final ViewGroup parent, int position) { final View view = LayoutInflater.from(host).inflate(R.layout.item_slide_reddit_post, parent, false); ButterKnife.bind(this, view); view.findViewById(R.id.progress_bar).setVisibility(View.GONE); return view; }

另一答案我记得要改变ProgressBar状态,你应该总是使用runOnUiThread()
getActivity().runOnUiThread(new Runnable() { public void run() { // implement View changes. } });


    推荐阅读