2013-11-01 14 views
7

Tôi muốn tạo Listview, trong đó tôi muốn bố trí khác nhau cho tất cả các hàng khác nhau. Sau đó, làm cách nào tôi có thể tạo bộ điều hợp tùy chỉnh để đặt bố cục khác nhau cho hàng khác nhau.Android Listview với bố cục khác nhau cho mỗi hàng?

Mọi trợ giúp sẽ được đánh giá cao.

Cảm ơn bạn trước.

+0

việc kiểm tra này http://stackoverflow.com/questions/4777272/android-listview-with-different-layout-for-each-row –

+0

sử dụng chế độ xem danh sách tùy chỉnh với bộ điều hợp tùy chỉnh – Raghunandan

+0

bạn có thể cung cấp mã medemo cho –

Trả lời

6

tạo bộ điều hợp thông thường, trong hàm create_view thổi phồng bố cục xml hàng theo loại hàng.

ví dụ

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context 
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    if (position % 2 == 0) 
     xml_type = R.layout.row_one 
    else 
     xml_type = R.layout.row_two 

    View rowView = inflater.inflate(xml_type, parent, false); 
} 
+0

cảm ơn sự giúp đỡ của bạn ... –

8

Bạn cần phải mở rộng Adapter của bạn, và ghi đè phương pháp getView của nó.

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    int resource; 

    // Here you set ‘resource’ with the correct layout, for the row 
    // given by the parameter ‘position.’ 
    // 
    // E.g.: 
    // 
    // switch (someArray[position].type) { 
    // case SOME_TYPE_A: resource = R.layout.a; break; 
    // case SOME_TYPE_B: resource = R.layout.b; break; 
    // ... 
    // } 

    View rowView = inflater.inflate(resource, parent, false); 

    // Here you initialize the contents of the newly created view. 
    // 
    // E.g.: 
    // switch (resource) { 
    // case R.layout.a: 
    //  TextView aA = (TextView) rowView.findViewById(R.id.aa); 
    //  aA.setText("View 1"); 
    //  ... 
    //  break; 
    // case R.layout.b: 
    //  TextView bB = (TextView) rowView.findViewById(R.id.bb); 
    //  bB.setText("View 2"); 
    //  ... 
    //  break; 
    // ... 
    // } 

    return rowView; 
} 

Để biết thêm ví dụ về bộ điều hợp và cách mở rộng, hãy xem các liên kết bên dưới.

+1

+1 cho sự đơn giản – A23149577

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