类似于Android中的树状视图的可扩展ListView

kittu88

我想在android中创建树视图。我试图实现带有复选框的可扩展列表视图。我想,当单组复选框被选中,然后在组内的所有列表项将被选中。用户可能能够选择列表视图中的特定项目。

请看图片:

所有项目均已折叠,已选择2013-2014!

选择的特定月份

2011-2012年入选

我应该如何在android中做到这一点?我面临的问题是,如果在可扩展列表视图的组中添加一个复选框,该列表将无法再扩展。

源代码

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#f4f4f4" >

            <ExpandableListView
                android:id="@+id/lvExp"
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:cacheColorHint="#00000000"/>   

</LinearLayout>

list_group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp"
    android:background="#CC7A00">

    <CheckBox
        android:id="@+id/lblListHeaderCheckbox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="" 
        android:textSize="17dp"
        android:textColor="#ffffff"/>

    <TextView
        android:id="@+id/lblListHeader"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textSize="17dp"
        android:textColor="#ffffff" />

</LinearLayout>

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="55dip"
    android:orientation="vertical" 
    android:background="#FFCC80">

    <TextView
        android:id="@+id/lblListItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="17dip"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" 
        android:textColor="#336699"/>

</LinearLayout>

MainActivity

public class MainActivity extends Activity {

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // get the listview
        expListView = (ExpandableListView) findViewById(R.id.lvExp);

        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);

        // Listview Group click listener
        expListView.setOnGroupClickListener(new OnGroupClickListener() {

            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {
                // Toast.makeText(getApplicationContext(),
                // "Group Clicked " + listDataHeader.get(groupPosition),
                // Toast.LENGTH_SHORT).show();
                return false;
            }
        });

        // Listview Group expanded listener
        expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Expanded",
                        Toast.LENGTH_SHORT).show();
            }
        });

        // Listview Group collasped listener
        expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

            @Override
            public void onGroupCollapse(int groupPosition) {
                Toast.makeText(getApplicationContext(),
                        listDataHeader.get(groupPosition) + " Collapsed",
                        Toast.LENGTH_SHORT).show();

            }
        });

        // Listview on child click listener
        expListView.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                // TODO Auto-generated method stub
                Toast.makeText(
                        getApplicationContext(),
                        listDataHeader.get(groupPosition)
                                + " : "
                                + listDataChild.get(
                                        listDataHeader.get(groupPosition)).get(
                                        childPosition), Toast.LENGTH_SHORT)
                        .show();
                return false;
            }
        });
    }

    /*
     * Preparing the list data
     */
    private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<String>>();

for (int i=0; i<=10; i++)
        {
            listDataHeader.add("Header"+ i);

            // Adding child data
            List<String> child = new ArrayList<String>();
            child.add("Child"+i);
            child.add("Child"+i);

            listDataChild.put(listDataHeader.get(i), child); // Header, Child data

        }
    }

}

ExpandableListAdapter

public class ExpandableListAdapter 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 ExpandableListAdapter(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 childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        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.list_item, null);
        }

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

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        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.list_group, null);
        }

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

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

应该如何达到要求?

马可·比斯卡洛(Marco Biscaro)

尝试将其添加到您的Checkbox(在list_group.xml文件中):

android:focusable="false"

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android ProgressBar的样式类似于SwipeRefreshLayout中的进度视图

来自分类Dev

ListView类似于Android中的Google Play设置活动

来自分类Dev

创建类似于iOS子视图的Android子视图

来自分类Dev

类似于Android中的StorageItemAccessList功能

来自分类Dev

类似于C ++中Android的ArrayMap的容器

来自分类Dev

类似于 android 中的 PageView 的元素

来自分类Dev

如何通过添加类似于++的函数在TypeScript中扩展Number

来自分类Dev

Android List视图布局类似于Google Play

来自分类Dev

hasStableIds()在可扩展ListView中?

来自分类Dev

在 CardView 中显示多个项目。类似于 ListView 或 GridView

来自分类Dev

Android:可扩展列表视图实现

来自分类Dev

Android使用可扩展列表视图

来自分类Dev

如何将JavaFX ListView的行为更改为类似于Android ListView的行为

来自分类Dev

使软键盘的行为类似于Android中的IOS

来自分类Dev

显示类似于Android中的本机来电屏幕的屏幕

来自分类Dev

是否有类似于Android中的stringByAppendingPathComponent的东西?

来自分类Dev

在Android中显示类似于菜单警报的警报

来自分类Dev

如何更改类似于Android中的图像的相对布局形状

来自分类Dev

如何在android中创建类似于fb的事件表单

来自分类Dev

从可扩展 ListView 中的 Child 获取值

来自分类Dev

是否可以在可扩展ListView的水平显示子视图?

来自分类Dev

类似于TreeView Android的ExpandableListview

来自分类Dev

Android-可扩展Listview SetIndicatorBounds在Kitkat Android-4.4中不起作用

来自分类Dev

可扩展列表视图在Android中的片段活动中不起作用

来自分类Dev

可扩展列表视图在Android中的片段活动中不起作用

来自分类Dev

集合视图中的类似于Tableview的动画?

来自分类Dev

类似于存折的下拉视图类

来自分类Dev

Fiddler中类似于“ <>”的图标

来自分类Dev

在WatchKit中类似于UIProgressView

Related 相关文章

  1. 1

    Android ProgressBar的样式类似于SwipeRefreshLayout中的进度视图

  2. 2

    ListView类似于Android中的Google Play设置活动

  3. 3

    创建类似于iOS子视图的Android子视图

  4. 4

    类似于Android中的StorageItemAccessList功能

  5. 5

    类似于C ++中Android的ArrayMap的容器

  6. 6

    类似于 android 中的 PageView 的元素

  7. 7

    如何通过添加类似于++的函数在TypeScript中扩展Number

  8. 8

    Android List视图布局类似于Google Play

  9. 9

    hasStableIds()在可扩展ListView中?

  10. 10

    在 CardView 中显示多个项目。类似于 ListView 或 GridView

  11. 11

    Android:可扩展列表视图实现

  12. 12

    Android使用可扩展列表视图

  13. 13

    如何将JavaFX ListView的行为更改为类似于Android ListView的行为

  14. 14

    使软键盘的行为类似于Android中的IOS

  15. 15

    显示类似于Android中的本机来电屏幕的屏幕

  16. 16

    是否有类似于Android中的stringByAppendingPathComponent的东西?

  17. 17

    在Android中显示类似于菜单警报的警报

  18. 18

    如何更改类似于Android中的图像的相对布局形状

  19. 19

    如何在android中创建类似于fb的事件表单

  20. 20

    从可扩展 ListView 中的 Child 获取值

  21. 21

    是否可以在可扩展ListView的水平显示子视图?

  22. 22

    类似于TreeView Android的ExpandableListview

  23. 23

    Android-可扩展Listview SetIndicatorBounds在Kitkat Android-4.4中不起作用

  24. 24

    可扩展列表视图在Android中的片段活动中不起作用

  25. 25

    可扩展列表视图在Android中的片段活动中不起作用

  26. 26

    集合视图中的类似于Tableview的动画?

  27. 27

    类似于存折的下拉视图类

  28. 28

    Fiddler中类似于“ <>”的图标

  29. 29

    在WatchKit中类似于UIProgressView

热门标签

归档