Android ListView with multiple layouts

送分小仙女□ 提交于 2019-12-04 03:56:06

问题


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?


回答1:


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;
       }
    }



回答2:


You can do that by overriding the Methods getViewCount() and getItemViewType(int position)

In the first you just return the amount of different Views you have.

In the second you return a unique Identifier for each different View.

In your getView() Method you check then for the View Type and inflate the right Layout for it.

More info about that topic: Android ListView with different layouts for each row

Android ListView with different layouts for each row




回答3:


This is what you need to use when you different types of Views inside a ListView or RecyclerView :-

getItemViewType() and getViewTypeCount()

First you need to use getViewTypeCount() and return the number of unique views you need inside your List. Then override getItemViewType() and return the View type based on the whatever condition you want your views to change on inside the ListView.

Hope it will help.



来源:https://stackoverflow.com/questions/29467455/android-listview-with-multiple-layouts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!