【问题标题】:How to collapse current group in ExpandableListView如何在 ExpandableListView 中折叠当前组
【发布时间】:2016-08-27 02:48:59
【问题描述】:

我有一个片段,它代表我项目的一个选项卡。该片段承载一个 ExpandableListAdapter。可扩展列表只是每个显示一些信息的组一个 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
}

【问题讨论】:

    标签: android expandablelistview expandablelistadapter gesturedetector


    【解决方案1】:

    首先你必须检测最后选择的位置 你可以试试这样的东西

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

    【讨论】:

    • 我不确定这有什么帮助。
    【解决方案2】:

    试试这个:

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

    希望对您有所帮助!祝你好运!

    【讨论】:

      猜你喜欢
      • 2011-08-04
      • 2015-11-18
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      • 2011-05-17
      • 2011-03-28
      • 1970-01-01
      相关资源
      最近更新 更多