您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇文章为大家展示了怎么在android中实现列表的展开和收缩功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
ActivityMain .java
package com.android; import android.app.ExpandableListActivity; import android.os.Bundle; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.TextView; import android.widget.Toast; import android.widget.ExpandableListView.ExpandableListContextMenuInfo; public class ActivityMain extends ExpandableListActivity { private ExpandableListAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setTitle("ExpandableList"); mAdapter = new MyExpandableListAdapter(this); setListAdapter(mAdapter); registerForContextMenu(this.getExpandableListView()); } //为列表的每一项创建上下文菜单(即长按后 呼出的菜单) @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("ContexMenu"); menu.add(0,0,0,"ContextMenu"); } //单击上下文菜单后的逻辑 @Override public boolean onContextItemSelected(MenuItem item) { ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo)item.getMenuInfo(); String title = ((TextView) info.targetView).getText().toString(); int type =ExpandableListView.getPackedPositionType(info.packedPosition); if(type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition); int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition); Toast.makeText(this, title+"-Group Index"+groupPos+"Child Index:"+childPos, Toast.LENGTH_SHORT).show(); return true; } return false; }
MyExpandableListAdapter.java
package com.android; import android.content.Context; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; public class MyExpandableListAdapter extends BaseExpandableListAdapter { private Context mContext; //父列表数据 private String[] groups ={"group1","group2","group3","group4",""}; //子列表数据 private String [][] children ={ {"child1"}, {"child1","child2"}, {"child1","child2","child3"}, {"child1","child2","child3","child4"}, }; MyExpandableListAdapter(Context context){ mContext = context; } @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub return children[groupPosition][childPosition]; } @Override public long getChildId(int groupPosition, int childPosition) { // TODO Auto-generated method stub return childPosition; } //取子列表中的某一项的view @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = getGenericView();; textView.setText(getChild(groupPosition, childPosition).toString()); return textView; } @Override public int getChildrenCount(int groupPosition) { // TODO Auto-generated method stub return children[groupPosition].length; } @Override public Object getGroup(int groupPosition) { return groups[groupPosition]; } @Override public int getGroupCount() { // TODO Auto-generated method stub return groups.length; } @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) { TextView textView = getGenericView(); textView.setText(getGroup(groupPosition).toString()); return textView; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return true; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { // TODO Auto-generated method stub return true; } //获取某一项的view的逻辑 private TextView getGenericView(){ AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,48); TextView textView = new TextView(mContext); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textView.setPadding(32, 0, 0, 0); return textView; } }
Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。
上述内容就是怎么在android中实现列表的展开和收缩功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。