2011-07-14 40 views
17

Tôi muốn đặt dải phân cách chỉ giữa các phần tử gốc. Khi tôi đặt ExpandableListView - Cách đặt chỉ chia giữa các phần tử cha

android:divider="@drawable/divider"
Android sẽ tạo dải phân cách giữa các phần tử gốc, nhưng cũng tạo dải phân cách giữa các phần tử con. Khi tôi thêm
android:childDivider="@color/transparent"
android sẽ xóa dải phân cách giữa các phần tử con, nhưng khoảng trống giữa chúng vẫn còn. Tại sao? Tôi đã cố gắng
android:dividerHeight="0dp"
nhưng không có gì xảy ra.

Tại tất cả tôi muốn đặt dải phân cách giữa các phần tử gốc, nhưng tôi không muốn bất kỳ dải phân cách hoặc khoảng trắng nào giữa các phần tử con.

bất kỳ ý tưởng nào về cách thực hiện điều đó?

Trả lời

1

Những gì tôi đã làm là để thiết lập chia của ExpandableListView 0 và sau đó chèn "chia" nhóm vào đó là ExpandableListAdapter:

// To get dividers between the groups we add 'divider views' as if they were 
// themselves groups. 
public int getGroupCount() { 
// Actual groups are at even groupPositions, dividers at odd 
    return (this.data.size() * 2) - 1; 
} 

public long getGroupId(int groupPosition) { 
    if(groupPosition % 2 != 0) return R.id.divider; 
    else return R.id.group; 
} 

public View getGroupView(int groupPosition, boolean isExpanded, 
     View convertView, ViewGroup parent) { 
    if(groupPosition % 2 != 0) { 
     View divider = convertView; 
     if(divider == null || divider.getId() != R.id.divider) { 
      divider = this.inflater.inflate(R.layout.divider, null); 
     } 

     return divider; 
    } 

    View group = convertView; 
    if(group == null || group.getId() != R.id.group) { 
     group = this.inflater.inflate(R.layout.group, null); 
    } 

    return group; 
} 
4

Bí quyết là để tạo thêm một view cho bố trí nhóm sụp đổ và bố trí con cuối cùng.

collapse_group.xml:

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/transparent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="17sp"/> 

    <!-- this is the extra view--> 
    <View 
     android:layout_width="fill_parent" 
     android:layout_height="10dp" 
     android:background="@android:color/transparent"/>   

</LinearLayout> 

expanded_group.xml:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/transparent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="17sp"/> 

    <!-- no extra view-->  

</LinearLayout> 

Và áp dụng các kỹ thuật tương tự để xem đứa trẻ, cho xem con cuối cùng chèn xem thêm

+0

Đó chỉ là hoàn hảo! –

+0

hoặc có thể thay vì tạo hai chế độ xem riêng biệt, chỉ cần đặt chế độ hiển thị thành View.GONE cho seperator Xem khi không yêu cầu –

-2

bộ như thế này android:divider="@null" không có dải phân cách cho cha mẹ android:divider="2dp" đặt dải phân cách cho cha mẹ

<ExpandableListView 
     android:id="@+id/expandable_list" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:background="#ffffff" 
     android:divider="2dp" 
     android:groupIndicator="@drawable/group_indicator" /> 
22

Để loại bỏ vách ngăn chỉ từ quan điểm con và không giữa các bậc phụ huynh trong danh sách mở rộng:

thêm android:childDivider="#00000000" trong ExapandableListView thuộc tính trong XML:

<ExpandableListView 
    android:id="@+id/elv" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:childDivider="#00000000" /> 

Tham khảo this page để biết thêm thông tin

+0

đó là nhận xét CHỈ cho đến nay đã giải quyết được vấn đề của tôi sau khi trải qua 50 trang SO – Muppet

0
expListView.setDividerHeight(10); 
expListView.setChildDivider(getResources().getDrawable(
      android.R.color.white)); 
0

Nếu bạn muốn có dải phân cách giữa phần tử gốc của danh sách ExpandableListV iew (Header) và không muốn có chia giữa trẻ em và cha mẹ, mà rõ ràng là trường hợp tự nhiên nhất, sau đó bạn phải làm như sau:

  1. Trong tệp gốc xml của bạn thêm một Xem ở phía dưới của bố cục với chiều cao dải phân cách và màu chia mà bạn cần. Ví dụ:

  2. Làm tương tự trong file xml đứa trẻ. Thêm một Chế độ xem ở cuối bố cục với chiều cao dải phân cách và màu chia mà bạn cần. Đảm bảo màu và chiều cao của dải phân cách giống như trong xml gốc.

  3. Into phương pháp ExpandableListView Adaptor của bạn công khai Xem getGroupView (int groupPosition, boolean isExpanded, Xem convertView, ViewGroup mẹ) quản lý trạng thái của Xem chia như thế này: Ví dụ:

    if (isExpanded) { tiêu đềDividerView.setBackgroundColor (ContextCompat.getColor (ngữ cảnh, R.color.darker_grey)); } else { tiêu đềDividerView.setBackgroundColor (ContextCompat.getColor (ngữ cảnh, android.R.color.transparent)); }

BTW: Trong trường hợp của tôi R.color.darker_grey là màu nền của phụ huynh

  1. Remove from ExpandableListView xml của bạn tất cả các ngăn và chia chiều cao. Đặt ExpandableListView thành không có dải phân cách và chiều cao dải phân cách. Bạn có thể sử dụng attrs như thế này:

    android: groupIndicator = "@ null" android: childIndicator = "@ null" android: chia = "@ null" android: dividerHeight = "0dp"

3

cho màu phân và chiều cao của nó (chia sẽ hiển thị khi nhóm đang sụp đổ)

<ExpandableListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:divider="@color/color_black" 
     android:dividerHeight="1dp" 
     android:groupIndicator="@null" /> 
+0

Đặt chiều cao cho tôi , hơn bạn! – jai

0

Nếu bạn muốn loại bỏ chỉ chia con, một mẹo đơn giản là bạn có thể tạo một drawable với cùng một màu sắc như màu nền của mục con bạn. Sau đó thiết lập nó như chia con.

ShapeDrawable sd1 = new ShapeDrawable(new RectShape()); 
    sd1.getPaint().setColor(YOUR CHILD ITEM BACKGROUND COLOR); 
    mExpListView.setChildDivider(sd1); 

Hoặc sử dụng xml:

android:childDivider="@color/child_bg_color" 
Các vấn đề liên quan