怎么在Android中实现一个ListView点击展开收起效果
怎么在Android中实现一个ListView点击展开收起效果?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
创新互联是一家集网站建设,五大连池企业网站建设,五大连池品牌网站建设,网站定制,五大连池网站建设报价,网络营销,网络优化,五大连池网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
布局文件:
然后就是具体实现:
这里主要是添加几个必须的属性 大多数方法不用重写
参考我代码中的位置稍加改动就行
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建一个ExpandableListAdapter对象 final ExpandableListAdapter adapter = new ExpandableListAdapter() { int[] logos = new int[]{ R.drawable.human1st, R.drawable.human1st, R.drawable.human2nd, R.drawable.human3rd }; private String[] humanTypes = new String[]{ "不是人","聪明人","普通人","我这样的人" }; private String[][] humans = new String[][]{ {"上仙","大神","荷兰猪"}, {"超人","一般聪明人","假的聪明人"}, {"努力的人","快乐的普通人","苦逼的普通人"}, {"天才","傻逼","蠢萌"} }; //获得制定组的位置、指定子列表项处的字列表项数据 private TextView getTextView(){ AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,164); TextView textView = new TextView(MainActivity.this); textView.setLayoutParams(layoutParams); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textView.setPadding(36,0,0,0); textView.setTextSize(30); return textView; } @Override public void registerDataSetObserver(DataSetObserver observer) { } @Override public void unregisterDataSetObserver(DataSetObserver observer) { } @Override public int getGroupCount() { return humanTypes.length; } @Override public int getChildrenCount(int groupPosition) { return humans[groupPosition].length; } //获取制定组位置处的组数据 @Override public Object getGroup(int groupPosition) { return humanTypes[groupPosition]; } @Override public Object getChild(int groupPosition, int childPosition) { return humans[groupPosition][childPosition]; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } //该方法决定每个组选项的外观 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout linearLayout = new LinearLayout(MainActivity.this); linearLayout.setOrientation(LinearLayout.HORIZONTAL); ImageView logo = new ImageView(MainActivity.this); logo.setImageResource(logos[groupPosition]); linearLayout.addView(logo); TextView textView = getTextView(); textView.setText(getGroup(groupPosition).toString()); linearLayout.addView(textView); // linearLayout.setMinimumHeight(50); return linearLayout; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = getTextView(); textView.setText(getChild(groupPosition,childPosition).toString()); return textView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } @Override public boolean areAllItemsEnabled() { return false; } @Override public boolean isEmpty() { return false; } @Override public void onGroupExpanded(int groupPosition) { } @Override public void onGroupCollapsed(int groupPosition) { } @Override public long getCombinedChildId(long groupId, long childId) { return 0; } @Override public long getCombinedGroupId(long groupId) { return 0; } }; ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.list); expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { Toast.makeText(MainActivity.this,"你是一个:" + adapter.getChild(groupPosition,childPosition),Toast.LENGTH_SHORT).show(); return true; } }); expandableListView.setAdapter(adapter); } }
看完上述内容,你们掌握怎么在Android中实现一个ListView点击展开收起效果的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
网页标题:怎么在Android中实现一个ListView点击展开收起效果
URL链接:http://myzitong.com/article/gocpgc.html