Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda