ListView 中的不同行布局

2024-01-08

这篇文章与此相关ViewHolder 不工作 https://stackoverflow.com/questions/9663569/viewholder-not-working/9663680。在那篇文章中,我正在遵循有关如何使用的教程ViewHolder on a ListView。我现在想要的是最后一个项目ListView具有与其他部分不同的布局。这是我的代码:

int lastpos = mList.size()-1;
          System.out.println("position: "+position+" mlist: "+lastpos);

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

我刚刚得到ListView尺寸减去1并有一个条件检查当前位置是否等于最后一项。但是,此代码将最后一个项目的布局设置为与其余项目相同。getView方法完整代码:

private class CustomListAdapter extends ArrayAdapter { 
    private ArrayList mList;
    private Context mContext;

    public CustomListAdapter(Context context, int textViewResourceId,
            ArrayList list) {
        super(context, textViewResourceId, list);
        this.mList = list;
        this.mContext = context;
    }   

    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;

        final ToggleButton tb;

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

          int lastpos = mList.size()-1;
          System.out.println("position: "+position+" mlist: "+lastpos);

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

         final ItemsDisplay listItem = (ItemsDisplay) mList.get(position);
            if (listItem != null) {
             ViewHolder holder1 = (ViewHolder)view.getTag();
             holder1.textView.setText(listItem.getTag());

             view.setOnClickListener(new OnClickListener() {
                    public void onClick(View arg0) { //--clickOnListItem
                     Toast.makeText(getBaseContext(),
                       "Button is "+position+" "+listItem.getTag(),
                       Toast.LENGTH_LONG).show();
                    }
                });

                tb  = (ToggleButton) view.findViewById(R.id.toggleButton);
                tb.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {

               if(tb.isChecked()){
                Toast.makeText(getBaseContext(),
                           "Button is "+position+" checked:"+tb.isChecked()+" "+listItem.getTag(),
                           Toast.LENGTH_LONG).show();
               }else{
                Toast.makeText(getBaseContext(),
                  "Button is "+position+" checked:"+tb.isChecked()+" "+listItem.getTag(),
                           Toast.LENGTH_LONG).show();
               }
              }
                });
            }



        return view;  
    }

}

有人可以帮我解决这个问题吗?


实施getItemViewType() http://developer.android.com/reference/android/widget/BaseAdapter.html#getItemViewType%28int%29 and getViewTypeCount() http://developer.android.com/reference/android/widget/BaseAdapter.html#getViewTypeCount%28%29对于您的适配器:

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

然后在getView()方法找出要膨胀的视图类型:

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
}

评论中的例子:http://pastebin.com/gn65240B http://pastebin.com/gn65240B (or https://gist.github.com/2641914 https://gist.github.com/2641914 )

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ListView 中的不同行布局 的相关文章

随机推荐