2011-06-30 49 views
11

Tôi đang cố gắng sử dụng một tùy chỉnh ExpandableListView với bố cục cha và con với XML mẹ và XML con.Android Danh sách mở rộng

Dữ liệu của tôi sẽ là phản hồi của máy chủ. Vì vậy, tôi tốt nhất muốn có dữ liệu dưới dạng Danh sách Mảng hoặc Bản đồ băm cho bố cục cha mẹ và con

Tôi kèm theo hình ảnh về những gì tôi muốn.

Có cách nào để thay đổi hình ảnh mũi tên danh sách có thể mở rộng (Mặc định) theo bất kỳ biểu diễn + hoặc - UI nào như được hiển thị bên dưới.

List

List1

Xin đề nghị một hướng dẫn hoặc logic mã cho tình hình cụ thể này.

Trả lời

5

Trong khi đó Tôi đã tạo ra một selector.xml như:

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

<selector xmlns:android="schemas.android.com/apk/res/android" > 

    <item 
     android:drawable="@drawable/friend_small" 
     android:state_expanded="true"/> 

    <item android:drawable="@drawable/place_small"/> 
</selector> 

và được đưa ra trong android:groupIndicator="@drawable/selector" trong xml - friend_small và đặt nhỏ là những hình ảnh của tôi về mở rộng và sụp đổ nhà nước

3

bạn có thể thay đổi chỉ bằng cách gọi setGroupIndicator

danh sách mở rộng là có thể hiển thị một chỉ báo bên cạnh mỗi mục để hiển thị trạng thái hiện tại của mặt hàng đó (các bang thường một trong những nhóm được mở rộng, nhóm sụp đổ, trẻ em, hoặc đứa trẻ cuối cùng). Sử dụng setChildIndicator (Drawable) hoặc setGroupIndicator (Drawable) (hoặc các thuộc tính XML tương ứng) để thiết lập các chỉ số này (xem tài liệu cho mỗi phương thức để xem trạng thái bổ sung mà mỗi Drawable có thể có).

cũng vậy, bạn cần thực hiện của riêng mình ExpandableListAdapter. nó có thể thổi phồng quan điểm của riêng bạn cho cả cha mẹ và trẻ em trong đó.

6

Sau một R & D qua danh sách mở rộng Tôi thấy rằng mở rộng Danh sách xem là chế độ xem dạng cây hai cấp do Android cung cấp.

Chế độ xem này chứa hai loại danh mục.

loại đầu tiên là Nhóm-Elements và thứ hai là Child-Elements, hay còn gọi là phụ huynh và phần tử con.

Mục đích chính của ví dụ này là tùy chỉnh chế độ xem danh sách có thể mở rộng làm hình ảnh trong câu hỏi hiển thị.

Tôi đã đề cập đến một số chủ đề quan trọng về chế độ xem danh sách có thể mở rộng mà tôi phải đối mặt trong quá trình trải nghiệm của mình.

Mã dưới đây là main.xml chứa chế độ xem danh sách có thể mở rộng. main.xml

<!--?xml version="1.0" encoding="UTF-8"?--> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> 

    <expandablelistview android:id="@+id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:groupindicator="@drawable/group_indicator.xml"> 

    <textview android:id="@+id/android:empty" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/main_no_items">  
</textview></expandablelistview></linearlayout> 

các group_row.xml như sau chứa bố trí cho nhóm danh sách mở rộng quan điểm structure.group_row.xml ?

 <textview android:id="@+id/tvGroupName" android:layout_width="wrap_content" android:layout_height="40dip" android:textsize="16sp" android:textstyle="bold" android:paddingleft="30dip" android:gravity="center_vertical"> 

child_row.xml đây là chứa các bố trí cho danh sách có thể mở rộng cấu trúc giao diện nhóm. child_row.xml

<!--?xml version="1.0" encoding="utf-8"?--> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="40dip" android:gravity="center_vertical"> 

    <textview android:id="@+id/tvPlayerName" android:paddingleft="50dip" android:textsize="14sp" android:layout_width="wrap_content" android:layout_height="30dip" android:gravity="center_vertical"> 

</textview></linearlayout> 

Đầu đọc tài liệu tham khảo của listview có thể mở rộng từ xml để lớp hoạt động.

public class ExpList extends ExpandableListActivity 
    { 
    /** 
     * strings for group elements 
     */ 
     static final String arrGroupelements[] = 
     { 
     "India", 
     "Australia", 
     "England", 
     "South Africa" 
    }; 

     /** 
     * strings for child elements 
     */ 
    static final String arrChildelements[][] = 
    { 
     { 
     "Sachin Tendulkar", 
     "Raina", 
     "Dhoni", 
     "Yuvi" 
     }, 
     { 
     "Ponting", 
     "Adam Gilchrist", 
     "Michael Clarke" 
     }, 
     { 
     "Andrew Strauss", 
     "kevin Peterson", 
     "Nasser Hussain" 
     }, 
     { 
     "Graeme Smith", 
     "AB de villiers", 
     "Jacques Kallis" 
     } 
     }; 

    DisplayMetrics metrics; 
    int width; 
    ExpandableListView expList; 

     @Override 
     public void onCreate(Bundle savedInstanceState) 
     { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 

      expList = getExpandableListView(); 
      metrics = new DisplayMetrics(); 
      getWindowManager().getDefaultDisplay().getMetrics(metrics); 
      width = metrics.widthPixels; 
      //this code for adjusting the group indicator into right side of the view 


if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) { 
      expList.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10)); 
     } else { 
      expList.setIndicatorBoundsRelative(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10)); 
     } 

      expList.setAdapter(new ExpAdapter(this)); 

     expList.setOnGroupExpandListener(new OnGroupExpandListener() 
     { 
     @Override 
     public void onGroupExpand(int groupPosition) 
     { 
     Log.e("onGroupExpand", "OK"); 
     } 
     }); 

     expList.setOnGroupCollapseListener(new OnGroupCollapseListener() 
     { 
     @Override 
     public void onGroupCollapse(int groupPosition) 
     { 
     Log.e("onGroupCollapse", "OK"); 
     } 
     }); 

     expList.setOnChildClickListener(new OnChildClickListener() 
     { 
     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, 
     int groupPosition, int childPosition, long id) { 
     Log.e("OnChildClickListener", "OK"); 
     return false; 
     } 
     }); 
     } 

     public int GetDipsFromPixel(float pixels) 
     { 
     // Get the screen's density scale 
     final float scale = getResources().getDisplayMetrics().density; 
     // Convert the dps to pixels, based on density scale 
     return (int) (pixels * scale + 0.5f); 
     } 
    } 

Để tùy chỉnh điều chính Exp Listview là bộ điều hợp. Android cung cấp BaseExpandableListAdapter để tùy chỉnh chế độ xem. Dưới đây là mã cho thiết kế của Adapter.

Đây là bộ điều hợp cho chế độ xem danh sách có thể mở rộng để xây dựng phần tử nhóm và phần tử con.

public class ExpAdapter extends BaseExpandableListAdapter { 

    private Context myContext; 
    public ExpAdapter(Context context) { 
    myContext = context; 
    } 
    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
    return null; 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
    return 0; 
    } 

    @Override 
    public View getChildView(int groupPosition, int childPosition, 
    boolean isLastChild, View convertView, ViewGroup parent) { 

    if (convertView == null) { 
    LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = inflater.inflate(R.layout.child_row, null); 
    } 

    TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName); 
    tvPlayerName.setText(arrChildelements[groupPosition][childPosition]); 

    return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
    return arrChildelements[groupPosition].length; 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
    return null; 
    } 

    @Override 
    public int getGroupCount() { 
    return arrGroupelements.length; 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
    return 0; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
    View convertView, ViewGroup parent) { 

    if (convertView == null) { 
    LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = inflater.inflate(R.layout.group_row, null); 
    } 

    TextView tvGroupName = (TextView) convertView.findViewById(R.id.tvGroupName); 
    tvGroupName.setText(arrGroupelements[groupPosition]); 

    return convertView; 
    } 

    @Override 
    public boolean hasStableIds() { 
    return false; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
    return true; 
    } 
} 

group_indicator.xml Đây là mã để thay đổi hình ảnh chỉ báo mặc định.

<?xml version="1.0" encoding="UTF-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item android:state_expanded="true" android:drawable="@drawable/friend_small" /> 
     <item android:drawable="@drawable/place_small" /> 
    </selector> 
+0

Sau khi R & D, tôi đã triển khai mã sử dụng danh sách có thể mở rộng tùy chỉnh với bố cục cha và con làm group_row của tôi. XML và child_row. XML. –

+0

Ngoài ra, Căn chỉnh hình thu nhỏ mặc định và mở rộng hình ảnh sang phải ... Theo yêu cầu trong bố cục của tôi ... –

+0

Mở rộngListView không thể có chế độ xem con (trong xml). – ddmps

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