Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change the row layout of a ListView

I'm working on a chat module in an app, where I want the messages from two participants on opposing alignment (other user left-aligned and my own msg right-aligned). Right now, my row layout is passed in through a static layout xml (with msg and avatar left-aligned). Is there a way to modify the view dynamically, or is there a way to pass an alternative row layout for the UI system to pick at runtime?

like image 295
jingyin Avatar asked Aug 15 '11 17:08

jingyin


People also ask

How can you update a ListView dynamically?

This example demonstrates how do I dynamically update a ListView in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Is ListView a layout?

Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list. The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result.

Does ListView recycle?

ListView is a view which groups several items and displays them in a vertical list. This list is also automatically made scrollable if the amount of data supplied cannot be accommodated on the screen. ArrayAdapter and ListView are required for view recycling.

What is ListView adapter in android?

BaseAdapter with Android ListView. ListView is a ViewGroup that displays a list of vertically scrollable items. The list items are automatically inserted into the list using an adapter that is connected to a source, such as an array or a database query, and each item is converted into a row in the ListView.


1 Answers

You can do that inside the getView() method of your ArrayAdapter class (assuming you are defining your own ArrayAdapter).

You could have something like this:

private class YourAdapter extends ArrayAdapter<Message> {
        private final LayoutInflater mLayoutInflater;

    YourAdapter(YourListActivity activity) {
        super(mContext, 0);
        mLayoutInflater = LayoutInflater.from(activity);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            // Inflate your view
            convertView = mLayoutInflater.inflate(R.layout.list_view_item, parent, false);
            mViewHolder = new ViewHolder();
            mViewHolder.avatar = (ImageView) convertView.findViewById(R.id.placeholder);
            mViewHolder.message = (TextView) convertView.findViewById(R.id.message);

            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }

        final Message message = getItem(position);

        mViewHolder.message.setText(message.getMessage());
        // etc. Manipulate your views as you wish


        return convertView;
    }
}


   private static class ViewHolder {
        TextView message;
        ImageView avatar;
   }

getView will get called each time you the ListView is modified (like when you scroll or when new elements are added to it), so you can manipulate each row as you want there. Don't forget to set the array adapter of the ListView to an instance of this class.

listView.setListAdapter(new mYourAdapter);  
like image 118
Amokrane Chentir Avatar answered Sep 29 '22 05:09

Amokrane Chentir