Android ListView equal to Instagram

1

I'm developing a project in Android Studio, the screens of the project are all made in Fragment, I'm using Material Design followed the tutorial

asked by anonymous 24.05.2015 / 06:31

1 answer

2

You will need a Adapter to manage ListView content.

Example:

Create the 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 the adapter to use in the 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.
            }
        });
    
25.05.2015 / 18:16