2012-06-25 26 views
6

Tôi có bộ điều hợp tùy chỉnh cho ListView Tôi muốn thêm tên dự án làm tiêu đề cho yêu cầu công việc của mình. Thêm một tiêu đề duy nhất hoạt động tốt nhưng tôi không chắc chắn cách thêm nhiều tiêu đề bằng cách sử dụng addHeaderView. Tôi không hiểu chính xác nơi đặt setAdapter hoặc nó được cho là được đặt nhiều lần?Cách thêm nhiều chế độ xem tiêu đề trong một ListView

Đây là mã java của tôi cho một tiêu đề duy nhất mà hoạt động:

mListView = (ListView)findViewById(R.id.dashboardList); 
View header1 = getLayoutInflater().inflate(R.layout.listview_header, null, false); 
tv = (TextView) header1.findViewById(R.id.listHeader); 
adapter = new MyCustomAdapter(MyDashboardActivity.this, R.layout.mydashboard_row, dashboardBean); 
tv.setText("Project 1"); 
mListView.addHeaderView(header1, null, false); 
for (int i=0; i < 7; i++) { 
    dashboardBean.add(new DashboardBean(workRequests[i],status[i],actualHours[i])); 
} 
mListView.setAdapter(adapter); 

Bây giờ, tôi cho hai phần đầu tôi đã cố gắng này:

mListView = (ListView)findViewById(R.id.dashboardList); 
View header1 = getLayoutInflater().inflate(R.layout.listview_header, null, false); 
tv = (TextView) header1.findViewById(R.id.listHeader); 
adapter = new MyCustomAdapter(MyDashboardActivity.this, R.layout.mydashboard_row, dashboardBean); 
tv.setText("RxOffice"); 
mListView.addHeaderView(header1, null, false); 
for (int i=0; i < 4; i++) { 
    dashboardBean.add(new DashboardBean(workRequests[i],status[i],actualHours[i])); 
} 

tv.setText(Project 2"); 

mListView.addHeaderView(header1, null, false); 
for (int i=4; i < workRequests.length; i++) { 
    dashboardBean.add(new DashboardBean(workRequests[i],status[i],actualHours[i])); 
} 
mListView.setAdapter(adapter); 

Nhưng điều này không làm việc! Nó chỉ cho tôi tiêu đề Project 2 và tất cả 7 mục bên dưới nó. Bất cứ ai có thể xin vui lòng cho tôi biết những gì sai? Tôi đoán nó có cái gì đó để làm với setAdapter. Cảm ơn!

Trả lời

11

Tôi không nghĩ rằng những gì bạn muốn làm là có thể theo cách bạn đang cố gắng để làm điều đó. Khi bạn sử dụng addHeaderView, nó sẽ kết thúc tốt đẹp ListAdapter của bạn trong HeaderViewListAdapter. Tôi đã xem các tài liệu cho nó here và điều đó dường như ngụ ý rằng bạn có thể có nhiều tiêu đề, nhưng tất cả chúng sẽ ở đầu (duh, tiêu đề).

Nghe có vẻ giống như những gì bạn thực sự muốn là seperators ...

Bạn có thể sử dụng CommonWare's MergeAdapter. Nó sẽ cho phép bạn chèn bộ điều hợp và khung nhìn (theo thứ tự nào bạn muốn) và trình bày tất cả chúng dưới dạng một bộ điều hợp duy nhất cho một listview. Bạn chỉ cần đưa tiêu đề và bộ điều hợp cho từng phần nội dung và sau đó đặt nó vào danh sách của bạn.

Pseudo-code ví dụ:

myMergeAdapter = new MergeAdapter(); 
myMergeAdapter.addView(HeaderView1); 
myMergeAdapter.addAdapter(listAdapter1); 
myMergeAdapter.addView(HeaderView2); 
myMergeAdapter.addAdapter(listAdapter2); 
setListAdapter(myMergeAdapter); 
2

tôi đã đạt được nhiều kịch bản tiêu đề sử dụng tùy chỉnh Section Adapter được ban đầu được mã hóa bởi CommonsWare, bạn có thể làm cho phần bên trong listivew, như Sách, Trò chơi và vv kiểm tra bên dưới mã.

Mục Adaptor:

package com.medplan.db; 

import java.util.ArrayList; 
import java.util.List; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Adapter; 
import android.widget.BaseAdapter; 




abstract public class SectionedAdapter extends BaseAdapter { 

    String TAG = "========SectionedAdapter============"; 

abstract protected View getHeaderView(String caption, 
             int index, 
             View convertView, 
             ViewGroup parent); 

private List<Section> sections=new ArrayList<Section>(); 
private static int TYPE_SECTION_HEADER=0; 

public SectionedAdapter() { 
    super(); 
    sections.clear(); 


} 

public void addSection(String caption, Adapter adapter) { 

    sections.add(new Section(caption, adapter)); 
} 


public void clear() { 

    sections.clear(); 
    notifyDataSetChanged(); 
} 


public Object getItem(int position) { 
    for (Section section : this.sections) { 
    if (position==0) { 
     return(section); 
    } 

    int size=section.adapter.getCount()+1; 

    if (position<size) { 
     return(section.adapter.getItem(position-1)); 
    } 

    position-=size; 
    } 

    return(null); 
} 

public int getCount() { 
    int total=0; 

    for (Section section : this.sections) { 
    total+=section.adapter.getCount()+1; // add one for header 
    } 

    return(total); 
} 

public int getViewTypeCount() { 
    int total=1; // one for the header, plus those from sections 

    for (Section section : this.sections) { 
    total+=section.adapter.getViewTypeCount(); 
    } 

    return(total); 
} 

public int getItemViewType(int position) { 
    int typeOffset=TYPE_SECTION_HEADER+1; // start counting from here 

    for (Section section : this.sections) { 
    if (position==0) { 
     return(TYPE_SECTION_HEADER); 
    } 

    int size=section.adapter.getCount()+1; 

    if (position<size) { 
     return(typeOffset+section.adapter.getItemViewType(position-1)); 
    } 

    position-=size; 
    typeOffset+=section.adapter.getViewTypeCount(); 
    } 

    return(-1); 
} 

public boolean areAllItemsSelectable() { 
    return(false); 
} 

public boolean isEnabled(int position) { 
    return(getItemViewType(position)!=TYPE_SECTION_HEADER); 
} 

public View getView(int position, View convertView, 
        ViewGroup parent) { 
    int sectionIndex=0; 

    for (Section section : this.sections) { 
    if (position==0) { 
     return(getHeaderView(section.caption, sectionIndex, 
          convertView, parent)); 
    } 

    int size=section.adapter.getCount()+1; 

    if (position<size) { 
     return(section.adapter.getView(position-1,convertView,parent)); 
    } 

    position-=size; 
    sectionIndex++; 
    } 

    return(null); 
} 

public long getItemId(int position) { 
    return(position); 
} 

class Section { 
    String caption = null; 
    Adapter adapter = null; 

    Section(String caption, Adapter adapter) { 
    this.caption=caption; 
    this.adapter=adapter; 
    } 
} 
} 

Trong Hoạt động thực hiện mục đối tượng bộ chuyển đổi, hãy kiểm tra mã dưới đây:

final SectionedAdapter adapter =new SectionedAdapter() 
       { 

         protected View getHeaderView(String caption, int index, View convertView,ViewGroup parent) 
         { 

         result=(TextView)convertView; 

         if (convertView==null) 
         { 
          result=(TextView)getLayoutInflater().inflate(R.layout.section_header,null); 

         } 

         result.setText(caption); 
         // temp=caption; 
         // ind=index; 

         return(result); 
         } 
        }; 

section_header.xml

<?xml version="1.0" encoding="utf-8"?> 

<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/black" 
    android:textColor="#FFFFFF" 
    android:ellipsize="end" 
    android:textSize="11sp" 
    style="?android:attr/listSeparatorTextViewStyle" /> 


<!-- android:background="#515050"--> 

Trong Hoạt động thêm phần như nhiều như bạn muốn như dưới đây:

Lưu ý: ảnh của người sử & medPic là tên của ArrayList.

adapter.addSection("section first", new EfficientAdapter(getApplicationContext(),usersPic)); 


adapter.addSection("section second", new EfficientAdapter(getApplicationContext(),medPic)); 

listview.setAdapter(adapter); 
+0

Tôi đã đọc về nó. Tôi muốn biết nếu tôi có thể làm điều đó mà không sử dụng 'MergeAdapter'. Hóa ra tôi không thể. Cảm ơn – Harsh

0

Tôi sẽ giải quyết vấn đề này với ExpandableListView. Nó không yêu cầu thêm bất kỳ thư viện nào.

  • Tạo bộ tiếp hợp tùy chỉnh của bạn.
  • Cung cấp dữ liệu của bạn và điền vào các phương pháp bạn cần ghi đè.
  • Sau khi đặt bộ điều hợp với ExpandableListView:
    • Ghi đè nhómItemNhấp vào các nhóm sẽ không thực sự mở rộng chế độ xem.
    • Lặp qua từng phụ huynh trong bộ điều hợp và đặt chúng mở rộng.
Các vấn đề liên quan