2012-03-12 40 views
8

Bài đăng này có liên quan đến điều này ViewHolder not working. Trên bài đăng đó, tôi đã làm theo hướng dẫn về cách sử dụng ViewHolder trên ListView. Những gì tôi muốn bây giờ là có mục cuối cùng trên ListView để có bố cục khác so với phần còn lại. Dưới đây là mã của tôi:Bố cục hàng khác nhau trong 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); 

tôi chỉ nhận được ListView kích thước trừ 1 và có một điều kiện để kiểm tra nếu vị trí hiện tại bằng với mục cuối cùng. Tuy nhiên, mã này đặt bố cục của mục cuối cùng giống như phần còn lại. getView phương thức đầy đủ mã:

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

} 

Ai có thể giúp tôi với điều này?

+1

dán mã bộ chuyển đổi đầy đủ của bạn ... –

+0

thêm. thats mã đầy đủ. – antonoVodka

+0

nếu vòng lặp gọi là kiểm tra như if (...) {Syso ("in if loop")} –

Trả lời

27

Thực hiện getItemViewType()getViewTypeCount() cho bộ chuyển đổi của bạn:

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

và:

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

Sau đó, trong getView() phương pháp tìm hiểu những gì loại nhằm thổi phồng:

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 
} 

Các ví dụ từ các ý kiến: http://pastebin.com/gn65240B (hoặc https://gist.github.com/2641914)

+0

tôi đã thử điều này nhưng vẫn không hoạt động. tôi đã thử in giá trị của vị trí và theType. vị trí đã cho tôi 0-5 chỉ với theType = 0. tại sao vậy? mList bao gồm 26 mục. Tôi chỉ tự hỏi tại sao vị trí có 6 giá trị chỉ chống lại 26 mục trong danh sách của tôi. – antonoVodka

+0

@ user1263567 Bạn đã cuộn danh sách xuống chưa? Tôi nghĩ rằng bình thường chỉ có được vị trí 5-6, có lẽ vì đây là vị trí có thể nhìn thấy trên màn hình, sẽ lãng phí khi gọi phương thức 'getView' cho các vị trí chưa được nhìn thấy. Nếu bạn cuộn danh sách, bạn sẽ thấy các vị trí khác. – Luksprog

+0

tôi hiểu. tuy nhiên, hàng cuối cùng vẫn giống như phần còn lại. tôi đã cố gắng chuyển hàng cuối cùng đến hàng đầu tiên, để xem nó có hoạt động không. nó hoạt động nhưng mỗi hàng thứ 7 cũng giống như hàng đầu tiên. chỉ có 1 nên là người duy nhất có bố cục khác với phần còn lại. – antonoVodka

0

Vấn đề là khi bạn đã tăng lượt xem, nó có thể được sử dụng lại nhiều lần ở bất kỳ vị trí nào. Tôi muốn đề xuất phương pháp sau: bạn thổi phồng tất cả trừ mục cuối cùng như bình thường (bao gồm cả người xem), nhưng đối với mục cuối cùng bạn giữ tham chiếu dưới dạng trường CustomListAdapter và trả lại mỗi khi mục cuối cùng được yêu cầu:

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

} 
+0

tính năng này cũng không hoạt động. nó đặt mục cuối cùng với phần còn lại. cảm ơn bạn mặc dù. – antonoVodka

Các vấn đề liên quan