«

android listView二级目录选中效果

时间:2024-3-2 17:42     作者:韩俊     分类: Android


一.listView的二级目录且选中实现:

记录下来,以便以后可能会用到,直接上贴源码:

先上效果图:


主界面:

public class MainActivity extends Activity {

    // 树形Listview显示类别
    private ExpandableListView listview1;
    // 一级
    private List<String> groups;
    // 二级
    private List<List<String>> child;
    // 适配器
    private ListViewAdapter mAdapter;
    //记录点击的节点位置,初始为-1
    public static int children_item = -1;
    public static int parent_item = -1;
    //记录上一次点击的item
    private View view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview1 = (ExpandableListView) findViewById(R.id.listview1);

        mAdapter = new ListViewAdapter(this, ListViewAdapter.PaddingLeft >> 1);

        groups = getListData();
        //一级目录
        child = new ArrayList<List<String>>();
        List<ListViewAdapter.TreeNode>  treeNode = mAdapter.GetTreeNode();
        //为每一个一级目录下添加内容
        for (int i = 0; i < groups.size(); i++) {
            ListViewAdapter.TreeNode node = new ListViewAdapter.TreeNode();
            node.parent = groups.get(i);
            child.add(i,getListData());
            for (int ii = 0; ii < child.get(i).size(); ii++) {
                node.childs.add(child.get(i).get(ii));
            }
            treeNode.add(node);
        }
        mAdapter.UpdateTreeNode(treeNode);
        listview1.setAdapter(mAdapter);
        listview1.setOnChildClickListener(new OnChildClickListener() {

            @Override
            public boolean onChildClick(ExpandableListView arg0,
                    View arg1, int parent, int children, long arg4) {
                //当前item变色
                arg1.setBackgroundResource(R.color.color1);
                //上一个item还原
                if (parent_item != -1
                        && children_item != -1
                        && (children_item != children || parent_item != parent)) {
                    view.setBackgroundResource(R.drawable.listview_item_bg);
                }
                view = arg1;
                children_item = children;
                parent_item = parent;
                return false;
            }
        });
    }

    public List<String> getListData() {

        List<String> list = new ArrayList<String>();
        list.add("语文");
        list.add("数学");
        list.add("英语");
        list.add("历史");
        list.add("毛概");
        list.add("自然");

        return list;
    }

}
适配器:

public class ListViewAdapter extends BaseExpandableListAdapter {
    public static final int ItemHeight = 72;// 每项的高度
    public static final int PaddingLeft = 48;// 每项的高度
    private int myPaddingLeft = 0;

    static public class TreeNode {
        public String parent;
        public List<String> childs = new ArrayList<String>();
    }

    List<TreeNode> treeNodes = new ArrayList<TreeNode>();
    Context parentContext;

    public ListViewAdapter(Context view, int myPaddingLeft) {
        parentContext = view;
        this.myPaddingLeft = myPaddingLeft;
    }

    public List<TreeNode> GetTreeNode() {
        return treeNodes;
    }

    public void UpdateTreeNode(List<TreeNode> nodes) {
        treeNodes = nodes;
    }

    public void RemoveAll() {
        treeNodes.clear();
    }

    public Object getChild(int groupPosition, int childPosition) {
        return treeNodes.get(groupPosition).childs.get(childPosition)
                .toString();
    }

    public int getChildrenCount(int groupPosition) {
        return treeNodes.get(groupPosition).childs.size();
    }

    static public TextView getTextView(Context context) {
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, ItemHeight);
        TextView textView = new TextView(context);
        textView.setLayoutParams(lp);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setTextSize(15);
        return textView;
    }

    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        TextView textView = getTextView(this.parentContext);
        textView.setText(getChild(groupPosition, childPosition).toString());
        textView.setPadding(myPaddingLeft + PaddingLeft, 0, 0, 0);
        textView.setTextSize(15);
        return textView;
    }

    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        //不会选中丢失
        if (MainActivity.children_item != -1) {
            if (MainActivity.children_item == groupPosition) {
                convertView
                        .setBackgroundResource(R.color.color1);
            } else {
                convertView
                        .setBackgroundResource(R.drawable.listview_item_bg);
            }
        }
        TextView textView = getTextView(this.parentContext);
        textView.setText(getGroup(groupPosition).toString());
        textView.setPadding(myPaddingLeft + (PaddingLeft >> 1), 0, 0, 0);
        return textView;
    }

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

    public Object getGroup(int groupPosition) {
        return treeNodes.get(groupPosition).parent.toString();
    }

    public int getGroupCount() {
        return treeNodes.size();
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

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

    public boolean hasStableIds() {
        return true;
    }

}

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/background_light"
    android:orientation="vertical" >
   <ExpandableListView
            android:id="@+id/listview1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:choiceMode="singleChoice" />
</LinearLayout>

drawable/listview_item_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/background_dark" android:state_selected="true"></item>
    <item android:drawable="@android:color/background_dark" android:state_pressed="true"></item>
    <item android:drawable="@android:color/background_dark" android:state_focused="true"></item>
    <item android:drawable="@android:color/transparent"></item>
</selector>

color.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="color1">@android:color/background_dark</color>
</resources>


标签: android

热门推荐