时间:2021-05-20
最近在做一个Rss阅读器,我看了一看别人做的阅读器中的lisView可以伸缩,展开,我就在网上搜索了一下。果然让我找到,下面就我找到的一个小例子,给大家分享一下。
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; } }运行的结果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了jQuery实现DIV层收缩展开的方法。分享给大家供大家参考。具体实现方法如下:复制代码代码如下:jQuery实现DIV层的收缩展开效果/*收缩展
vuejs通过css3实现元素固定高度到auto高度的动画和auto高度到固定高度的动画。循环列表,html:{{item.content}}【展开】【收缩】在
本文实例讲述了JS实现固定在右下角可展开收缩DIV层的方法。分享给大家供大家参考。具体实现方法如下:复制代码代码如下:JS实现固定在右下角可展开收缩的DIV层仅
本文实例讲述了jquery简单实现网页层的展开与收缩效果。分享给大家供大家参考。具体如下:这里演示了jquery网页层展开、层收缩代码,带缓冲动画效果,点击指定
今天给大家讲讲android的目录实现方法,就像大家看到的小说目录一样,android提供了ExpandableListView控件可以实现二级列表展示效果,现