development

ExpandableListView-자식이없는 그룹에 대한 표시기 숨기기

big-blog 2020. 8. 15. 09:52
반응형

ExpandableListView-자식이없는 그룹에 대한 표시기 숨기기


에서 ExpandableListView하위가없는 그룹에 대해 그룹 표시기를 숨기는 방법이 있습니까?


이것을보십시오 >>>

모든 항목

 getExpandableListView().setGroupIndicator(null);

XML에서

android:groupIndicator="@null"

android:groupIndicator속성은 상태를 사용할 수 당김을합니다. 즉, 상태마다 다른 이미지를 설정할 수 있습니다.

그룹에 자식이없는 경우 해당 상태는 'state_empty'입니다.

다음 참조 링크를 참조하십시오.

이것이것

의 경우 state_empty혼란스럽지 않은 다른 이미지를 설정하거나 단순히 투명한 색상을 사용하여 아무것도 표시하지 않을 수 있습니다.

이 항목을 다른 항목과 함께 상태 저장 드로어 블에 추가하십시오.

<item android:state_empty="true" android:drawable="@android:color/transparent"/>

따라서 상태 목록은 다음과 같을 수 있습니다.

<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에서 그룹 표시기를 설정할 수 있습니다.

getExpandableListView().setGroupIndicator(getResources().getDrawable(R.drawable.my_group_statelist));

나는 이것이 작동하는지 테스트했습니다.


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를 통해 수행됩니다.

또한이 접근 방식은 블로그의 코드에서 작동하지 않는 그룹이 기본적으로 확장 될 때 올바른 표시기를 표시합니다.


다른 답변에서 언급했듯이 Android는 확장되지 않은 목록 그룹을 빈 것으로 취급하므로 그룹에 자식이 있어도 아이콘이 그려지지 않습니다.

이 링크는 나를 위해 문제를 해결했습니다. http://mylifewithandroid.blogspot.com/2011/06/hiding-group-indicator-for-empty-groups.html

기본적으로 기본 드로어 블을 투명으로 설정하고 드로어 블을 그룹보기로 ImageView로 이동 한 다음 어댑터에서 이미지를 토글해야합니다.


코드에서 그룹 목록에 대한 사용자 지정 xml을 사용하고 GroupIndicator 에 대한 ImageView넣으십시오 .

그리고 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


이것은 XML의 또 다른 방법 일 수 있습니다. android:groupIndicator="@null"

참조 링크 : https://stackoverflow.com/a/5853520/2624806


XML에서

android:groupIndicator="@null"

에서 ExpandableListAdapter-> getGroupView다음 코드 복사

if (this.mListDataChild.get(this.mListDataHeader.get(groupPosition)).size() > 0){
      if (isExpanded) {
          arrowicon.setImageResource(R.drawable.group_up);
      } else {
          arrowicon.setImageResource(R.drawable.group_down);
      }
}

내 해결책을 제안하십시오.

1) 기본 그룹 지우기 표시기 :

<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) in 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;
}

Use this it is perfectly working for me.

<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>

Simply, you create a new xml layout with height=0 for the hidden group header. For example, it's '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>

Then your normal group header layout is '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>

Finally, you just update the getGroupView method in your Adapter Class:

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;
        }
    }
}

IMPORTANT: The root tag of the layout files (hidden and normal) must be different (as above example, LinearLayout and RelativeLayout )


Have you tried to change ExpandableListView's attribute android:groupIndicator="@null"?


Just wanted to improve on Mihir Trivedi's answer. You can place this within getGroupView() that's inside MyExpandableListAdapter class

    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]);
            }
        }
    }

...and as for the layout view, this is how my group_items.xml appears to be

<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"/>

Hope that helps...remember to leave an upvote


convertView.setVisibility(View.GONE) should do the trick.

@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);
        }

참고URL : https://stackoverflow.com/questions/4145090/expandablelistview-hide-indicator-for-groups-with-no-children

반응형