不同的行布局中的ListView [英] Different row layouts in ListView

查看:115
本文介绍了不同的行布局中的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这职位是与此相关的 ViewHolder不工作。在这个职位,我下面就如何使用 ViewHolder 的ListView 的教程。我现在希望的就是有一个的ListView 的最后一个项目有不同的布局比其他人。这是我的code:

This post is related to this ViewHolder not working. On that post, I was following a tutorial on how to use ViewHolder on a ListView. What I want now is to have the last item on a ListView to have a different layout than the rest. Here's my code:

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 并有检查一个条件,如果当前位置等于最后一个项目。然而,这code设置的最后一个项目的布局相同休息。 getView 方法全code:

I just get the ListView size minus 1 and have a condition that checks the if current position is equal to the last item. However, this code sets the last item's layout same as the rest. getView method full code:

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

}

任何人可以帮助我?

Could anybody help me with this?

推荐答案

实施<一个href="http://developer.android.com/reference/android/widget/BaseAdapter.html#getItemViewType%28int%29"><$c$c>getItemViewType()和<一href="http://developer.android.com/reference/android/widget/BaseAdapter.html#getViewTypeCount%28%29"><$c$c>getViewTypeCount()为您的适配器:

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

@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()方法找出来看什么类型的膨胀:

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
}

从注​​释的例子: http://pastebin.com/gn65240B (或的 https://gist.github.com/2641914

这篇关于不同的行布局中的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆