Add empty item at the end of Listview

Asked

Viewed 254 times

1

I have a FloatingActionButton in the lower right corner of my screen and when it comes to the end of ListView, it sits on top of a button. I would like to add an empty item at the end of the ListView so that when I got to the bottom of the list that didn’t happen.

I want to do the same in Gmail:

inserir a descrição da imagem aqui

The white space appears only when it arrives at the last item. I am using adapter for my Listview:

public class AdaptadorProdutosSemelhantes extends BaseAdapter {
    String[] nomeprod,estabelecimentoprod,precoprod;
    int[] imgprod;
    Context context;
    LayoutInflater inflater = null;

    public AdaptadorProdutosSemelhantes(int[] imgprod, String[] nomeprod, String[] estabelecimentoprod, String[] precoprod, Context context) {
        this.nomeprod = nomeprod;
        this.estabelecimentoprod = estabelecimentoprod;
        this.precoprod = precoprod;
        this.context = context;
        this.imgprod = imgprod;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return nomeprod.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public class Holder{
        ImageView imgproduto;
        TextView nomeproduto,precoproduto,estabelecimentoproduto;
        Button vertodas;
        RatingBar ratingBar;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder h = new Holder();
        View v = inflater.inflate(R.layout.lista_produtos_semelhantes,null);
        h.imgproduto = (ImageView) v.findViewById(R.id.imgprodsemelhante);
        h.nomeproduto = (TextView) v.findViewById(R.id.nomeprodsemelhante);
        h.precoproduto= (TextView) v.findViewById(R.id.precoprodsemelhante);
        h.estabelecimentoproduto = (TextView) v.findViewById(R.id.infosupprodsemelhante);
        h.vertodas = (Button) v.findViewById(R.id.vertodasofertas);
        h.ratingBar = (RatingBar) v.findViewById(R.id.ratingBarprodsemelhante);
        h.imgproduto.setImageResource(imgprod[position]);
        h.nomeproduto.setText(nomeprod[position]);
        h.precoproduto.setText(precoprod[position]);
        h.estabelecimentoproduto.setText(estabelecimentoprod[position]);

        h.vertodas.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return v;
    }
  • 1

    What if you gave a padding bottom on your list? Wouldn’t solve??

  • You can add an empty item at the time you fill in your String[] in the Activity you call your Adapter. To add just check the size of your vector and add +1. You have to observe if your item has variable size. If it is variable size, you would have to set a fixed size so that the float button is not on top.

1 answer

2


To solve your problem, there are several possibilities. Sometimes the action button is not vital enough so it needs to be seen all the time. In some cases, hiding it while scrolling down can make enough sense.

Updated Google guidelines show a version - Floating action button - to animate the button in and out.

inserir a descrição da imagem aqui

Already, also for your case, you can use Listview#addFooterView() to add a view view at the bottom of Listview.

You can try so creating a classe Viewtypes:

private class Viewtypes{
        public static final int Header = 1;
        public static final int Normal = 2;
        public static final int Footer = 3;
}

Then in his Adapter:

@Override
public int getItemViewType(int position) {

    if(items.get(position).isHeader)
        return Viewtypes.Header;
    else if(items.get(position).isFooter)
        return Viewtypes.Footer;
    else
        return Viewtypes.Normal;

}

Getview:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
    View rowView;

    switch (position)
    {
        case Viewtypes.Normal:
            rowView=LayoutInflater.from(parent.getContext()).inflate(R.layout.normal, parent, false);
            break;
        case Viewtypes.Header:
            rowView=LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);
            break;
        case Viewtypes.Footer:
            rowView=LayoutInflater.from(parent.getContext()).inflate(R.layout.footer, parent, false);
            break;
        default:
            rowView=LayoutInflater.from(parent.getContext()).inflate(R.layout.normal, parent, false);
            break;
    }
    return new ViewHolder (rowView);
}

Good luck!

  • It worked! I used the idea of the footer :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.