ListView优化
来源:互联网 发布:火石软件招聘 编辑:程序博客网 时间:2024/05/16 15:04
ListView数据分页展示
- 触发加载更多数据:
- 点击足视图 Button加载更多
- ListView闲置状态 ListView滑动底部
- 必备条件!接口自带分页功能!
- ListView的滚动监听
listView.setOnScrollListener(new OnScrollListener() {
//判断当前状态
//状态改变的收才会调用
/*
* 参数2: scrollState改变后的状态
* 滚动状态
* 抛
* 闲置
* (non-Javadoc)
* @see android.widget.AbsListView.OnScrollListener#onScrollStateChanged(android.widget.AbsListView, int)
*/
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
public static int SCROLL_STATE_IDLE = 0;
public static int SCROLL_STATE_TOUCH_SCROLL = 1;
public static int SCROLL_STATE_FLING = 2;
//滚动到底并且闲置
if (isBottom && scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
//加载下一页数据
currentPager++;
getDataFromHttp();
}
}
//滚动监听 此方法会频繁调用
/**
* firstVisibleItem: 第一条可见的条目
* visibleItemCount: 可见的总条目
* totalItemCount : 总共的条目
*/
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
isBottom= (firstVisibleItem+visibleItemCount) == totalItemCount;
}
});
多套item布局使用
- BaseAdapter需要额外重写两个方法
- getViewTypeCount()//获得条目的种数
- getItemViewType(int position)//对应返回值的类型
- BaseAdapter ViewHolder可能创建多个
- BaseAdapter getView()需要判断类型,在进行赋值和convertView使用!
- 多套item布局 数据源一定要进行分类!
- 定义一个标签!
- 根据类型去区分
ListView常见问题
- 内部事件处理
- 现象: 当Item中包含Button,并且做了监听事件!ListView单条点击事件不生效!
- 原因: Button消费了原本属于Item点击事件!
- 解决方法:
- item的单条布局的最外层 添加一个属性!
- android:descendantFocusability=”blocksDescendants”
- 阻止事件,如果点中单条!那么事件不给子控件使用!
- 嵌套问题
- ScrollView+ListView
- ListView+ListView
- ListView+GridView
- GridView+ListView
- GridView+GridView
- 问题:
- 内部嵌套的ListView或者GridView显示不全!只会显示一行
- 原因:
- 内部的ListView、GridView高不会被完全赋值
- 解决方法:
- 自定义控件或者重写onMeasure()方法
- 控件显示过程:onMeasur()->onLayout()->onDraw()
ExpandableListView
- 继承关系
- ListView – AbsListView – AdapterView
- 属性
- android:childDivider:
- android:childIndicator:
- android:childIndicatorEnd:
- android:childIndicatorLeft:
- android:childIndicatorRight:
- android:childIndicatorStart:
- android:groupIndicator:
- android:indicatorEnd:
- android:indicatorLeft:
- android:indicatorRight:
- android:indicatorStart:
- 指示器的状态图片
- state_expanded:是否是展开状态,注意,该属性在XML中不会提示!但是存在!需要手动写!!
<item android:drawable="@drawable/bottom" android:state_expanded="true"></item>
<item android:drawable="@drawable/right"></item>
- state_expanded:是否是展开状态,注意,该属性在XML中不会提示!但是存在!需要手动写!!
- ExpandableListView使用特定的Adapter
- BaseExpandableListAdapter
- ExpandableListView需要数据类型的问题
- 组的数据 List
- 子的数据 List
GridView
- 继承于AbsListView AdapterView
- 显示也需要三要素!
- GridView相当于多行的ListView
- 属性:
- android:numCloumns:每行中有几列!列数不建议写成固定值,这样整体适配效果不好!写成auto_fit为自动适应
- android:cloumnWidth:列宽
- android:
- android:
- android:
- GridView的使用
- 和ListView的使用一模一样,连监听事件都一模一样
- 如何根据意图查找对应的Activity信息
- 定义想要查找的意图
Intent intent= new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
- 获取PackageManager
PackageManager pm = Contex.getPackageManager();
- 使用PackageManager进行查找
/*
参数1:Intent意图,你想要查找的Activity,Intent隐式意图匹配 Action、Category、Data、Type
每个程序中都会有一个入口启动Activity 他们的intent-filter都是固定的
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
参数2:是否模糊查找,0是准确查找
*/
List<ResolveInfo> data = pm.queryIntentActivities(intent, 0);
- ResolveInfo实体类,内部包含大量的信息
- Drawable:loadIcon(packageManager)获取程序的logo
- Char:loadLabel(packageManager)获取程序的名称
- ResolveInfo.activityInfo.name:Activity的全路径
- ResolveInfo.activityInfo.packageName:程序的包名
- 定义想要查找的意图
- 跨程序页面跳转
Intent intent=new Intent();
/*
*参数1:目标程序的包名
*参数2:目标程序的类名
*/
ComponentName componentName=new ComponentName(参数1,参数2);
intent.setComponent(componentName);
startActivity(intent);
总结
- 三个视图的加载
- 头视图:addHeaderView
- 足视图:addFooterView
- 空视图:setEmptyView
- 空视图的作用
- 当没有数据的时候!显示!当有数据的时候会自动隐藏!给用户一定提示!
- 当没有数据的时候,可以隐藏头视图和足视图!
- 空视图的添加方法:
- 如果只想加一个简单的TextView,可以直接将TextView写在ListView所在的布局中!
- 如果想要创建一个复杂的空视图,可以创建一个专门的Layout,使用
LayoutInflater.from(Context).inflate(R.layout.空视图文件,(ViewGroup)List.getParent(),false)
- 空视图的作用
- ListView属性
- SimpleAdapter(参数1,参数2,参数3,参数4,参数5)
- 参数1:上下文对象Context
- 参数2:数据,数据类型必须为List
0 0
- ListView优化
- listView优化
- ListView优化
- ListView优化
- ListView优化
- ListView优化
- listView优化
- listview 优化
- ListView优化
- listView优化
- ListView 优化
- ListVIew优化
- ListView优化
- ListView优化
- listview优化
- Listview 优化
- ListView优化
- ListView 优化
- ContentProvider
- Intent
- 将eclipse中的Android项目导入到Android Studio
- ListView
- Unity 实现Android不锁屏
- ListView优化
- 我的C语言
- Loader加载器
- python模块学习---urllib,urlparse(3.0以后有改动)
- 学期总结
- markdown 语法之字体、字号、颜色以及背景色(转载)
- Menu菜单
- 大数据Spark “蘑菇云”行动第96课:基于Hive对电商数据案例分析
- SQLite数据库