0

J'ai un fragment qui représente un onglet de mon projet. Ce fragment héberge un ExpandableListAdapater. La liste extensible est simplement un enfant TextView par groupe qui affiche certaines informations.Comment effondrement groupe actuel ExpandableListView

Ce que je suis en train de réaliser est de mettre en œuvre un double tap sur la TextView qui est développée. En tapant deux fois sur le TextView, il va réduire ce groupe.

J'ai essayé de réduire le groupe à l'intérieur de l'adaptateur, mais il obtient un SO pour une raison quelconque. Je sais que je dois implémenter un GestureDetector mais je n'arrive pas à l'implémenter dans l'adaptateur ou même le fragment.

Mon fragment:

public class FragmentProgramInfo extends Fragment 
{ 
View view; 
ExpandableListView expandableListView; 
ExpandableListAdapterProgramInfo expandableListAdapterProgramInfo; 

public FragmentProgramInfo() 
    {} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
    view = inflater.inflate(R.layout.fragment_program_info, container, false); 
    return view; 
    } 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) 
    { 
    super.onViewCreated(view, savedInstanceState); 
    expandableListAdapterProgramInfo = new ExpandableListAdapterProgramInfo(getActivity()); 
    expandableListView = (ExpandableListView) view.findViewById(R.id.ELV_program_info_list); 
    expandableListView.setAdapter(expandableListAdapterProgramInfo); 
    } 
} 

Mon Adapater:

public class ExpandableListAdapterProgramInfo extends BaseExpandableListAdapter 
{ 
Activity context; 
View masterView; 
private String[] listGroups; 
private String[][] listChilds; 

public ExpandableListAdapterProgramInfo(Activity context) 
    { 
    this.context = context; 
    interfaceExpandableListAdapterProgramInfo = (InterfaceExpandableListAdapterProgramInfo) context; 
    masterView = context.getWindow().getDecorView().findViewById(android.R.id.content); 

    /// Intitialize strings of listGroups and listChilds 
    } 

@Override 
public View getGroupView(final int listGroupPosition, boolean isExpanded, View view, ViewGroup viewGroup) 
    { 
    LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    if(view == null) 
     view = layoutInflater.inflate(R.layout.list_group_program_info, viewGroup, false); 
    TextView tvListGroupProgramInfo = (TextView) view.findViewById(R.id.TV_list_group_program_info); 
    tvListGroupProgramInfo.setText(getGroup(listGroupPosition).toString()); 
    return view; 
    } 

@Override 
public View getChildView(final int listGroupPosition, final int listChildPosition, boolean isLastChild, 
         View view, ViewGroup viewGroup) 
    { 
    LayoutInflater inflater = context.getLayoutInflater(); 
    view = inflater.inflate(R.layout.list_child_program_info, viewGroup, false); 
    TextView tvListChildProgramInfo = (TextView) view.findViewById(R.id.TV_list_child_program_info); 
    tvListChildProgramInfo.setText(listChilds[listGroupPosition][listChildPosition]); 
    return view; 
    } 

/// Override other methods of ELV 
} 

Répondre

0

Au début, vous devez détecter la dernière position sélectionnée Vous pouvez essayer quelque chose comme ça

private int lastExpandedPosition = -1;// last index selected 
private ExpandableListAdapterProgramInfo expandableListView; //your expandable listview 
... 

expandableListView.setOnGroupExpandListener(new OnGroupExpandListener() { 

    @Override 
    public void onGroupExpand(int groupPosition) { 
      if (lastExpandedPosition != -1 
        && groupPosition != lastExpandedPosition) { 
       expandableListView.collapseGroup(lastExpandedPosition); 
      } 
      lastExpandedPosition = groupPosition; 
    } 
}); 
+0

Je ne sais pas comment cela aide. – Samuel

0

Essayez ceci:

@Override 
public void onGroupExpanded(int groupPosition){ 
    //collapse the old expanded group, if not the same 
    //as new group to expand 
    if(groupPosition != lastExpandedGroupPosition){ 
     listView.collapseGroup(lastExpandedGroupPosition); 
    } 

    super.onGroupExpanded(groupPosition);   
    lastExpandedGroupPosition = groupPosition; 
} 

Hope it helps! Bonne chance!