Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

221
Views
Android setVisibility not working in PagerAdapter

I'm trying to hide and show part of my view in my PagerAdapter item but it doesn't do anything, not GONE, VISIBLE, or 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;
    }
}

Here's the item 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>

And here is how the adapter is set up from the host activity:

adapter = new FeedPagerAdapter(SlideActivity.this, List<> of items);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(position);
adapter.notifyDataSetChanged();

Any help would be awesome

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

It seems that using Butterknife or standard findViewById to setup ProgressBar item doesn't work to update the view, not sure why, maybe someone can explain. I solved by seeking out the actual item every time it's visibility needs to be changed in PagerAdapter like so:

@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;
    }
over 4 years ago · Santiago Trujillo Report

0

Look at that item.xml again, it looks for me ur image is match parent, and ur progress bar is wrap content. change image to wrap_contet and make sure progress bar appears on display!

over 4 years ago · Santiago Trujillo Report

0

Add a notifyDataSetChanged() after the progressBar.setVisibility(View.GONE); maybe the line adapter.notifyDataSetChanged(); is happening before you set the progressBar visibility.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!