Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 941|回复: 0

Android BaseExpandableListAdapter代码 ,教你做出超炫效果! - Andr

[复制链接]

该用户从未签到

发表于 2011-10-26 16:56:00 | 显示全部楼层 |阅读模式

羡慕别人做出的手机炫效果吗?我们也来试下吧!
         java代码 部分:       import java.util.List;  import android.content.Context;
  import android.graphics.drawable.Drawable;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseExpandableListAdapter;
  import android.widget.CheckBox;
  import android.widget.ImageView;
  import android.widget.TextView;
  import com.iwidsets.clear.manager.R;
  import com.iwidsets.clear.manager.adapter.BrowserInfo;
  public
  class ClearExpandableListAdapter extends BaseExpandableListAdapter {
  class ExpandableListHolder {
  ImageView appIcon;
  TextView appInfo;
  CheckBox appCheckBox;
  }
  private Context context;
  private LayoutInflater mChildInflater;
  private LayoutInflater mGroupInflater;
  private List group;
  public ClearExpandableListAdapter(Context c, List group) {
  this.context = c;
  mChildInflater = (LayoutInflater) context
  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  mGroupInflater = (LayoutInflater) context
  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  this.group = group;
  }
  public Object getChild(int childPosition, int itemPosition) {
  return group.get(childPosition).getChild(itemPosition);
  }
  public
  long getChildId(int childPosition, int itemPosition) {
  return itemPosition;
  }
  @Override
  public
  int getChildrenCount(int index) {
  return group.get(index).getChildSize();
  }
  public Object getGroup(int index) {
  return group.get(index);
  }
  public
  int getGroupCount() {
  return group.size();
  }
  public
  long getGroupId(int index) {
  return index;
  }
  public View getGroupView(int position, boolean flag, View view,
  ViewGroup parent) {
  ExpandableListHolder holder = null;
  if (view == null) {
  view = mGroupInflater.inflate(
  R.layout.browser_expandable_list_item, null);
  holder = new ExpandableListHolder();
  holder.appIcon = (ImageView) view.findViewById(R.id.app_icon);
  view.setTag(holder);
  holder.appInfo = (TextView) view.findViewById(R.id.app_info);
  holder.appCheckBox = (CheckBox) view
  .findViewById(R.id.app_checkbox);
  } else {
  holder = (ExpandableListHolder) view.getTag();
  }
  GroupInfo info = this.group.get(position);
  if (info != null) {
  holder.appInfo.setText(info.getBrowserInfo().getAppInfoId());
  Drawable draw = this.context.getResources().getDrawable(
  info.getBrowserInfo().getImageId());
  holder.appIcon.setImageDrawable(draw);
  holder.appCheckBox.setChecked(info.getBrowserInfo().isChecked());
  }
  return view;
  }
  public
  boolean hasStableIds() {
  return
  false;
  }
  public
  boolean isChildSelectable(int arg0, int arg1) {
  return
  false;
  }
  @Override
  public View getChildView(int groupPosition, int childPosition,
  boolean isLastChild, View convertView, ViewGroup parent) {
  ExpandableListHolder holder = null;
  if (convertView == null) {
  convertView = mChildInflater.inflate(
  R.layout.browser_expandable_list_item, null);
  holder = new ExpandableListHolder();
  holder.appIcon = (ImageView) convertView
  .findViewById(R.id.app_icon);
  convertView.setTag(holder);
  holder.appInfo = (TextView) convertView.findViewById(R.id.app_info);
  holder.appCheckBox = (CheckBox) convertView
  .findViewById(R.id.app_checkbox);
  } else {
  holder = (ExpandableListHolder) convertView.getTag();
  }
  BrowserInfo info = this.group.get(groupPosition)
  .getChild(childPosition);
  if (info != null) {
  holder.appInfo.setText(info.getAppInfoId());
  Drawable draw = this.context.getResources().getDrawable(
  info.getImageId());
  holder.appIcon.setImageDrawable(draw);
  holder.appCheckBox.setChecked(info.isChecked());
  }
  return convertView;
  }
  }

  要想让child获得焦点,只在改
  Java代码
  public
  boolean isChildSelectable(int arg0, int arg1) {
  return
  true;
  }
  public boolean isChildSelectable(int arg0, int arg1) { return true; }
  browser_expandable_list_item.xml 用于显示每一个ITEM的XML
  Xml代码 部分

  version="1.0"
  encoding="utf-8"?>
  <linearlayout </linearlayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  androidrientation="horizontal"
  android:minHeight="40px"
  android:layout_gravity="center_vertical">
  <checkbox </checkbox
  android:id="@+id/app_checkbox"
  android:focusable="false"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginLeft="35px"
  android:checked="true"/>
  <imageview </imageview
  android:id="@+id/app_icon"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_vertical"
  />
  <textview </textview
  android:id="@+id/app_info"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textColor="?android:attr/textColorPrimary"
  android:paddingLeft="3px"
  android:layout_gravity="center_vertical"
  />

  
  browserlayout.xml 用于显示整个界面
  Xml代码

  version="1.0"
  encoding="utf-8"?>
  <linearlayout </linearlayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  androidrientation="vertical">
  <expandablelistview </expandablelistview
  android:id="@+id/appList"
  android:layout_width="fill_parent"
  android:layout_height="0dip"
  android:layout_weight="1"
  />
  <linearlayout </linearlayout
  android:layout_height="wrap_content"
  android:layout_width="fill_parent"
  android:paddingLeft="4dip"
  android:paddingRight="4dip"
  android:paddingBottom="1dip"
  android:paddingTop="5dip"
  android:background="@android:drawable/bottom_bar"
  android:id="@+id/app_footer">
  <button </button
  android:layout_weight="1"
  android:id="@+id/btn_export"
  android:layout_width="0dip"
  android:layout_height="fill_parent"
  android:text="@string/clear">
  <button </button
  android:layout_weight="1"
  android:id="@+id/btn_sel_all"
  android:layout_width="0dip"
  android:layout_height="fill_parent"
  android:text="select_all">
  <button </button
  android:layout_weight="1"
  android:id="@+id/btn_desel_all"
  android:layout_width="0dip"
  android:layout_height="fill_parent"
  android:text="deselect_all">



  BrowserInfo用于提供一个项的信息
  Java代码
  public
  class BrowserInfo {
  private
  int appInfoId;
  private
  int imageId;
  private
  boolean checked;
  public BrowserInfo(int appInfoId, int imageId, boolean checked) {
  this.appInfoId = appInfoId;
  this.imageId = imageId;
  this.checked = checked;
  }
  public
  boolean isChecked() {
  return checked;
  }
  public
  void setChecked(boolean checked) {
  this.checked = checked;
  }
  public
  int getAppInfoId() {
  return appInfoId;
  }
  public
  void setAppInfoId(int appInfoId) {
  this.appInfoId = appInfoId;
  }
  public
  int getImageId() {
  return imageId;
  }
  public
  void setImageId(int imageId) {
  this.imageId = imageId;
  }
  }

  GroupInfo 用于显示一个组的信息
  Java代码 部分:
  import java.util.List;
  public
  class GroupInfo {
  private BrowserInfo group;
  private List
child;
  public GroupInfo(BrowserInfo group, List
child) {
  this.group = group;
  this.child = child;
  }
  public
  void add(BrowserInfo info){
  child.add(info);
  }
  public
  void remove(BrowserInfo info){
  child.remove(info);
  }
  public
  void remove(int index){
  child.remove(index);
  }
  public
  int getChildSize(){
  return child.size();
  }
  public BrowserInfo getChild(int index){
  return child.get(index);
  }
  public BrowserInfo getBrowserInfo() {
  return group;
  }
  public
  void setBrowserInfo(BrowserInfo group) {
  this.group = group;
  }
  public List
getChild() {
  return child;
  }
  public
  void setChild(List
child) {
  this.child = child;
  }
  }

        最后就是把要显示的内容显示出来的。
  学会了吗?赶快来试试吧!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

GMT+8, 2024-4-29 03:22 , Processed in 0.382692 second(s), 47 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表