在ExpandableListView
中,有沒有辦法隱藏沒有子女的組的組指示符?ExpandableListView - 隱藏無子女組的指示燈
回答
android:groupIndicator
屬性需要啓用狀態drawable。也就是說,您可以爲不同的狀態設置不同的圖像。
在組一直沒有孩子,相應的狀態是「state_empty」
看到這些參考鏈接:
爲state_empty
,您可以設置不同的圖像這是不混淆,或者,只是使用透明的顏色來顯示沒有什麼...
添加此項目在你的有狀態drawable與其他...
3210所以,你statelist可以是這樣的:
getExpandableListView().setGroupIndicator(getResources().getDrawable(R.drawable.my_group_statelist));
我:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_empty="true" android:drawable="@android:color/transparent"/>
<item android:state_expanded="true" android:drawable="@drawable/my_icon_max" />
<item android:drawable="@drawable/my_icon_min" />
</selector>
如果您使用的是ExpandableListActivity,你可以按照如下設置的onCreate的groupindicator測試了這個工作。
正如在另一個答案中提到的,由於Android將未展開的列表組視爲空,因此即使該組具有子組件也不會繪製該圖標。
這個環節解決了這個問題對我來說: http://mylifewithandroid.blogspot.com/2011/06/hiding-group-indicator-for-empty-groups.html
基本上,你必須設置缺省繪製透明,將繪製到您的組視圖作爲一個ImageView的和您的適配器切換圖像。
基於StrayPointer的答案,並從博客的代碼,就可以簡化代碼甚至更多:
在XML中添加如下因素來ExpandableListView:
android:groupIndicator="@android:color/transparent"
然後,在你做的適配器以下內容:
@Override
protected void bindGroupView(View view, Context paramContext, Cursor cursor, boolean paramBoolean){
**...**
if (getChildrenCount(groupPosition) == 0) {
indicator.setVisibility(View.INVISIBLE);
} else {
indicator.setVisibility(View.VISIBLE);
indicator.setImageResource(isExpanded ? R.drawable.list_group_expanded : R.drawable.list_group_closed);
}
}
通過使用setImageResource方法,您可以完成所有工作。 您不需要在適配器中使用三個整數數組。你也不需要一個XML選擇器來擴展和摺疊狀態。全部通過Java完成。
此外,此方法還會顯示正確的指標,當某個組默認展開時,哪些內容與博客中的代碼無效。
試試這個>>>
所有項目
getExpandableListView().setGroupIndicator(null);
在XML
android:groupIndicator="@null"
使用此它是完全爲我工作。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/group_indicator_expanded" android:state_empty="false" android:state_expanded="true"/>
<item android:drawable="@drawable/group_indicator" android:state_empty="true"/>
<item android:drawable="@drawable/group_indicator"/>
</selector>
你可以發佈你的ExpandableListView代碼嗎?這不適合我(group_indicator出現在有和沒有孩子的團體中)。 – 2013-12-03 22:25:01
我已經看過這段代碼很多次了,不工作。 – Joseph 2014-04-17 17:02:51
在你的代碼,只需使用自定義XML的組列表,並在把ImageView的爲GroupIndicator。
並添加陣列下面你ExpandableListAdapter
private static final int[] EMPTY_STATE_SET = {};
private static final int[] GROUP_EXPANDED_STATE_SET = { android.R.attr.state_expanded };
private static final int[][] GROUP_STATE_SETS = { EMPTY_STATE_SET, // 0
GROUP_EXPANDED_STATE_SET // 1
};
也ExpandableListAdapter的方法添加同樣的事情如下
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
if (convertView == null)
{
LayoutInflater infalInflater = (LayoutInflater) this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.row_group_list, null);
}
//Image view which you put in row_group_list.xml
View ind = convertView.findViewById(R.id.iv_navigation);
if (ind != null)
{
ImageView indicator = (ImageView) ind;
if (getChildrenCount(groupPosition) == 0)
{
indicator.setVisibility(View.INVISIBLE);
}
else
{
indicator.setVisibility(View.VISIBLE);
int stateSetIndex = (isExpanded ? 1 : 0);
Drawable drawable = indicator.getDrawable();
drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
}
}
return convertView;
}
參考: http://mylifewithandroid.blogspot.in/2011/06/hiding-group-indicator-for-empty-groups.html
建議你我的解決方案:
1)清除默認groupIndicator:
<ExpandableListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="240dp"
android:layout_gravity="start"
android:background="#cccc"
android:groupIndicator="@android:color/transparent"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
/>
2)在ExpandableAdapter:
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = new TextView(context);
}
((TextView) convertView).setText(groupItem.get(groupPosition));
((TextView) convertView).setHeight(groupHeight);
((TextView) convertView).setTextSize(groupTextSize);
//create groupIndicator using TextView drawable
if (getChildrenCount(groupPosition)>0) {
Drawable zzz ;
if (isExpanded) {
zzz = context.getResources().getDrawable(R.drawable.arrowup);
} else {
zzz = context.getResources().getDrawable(R.drawable.arrowdown);
}
zzz.setBounds(0, 0, groupHeight, groupHeight);
((TextView) convertView).setCompoundDrawables(null, null,zzz, null);
}
convertView.setTag(groupItem.get(groupPosition));
return convertView;
}
簡單地說,你創建一個高度= 0一個新的XML佈局爲隱藏的組頭。 例如,它的 'group_list_item_empty.xml'
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp">
</RelativeLayout>
那麼你的正常組頭的佈局是 'your_group_list_item_file.xml'
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal">
...your xml layout define...
</LinearLayout>
最後,你只是在你的適配器類別更新getGroupView方法:
public class MyExpandableListAdapter extends BaseExpandableListAdapter{
//Your code here ...
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
if (Your condition to hide the group header){
if (convertView == null || convertView instanceof LinearLayout) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.group_list_item_empty, null);
}
return convertView;
}else{
if (convertView == null || convertView instanceof RelativeLayout) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.your_group_list_item_file, null);
}
//Your code here ...
return convertView;
}
}
}
重要:佈局文件的根標籤(隱藏和正常)米烏斯季有所不同(如上面的例子中,LinearLayout中和的RelativeLayout)
,這可能是從XML的另一種方式,設置android:groupIndicator="@null"
你有沒有試圖改變ExpandableListView
的屬性android:groupIndicator="@null"
?
並非適用於所有人羣。 – Ich 2016-01-05 02:02:36
convertView.setVisibility(View.GONE)應該做的伎倆。
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
DistanceHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.list_search_distance_header, parent, false);
if (getChildrenCount(groupPosition)==0) {
convertView.setVisibility(View.GONE);
}
這只是刪除所有項目:列表是空的 - >我很傷心:( – Ich 2016-01-05 02:04:44
你有沒有添加計數器檢查?if(getChildrenCount(groupPosition)== 0){ – 2017-03-30 21:49:16
只是想改進Mihir Trivedi的答案。您可以在getGroupView把這個()這是MyExpandableListAdapter類
View ind = convertView.findViewById(R.id.group_indicator);
View ind2 = convertView.findViewById(R.id.group_indicator2);
if (ind != null)
{
ImageView indicator = (ImageView) ind;
if (getChildrenCount(groupPosition) == 0)
{
indicator.setVisibility(View.INVISIBLE);
}
else
{
indicator.setVisibility(View.VISIBLE);
int stateSetIndex = (isExpanded ? 1 : 0);
/*toggles down button to change upwards when list has expanded*/
if(stateSetIndex == 1){
ind.setVisibility(View.INVISIBLE);
ind2.setVisibility(View.VISIBLE);
Drawable drawable = indicator.getDrawable();
drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
}
else if(stateSetIndex == 0){
ind.setVisibility(View.VISIBLE);
ind2.setVisibility(View.INVISIBLE);
Drawable drawable = indicator.getDrawable();
drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
}
}
}
裏面......並以此爲佈局來看,這是我的group_items.xml如何似乎
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/group_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:textSize="15sp"
android:textStyle="bold"/>
<ImageView
android:id="@+id/group_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/arrow_down_float"
android:layout_alignParentRight="true"
android:paddingRight="20dp"
android:paddingTop="20dp"/>
<ImageView
android:id="@+id/group_indicator2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/arrow_up_float"
android:layout_alignParentRight="true"
android:visibility="gone"
android:paddingRight="20dp"
android:paddingTop="20dp"/>
希望這有助於......記得留下一個upvote
- 1. 隱藏ExpandableListView的組標題
- 2. RadGridView隱藏兒童指示燈
- 3. 忙碌指示燈在Android的隱藏在工作燈6.1
- 4. 無法隱藏網絡活動指示燈
- 5. Android:在ExpandableListView中隱藏子分隔符
- 6. ExpandableListView中的隱藏按鈕
- 7. xamarin andriod ExpandableListView:從組中刪除或隱藏子視圖
- 8. 如何顯示/隱藏子組件
- 9. XULRunner在初始啓動時沒有隱藏繁忙指示燈
- 10. 使用移動設備時隱藏blueimp指示燈?
- 11. jquery div幻燈片隱藏子元素
- 12. 如何完全隱藏ExpandableListView的groupIndicator?
- 13. jQuery隱藏顯示幻燈片懸停
- 14. jQuery幻燈片和顯示/隱藏div
- 15. 顯示/隱藏和幻燈片表
- 16. 如何隱藏Android中的ExpandableListView中的組
- 17. 如何在ICS手機上禁用/隱藏三點指示燈(選項菜單指示燈)
- 18. 隱藏分隔線沒有隱藏childDivider on ExpandableListView
- 19. 隱藏AS3代碼中的CS5對象的所有子女
- 20. 將組容器高度設置爲0不會隱藏他的子女
- 21. 顯示或隱藏子div
- 22. 顯示/隱藏子視圖
- 23. 顯示/隱藏無框架
- 24. 在隱藏其所有子組件時隱藏的Wicket容器
- 25. Android ExpandableListView - 處理沒有子女的父母
- 26. 離子幻燈片(離子幻燈片):在達到結尾時隱藏pagger
- 27. 無法隱藏API調用結束時的活動指示器
- 28. XSLT複製的無子女
- 29. 隱藏分組列的子標題 - nestedTreeDataGrid
- 30. 的NSView顯示和隱藏子視圖
澄清:有沒有辦法隱藏組指標只有沒有孩子的組? – AlleyOOP 2015-07-07 23:24:14