Android Listview equal to Instagram

Asked

Viewed 468 times

1

I’m developing a project in Android Studio, the project screens are all made in Fragment, I’m using the Material Design followed the tutorial here, and would like to make the main screen a Listview similar to Instagram. In my case it would be, a photo, and information, and when you click on the photo, open an Activity or Fragment (I don’t know which one would be right). I ask for your help, because the tutorials I find on the Internet, are very bad to understand, I would prefer to step-by-step. Below is an image that shows more or less what I want.inserir a descrição da imagem aqui

1 answer

2

You will need a Adapter to manage Listview content.

Example:

Create Adapter. I suggest you read the documentation to better understand the responsibility of each method.

public class SeuAdapter extends BaseAdapter {
    List<String> lista = new ArrayList<>();
    Context context;

    public SeuAdapter(Context context, List<String> lista){
        this.lista = lista;
        this.context = context;
    }

    @Override
    public int getCount() {
        return lista.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view = layoutInflater.inflate(R.layout.seu_layout, null);
        //TODO preencher a view com as informações necessárias
        return view;
    }
}

Instantiate Adapter to use in listview:

SeuAdapter seuAdapter = new SeuAdapter(getContext(), lista);
ListView listView = (ListView) findViewById(R.id.listview);
listview.set(adapter)

Listener to take action when selecting an item from the list, using the Onitemclicklistener:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //TODO pode utilizar a variavel position para pegar o objeto selecioando da lista.
            }
        });

Browser other questions tagged

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