【问题标题】:Changing ListView to ExpandableListView将 ListView 更改为 ExpandableListView
【发布时间】:2013-08-29 11:54:53
【问题描述】:

我的活动中有一个ListView,但现在我想将其更改为ExpandableListView。我需要完全更改我的列表视图还是可以将 expandableListView 添加到我现有的列表视图中?

【问题讨论】:

  • 可扩展列表视图不过是列表视图本身,但您还需要更改视图及其适配器。
  • @Sameer:但是我该怎么做呢?
  • @Spackplug:谷歌上有很多例子。我也发布了我自己的例子来回答你的问题。请检查一下。

标签: android android-layout listview expandablelistview


【解决方案1】:

这是我使用的示例..您可以更改并使用它。

// 列表视图代码

public class FevList extends ExpandableListActivity {
        String title;
        String url;
        SQLiteDatabase database;
        DbUtil db;
        HashMap<String, String> map = new HashMap<String, String>();
        ArrayList<HashMap<String, String>> subjectList = new ArrayList<HashMap<String, String>>();


        @SuppressWarnings("unchecked")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            try {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.exlist);
                db = new DbUtil();
                database = db.openConnection(this);
                // Thread for getting values of title from DataBase.
                SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
                        this, createGroupList(), R.layout.group_row,
                        new String[] { "subject" }, new int[] { R.id.row_name },
                        createChildList(), R.layout.child_row,
                        new String[] { "title" }, new int[] { R.id.grp_child });
                setListAdapter(expListAdapter);
                registerForContextMenu(getExpandableListView());
            } catch (Exception e) {
                e.printStackTrace();
            }

        }



 // code for adapter



/* Creating the Hashmap for the row */
                ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();

                @SuppressWarnings("unchecked")
                private List createGroupList() {

    // write your code here.

                    return (List) result;

                }

                /* creatin the HashMap for the children */

                @SuppressWarnings("unchecked")
                private List createChildList() {

    // write your code here.
                    return result;
                }

                public void onContentChanged() {

                    System.out.println("onContentChanged");

                    super.onContentChanged();

                }

                /* This function is called on each child click */

                public boolean onChildClick(ExpandableListView parent, View v,
                        int groupPosition, int childPosition, long id) {

    // write your code here.

                    return true;

                }

                /* This function is called on expansion of the group */

                public void onGroupExpand(int groupPosition) {

                    try {

                        System.out.println("Group exapanding Listener => groupPosition = "
                                + groupPosition);

                    } catch (Exception e) {

                        System.out.println(" groupPosition Errrr +++ " + e.getMessage());

                    }

                }
            }

【讨论】:

    【解决方案2】:

    试试这样的:

    public class PharmaciesListScreen extends ExpandableListActivity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ExpandableListView expandbleLis = getExpandableListView();
    
            List<RegionItem> regionsList = new RegionsDBUtils(getApplication())
                    .getAllRegions();
    
            PharmaciesListAdapter mNewAdapter = new PharmaciesListAdapter(regionsList);
            mNewAdapter
                    .setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
            getExpandableListView().setAdapter(mNewAdapter);
        }
    

    适配器代码:

    public class PharmaciesListAdapter extends BaseExpandableListAdapter {
    
     @Override
    public View getChildView(int groupPosition, final int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
           //set up here you child elements.
           //you can use inflater here
        }
    
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = minflater.inflate(R.layout.grouprow, null);
            }
            ((CheckedTextView) convertView).setText(mRegionsList.get(groupPosition).region);
            ((CheckedTextView) convertView).setChecked(isExpanded);
            return convertView;
        }
    
    public void setInflater(LayoutInflater mInflater, Activity act) {
        this.minflater = mInflater;
        activity = act;
    }
    }
    

    注意:在适配器中,您需要实现更多方法。 另外,看看这个帖子: Android ExpandableListView - Looking for a tutorial

    【讨论】:

      【解决方案3】:

      "Here is a pretty good example for ExpandableListView"

      ExpandableListView 继承自 ListView。它向 ListView 实现添加了更多内容,例如扩展和折叠属性、标题和子视图等。从 listView 移动到可扩展列表视图时,您需要在以下方面进行更改:

      1. XML 视图
      2. 实现 ExpandableListAdapter 的适配器
      3. 在设置适配器时,您需要新建 ExpandableListAdapter(此,每个列表的 DataHeader 列表,您要在每个标题下填充的 DataChild 列表);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多