android listview实现目录返回,android(1) listView二级目录选中效果

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

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

先上效果图:

0818b9ca8b590ca3270a3433284dd417.png                 

0818b9ca8b590ca3270a3433284dd417.png

主界面:

public class MainActivity extends Activity {

// 树形Listview显示类别

private ExpandableListView listview1;

// 一级

private List groups;

// 二级

private List> 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 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 getListData() {

List list = new ArrayList();

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 childs = new ArrayList();

}

List treeNodes = new ArrayList();

Context parentContext;

public ListViewAdapter(Context view, int myPaddingLeft) {

parentContext = view;

this.myPaddingLeft = myPaddingLeft;

}

public List GetTreeNode() {

return treeNodes;

}

public void UpdateTreeNode(List 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:

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" >

android:id="@+id/listview1"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:choiceMode="singleChoice" />

drawable/listview_item_bg.xml:

color.xml:

@android:color/background_dark

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值