我有一个片段代表我项目的一个标签。此片段承载ExpandableListAdapater。可扩展列表只是每组显示一些信息的一个TextView子项。
我想要实现的是在扩展的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);
}
}
我的适配器:
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 :(得分: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;
}
});
答案 1 :(得分: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;
}
希望它有所帮助!祝你好运!