0

我有一段代表我的項目的一個選項卡。該片段承載ExpandableListAdapater。可擴展列表只是每個組中顯示一些信息的一個TextView子組。如何在ExpandableListView中摺疊當前組

我想要實現的是在展開的TextView上實現雙擊。在雙擊TextView時,它將摺疊該組。

我試圖摺疊適配器內部的組,但它由於某種原因得到了SO。我知道我必須實現一個GestureDetector,但我似乎無法在適配器甚至片段中實現它。

我的片段:

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

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

回答

0

首先,你必須檢測最後選定的位置 你可以嘗試這樣的

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

我不知道這是如何幫助。 – Samuel

0

嘗試一些東西這個:

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

希望它有幫助!祝你好運!

相關問題