Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView with multiple layouts

I have to show a list with different type of Views. So I have to define a ListView with an Adapter where I have to inflate multiple views. I have gone through example given, but problem is for my list is not symmetric like the example where header is repeated each time after 4 items. So I am facing problem with reuse of items in getView()

 public View getView(int position, View convertView, ViewGroup parent) {   
    int type = getItemViewType(position);
    if (convertView == null) {
        holder = new ViewHolder();
        switch (type) {
             case TYPE_1:
             convertView = mInflater.inflate(R.layout.item1, null);
             .......
             break;

             case TYPE_2:
             convertView = mInflater.inflate(R.layout.item2, null);
             .......
             break;
        }
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder)convertView.getTag();
    }
    ............
    ............
}

Now if the convertView is not null, but the item type for which it was earlier used was different, then the layout will not match with same. How this code will handle this issue?

like image 662
dev_android Avatar asked Apr 06 '15 07:04

dev_android


1 Answers

You can look this example adapter. getViewTypeCount method return your different type row. getItemViewType method, if position equals 0 inflate first row layout else inflate other row layout. You can customize this code example.

    import java.util.List;
    import android.app.Activity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;

    /**
     * Created by MustafaS on 9.2.2015.
     */
    public class CustomArrayAdapter extends ArrayAdapter<YourModel> {

       @Override
       public int getItemViewType(int position) {

          if (position == 0) {
             return 0;
          }
          else {
             return 1;
          }
       }

       @Override
       public int getViewTypeCount() {
          return 2;
       }

       @Override
       public View getView(int position, View convertView, ViewGroup parent) {
          int type = getItemViewType(position);
          if (type == 0) {
             if (convertView == null) {
                convertView = LayoutInflater.from(activity).inflate(R.layout.listview_first_row, parent, false);
                viewHolderFirst = new ViewHolderFirst();
                convertView.setTag(viewHolderFirst);
             }
             else {
                viewHolderFirst = (ViewHolderFirst) convertView.getTag();
             }

          }
          else {
             if (convertView == null) {
                convertView = LayoutInflater.from(activity).inflate(R.layout.listview_other_row, parent, false);
                viewHolder = new ViewHolder();

             }
             else {
                viewHolder = (ViewHolder) convertView.getTag();
             }
          }
          return convertView;
       }

       protected class ViewHolderFirst {
          private RunnableViewPager pager;
          private CircleIndicator   indicator;
       }

       protected class ViewHolder {
          private ImageView imageviewRestaurant;
          private TextView  textviewRestaurantName;
          private TextView  textviewType;
          private TextView  textviewPrice;
          private TextView  textviewDistance;
          private ImageView imageviewCall;
          private ImageView imageviewCalendar;
       }
    }
like image 165
msevgi Avatar answered Nov 05 '22 04:11

msevgi