Skip to content Skip to sidebar Skip to footer

Making Dynamic Layout For Each Row In Listview In Android

I have a xml file which contains basic layout for each row of ListView(which is a realtive layout and has TextView inside it). I want to change the attributes of this layout for ea

Solution 1:

You can implement a ViewHolder pattern for your adapter, and for each position, inflate your own layout.

To do that, override getView like this:

public View getView(int position, View convertView, ViewGroup parent) {            

   // A ViewHolder keeps references to children views to avoid unneccessary calls            // to findViewById() on each row.            
  ViewHolder holder;            
  // When convertView is not null, we can reuse it directly, there is no need            // to reinflate it. We only inflate a new View when the convertView supplied            // by ListView is null.            if (convertView == null) {                

    convertView = mInflater.inflate(R.layout.sample, null);   
    // Creates a ViewHolder and store references to the two children views                // we want to bind data to.               
   holder = new ViewHolder();                
   holder.name = (TextView) convertView.findViewById(R.id.text);               
   holder.icon = (ImageView) convertView.findViewById(R.id.icon);                
   convertView.setTag(holder);            
 } else {                
   // Get the ViewHolder back to get fast access to the TextView                // and the ImageView.
   holder = (ViewHolder) convertView.getTag();
 }             


  // Bind the data efficiently with the holder. 
  holder.name.setText(myElements.get(id)); 
  holder.icon.setImageBitmap( mIcon1 );

return convertView;
}  

Read more about this on the Android developer site here

EDIT:

To change the width and height of each row, you can use something like the following, for each row on bind data section :

RelativeLayoutrl= (RelativeLayout) findViewById(R.id.yourId); 
rl.getLayoutParams().height = 100;
rl.getLayoutParams().width = 100;`

Solution 2:

Making dynamic layout for each row in ListView in android

Maybe @Arkde's answer could work but i i think it's little dirty solution.

What about to create one generic layout and update / change appearance due to provided conditions? (for example due to value in current row).

different layout width and height of each row

This can by easily achieved by an usage of "margins". By "margins" i think a creation of empty Views which will work as "margins" and will determine height of row for instance. And then due to mentioned condition(s) showing or hiding them.

What are advantages of generic layout?

  • Easier, efficient and more human-readable solution
  • You don't need to implement ViewType and ViewCount for Adapter
  • One layout for each row with changeable appearance
  • You don't need to change UI appearance from application logic in "hardcoded" way

When visibility of View is assigned to View.GONE it won't take place in layout whereas View.VISIBLE takes place.

Example of "margin":

<LinearLayout>
   ...
   <Viewandroid:id="@+id/upperMargin"android:layout_width="match_parent"android:layout_height="40dp"android:background="@android:color/transparent"
   />

   ...

   <Viewandroid:id="@+id/lowerMargin"android:layout_width="match_parent"android:layout_height="40dp"android:background="@android:color/transparent"
   />
   ...
</LinearLayout>

Note: Suggested an usage of ViewHolder is very neat and effective approach if you want to increase perfomance of ListView.

I hope that my solution will help to solve your problem you're facing now.

Post a Comment for "Making Dynamic Layout For Each Row In Listview In Android"