Different row layouts in ListView

孤人 提交于 2019-11-27 08:57:25

Implement the getItemViewType() and getViewTypeCount() for your adapter:

@Override
public int getViewTypeCount() {
   return 2; //return 2, you have two types that the getView() method will return, normal(0) and for the last row(1)
}

and:

@Override
public int getItemViewType(int position) {
    return (position == this.getCount() - 1) ? 1 : 0; //if we are at the last position then return 1, for any other position return 0
}

Then in the getView() method find out what type of view to inflate:

public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        int theType = getItemViewType(position); 
        if (view == null) {
          ViewHolder holder = new ViewHolder();
          LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          if (theType == 0) {
              // inflate the ordinary row
              view = vi.inflate(R.layout.list_item_bn, null);
              holder.textView = (TextView)view.findViewById(R.id.tv_name);      
          } else if (theType == 1){
             // inflate the row for the last position
              view = vi.inflate(R.layout.list_item_record, null);
              holder.textView = (TextView)view.findViewById(R.id.record_view);
          } 
          view.setTag(holder);
         }
 //other stuff here, keep in mind that you have a different layout for your last position so double check what are trying to initialize
}

The example from the comments: http://pastebin.com/gn65240B (or https://gist.github.com/2641914 )

The problem is that once you've inflated the view it can be reused many times in any position. I'd suggest the following approach: you inflate all but last item as usual (including the view holder), but for the last item you hold the reference as a field of CustomListAdapter and return it every time the last item is requested:

private class CustomListAdapter extends ArrayAdapter { 
    ...
    private View mLastItem; 

    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        ...
        int lastpos = mList.size()-1;

        if (view == null) {
            ViewHolder holder = new ViewHolder();
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (position == lastpos) {
                view = vi.inflate(R.layout.list_item_record, null);
                holder.textView = (TextView)view.findViewById(R.id.record_view);
                mLastItem = view;
            }
            else {
                view = vi.inflate(R.layout.list_item_bn, null);
                holder.textView = (TextView)view.findViewById(R.id.tv_name);
            }
            view.setTag(holder);
        }

        if (position == lastpos) {
            ... // Update the last item here
            return mLastItem; 
        }
        ...
    }

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