【问题标题】:Android,want to show Expandible listview in a tabAndroid,想在选项卡中显示可扩展列表视图
【发布时间】:2014-02-24 10:23:09
【问题描述】:

我想在我的应用程序中实现选项卡。标签效果很好。现在在其中一个选项卡中实现可扩展列表视图...这意味着活动扩展了 Fragment 而不是 FragmentActivity...但是我在可扩展列表视图中遇到了一个问题...我该如何克服这个...这是我的代码..

public class ExpandableListView extends BaseExpandableListAdapter {
        private Context _context;
        private List<String> _listDataHeader; // header titles
        // child data in format of header title, child title
        private HashMap<String, List<String>> _listDataChild;


        public ExpandableListView(Context context, List<String> listDataHeader,
                HashMap<String, List<String>> listChildData) {
            this._context = context;
            this._listDataHeader = listDataHeader;
            this._listDataChild = listChildData;
        }
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
         final String childText = (String) getChild(groupPosition, childPosition);
         if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.child_battery, null);
            }

            TextView txtListChild = (TextView) convertView
                    .findViewById(R.id.battery);

            txtListChild.setText(childText);
            return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.parent, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }


}

主要课程是......

public class Setting extends Fragment{

    ExpandableListView listAdapter;
    ExpandableListView expListView;
    List<String> listDataHeader;
    HashMap<String, List<String>> listDataChild;

    //private LayoutInflater context;

      @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

         //// context =inflater;

            View rootView = inflater.inflate(R.layout.setting, container, false);


            expListView = (ExpandableListView)rootView. findViewById(R.id.lvExp);

            // preparing list data
            prepareListData();

            listAdapter = new ExpandableListView(getActivity().getApplicationContext());

            // setting list adapter
            expListView.setAdapter((ListAdapter) listAdapter);
            return rootView;

        }

    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

        // Adding child data
        listDataHeader.add("Top 250");
        listDataHeader.add("Now Showing");
        listDataHeader.add("Coming Soon..");

        // Adding child data
        List<String> top250 = new ArrayList<String>();
        top250.add("Remaining Battery");
        top250.add("Set Alarm Distance");
        top250.add("Pick Alarm Tone");
        top250.add("Set Vibrate");

        List<String> nowShowing = new ArrayList<String>();
        nowShowing.add("Remaining Battery");
        nowShowing.add("Set Alarm Distance");
        nowShowing.add("Pick Alarm Tone");
        nowShowing.add("Set Vibrate");


        List<String> comingSoon = new ArrayList<String>();
        comingSoon.add("2 Guns");
        comingSoon.add("The Smurfs 2");
        comingSoon.add("The Spectacular Now");
        comingSoon.add("The Canyons");
        comingSoon.add("Europa Report");

        listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
        listDataChild.put(listDataHeader.get(1), nowShowing);
        listDataChild.put(listDataHeader.get(2), comingSoon);
    }


    }

错误是构造函数“构造函数 ExpandableListView(Context, List, HashMap>) 未定义”...

【问题讨论】:

标签: java android android-fragments expandablelistview


【解决方案1】:

这实际上与您使用标签的事实无关。

您有两个名为 ExpandableListView 的类,并且似乎在您的 Settings 类中导入了错误的类。

为了避免这种情况,我建议为您的 Adapter 选择一个不同的名称,这样您和编译器就不会混淆。

或者,您可以更改Settings 顶部的导入以包含您自己的ExpandableListView 类,并将实际小部件的外观更改为android.widget.ExpandableListView

【讨论】:

    【解决方案2】:

    你可以试试这个

    查看此链接以获取详细答案Add ExpandableListView Under TabHost

    此外,如果您想在选项卡小部件本身中添加列表,那么您可以尝试此链接 Add ExpandableListView to a tab with other elements

    【讨论】:

      猜你喜欢
      • 2018-02-16
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多