欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

android列表控件实现展开、收缩功能

时间:2021-10-17 09:12:02|栏目:Android代码|点击:

最近在做一个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;
 }
 
}

 运行的结果如下:

上一篇:Android实现高德地图显示及定位

栏    目:Android代码

下一篇:Android Service(不和用户交互应用组件)案例分析

本文标题:android列表控件实现展开、收缩功能

本文地址:http://www.codeinn.net/misctech/180208.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有