1.ListView简单用法
第一步:
在activity_main.xml中增加:
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
第二步:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1 ,data); //data是自定义数组
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
2.定制LIstView界面(MVC)
V层:
在activity_main.xml建立一个ListView控件
新建立一ListView的页面子布局
(绘制V层的时候会自动调用getView())
M层:
建立一需要展示的实体类
C层:
建立一自定义Adapter(继承ArrayAdapter)
例子如下:
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
/***
*
* @param context 域,上下文
* @param textViewResourceId 布局ID
* @param objects 实体对象
*/
public FruitAdapter(Context context,int textViewResourceId, List<Fruit> objects ) {
super(context, textViewResourceId, objects);
resourceId = textViewResourceId;
}
*Provides the metrics for the shadow image. These include the dimensions of
* the shadow image, and the point within that shadow that should
* be centered under the touch location while dragging.
//控制展示,相当于DAO类,获得数据
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Fruit fruit = getItem(position); //自动将List<Fruit>中的值解出,获取当前项的Fruit实例
//new 出布局、控件
View view = LayoutInflater.from(getContext()).inflate(resourceId,null); (布局)
ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image); (控件)
TextView fruitName = (TextView) view.findViewById(R.id.fruit_name); (控件)
//赋值
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view;
}
}
在MainActivity中调用,相当于Action类
例子如下:
private List<Fruit> fruitList = new ArrayList<Fruit>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化fruitList
initFruits();
FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
ListView listView = (ListView) findViewById(R.id.list_view);
//返回数据至V层
listView.setAdapter(adapter);
}
private void initFruits() {
Fruit apple = new Fruit("Apple",R.drawable.a);
fruitList.add(apple);
Fruit banana = new Fruit("Banana",R.drawable.b);
fruitList.add(banana);
Fruit orange = new Fruit("Orange",R.drawable.c);
fruitList.add(orange);
Fruit watermelon = new Fruit("Watermelon",R.drawable.d);
fruitList.add(watermelon);
Fruit pear = new Fruit("Pear",R.drawable.e);
fruitList.add(pear);
Fruit grape = new Fruit("Grape",R.drawable.f);
fruitList.add(grape);
Fruit pineapple = new Fruit("Pineapple",R.drawable.g);
fruitList.add(pineapple);
Fruit strawberry = new Fruit("Strawberry",R.drawable.h);
fruitList.add(strawberry);
Fruit cherry = new Fruit("Cherry",R.drawable.i);
fruitList.add(cherry);
Fruit mango = new Fruit("Mango",R.drawable.j);
fruitList.add(mango);
}
getView:
getView()方法是ListView的每个列表项目绘制在屏幕上时被调用
3.提升listview的运行效率
View view; if (convertView == null) { view = LayoutInflater.from(getContext()).inflate(resourceId, null); } else { view = convertView; }
ConvertView是对之前加载好的布局进行缓存
4.Android获取LayoutInflater对象的方法总结
获取context对象
一:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View child = inflater.inflate(R.layout.child, null);
二:
LayoutInflater inflater = LayoutInflater.from(context); View child = inflater.inflate(R.layout.child, null);
方法1和方法2其实都是对context().getSystemService()的使用
相关推荐
listView的item滑动解锁功能,其中关于焦点问题的讨论大家可以自行找资料用这个例子改改。
横向ListViewDemo,很简单,但是很实用的demo,希望可以帮助到大家,如果大家还有其他问题欢迎加入我的开发群讨论交流: 开发一群:454430053开发二群:537532956
Android提高篇之listView点击button翻页功能实现源码,最近的开发需要在手机上实现列表分页功能,可以设置每页显示的记录数,第一页和最后一页翻页按钮自动置灰。代码中包括RelativeLayout对视图(View)和按钮位置...
由于在适配器中不能直接监听修改输入框右下角监听现实剩余输入数字tv,所以要写一个回调 在Activity中修改tv,我写的这个例子上传上去,和大家交流,讨论,学习
在WPF ListView中讨论自动拖放。
之前我们可能会也会肯定遇到了图片的异步加载问题,然而我们也可能会遇到图片二次或多次加载,这是ListView的特性造成的,具体原因不在这里讨论,又或者是OOM等问题。今天要讲的是一个开源框架Imageloader,个人觉得...
就实现了些小功能,我也新手,大家一起讨论
本文讨论了一种使用JavaScript重新排列ASP.NET ListView项的简单方法。
列表Kotlist 是 Kotlin 中一个简单的 ListView 实现(仅供学习)! 它的创建是为了探索在 Android 项目中使用 Kotlin 的可行性。使用 Kotlin 的优势我不会在这里讨论 - Kotlin 很棒的原因有很多! 如果您对使用 ...
本文简单实现了用ListView显示SDCard文件列表,目录的回退等功能暂不讨论,获取文件列表,files即为所选择目录下的所有文件列表
自己写的,大家能用就用,有问题讨论更改。
ReaderRSS-listView Titanium Alloy 中的小型应用程序(用于教育目的),它以 JSON 格式... 我试图讨论代码中最重要的部分,尤其是 UI listView 组件和模型/集合的使用。 此外,我通过添加了小部件和基本图形艺术。
作者vivian8725118,源码SearchListView,讨论问题可以点击这个按钮(☞゚ヮ゚)☞Join the chat at https://gitter.im/vivian8725118/SearchListView(☜(゚...带搜索栏的 listview,轻拉出现搜索栏,用力拉出现下拉刷新
很多人都在讨论如何让图片能在异步加载更加流畅,可以显示大量图片,在拖动ListView的时候不会出现卡的现象。关于ImageLoader这个开源框架的使用有很多网友都介绍过,不过还不够清楚,这里有一个关于这个开源项目的...
学习起步程序,换一个手机,原手机里的便签很难移动到新的手机里.并且新旧手机便签极不相同,给频繁换手机的用户带来很大的麻烦....欢迎有开发学习安卓apk的爱好者,互相学习讨论. 后续慢慢增加本人认为极好的一些代码.
实现了tab分栏,用listview显示联系人信息,查看信息并加入3d旋转效果。功能不全,请大家多多讨论。
在上篇文章中说过对于像 RecyclerView 或者 ListView 等等此类在有限屏幕中展示大量内容的控件,复用的逻辑就是其核心的逻辑,而关于复用导致最常见的 bug 就是复用错乱。在大上周我就遇到了一个很奇怪的问题,这也...
异步加载图片:解决了图片的异步加载以及OOM和图片错位等问题 CountDownTimer:对Timer的优化,对应用程序中的倒计时等问题很有用 对控件进行拖拽的操作 ...如果代码阅读有任何问题,请加入QQ群:57472287讨论
持续更新中,欢迎一起讨论学习! 如果你支持这个项目,请 Star and Fork Me。 注:有任何问题请在Issues提交,EleTeam会尽快回复。 EleTeam开源项目 - 电商全套解决方案之 React Native 版 - Shop-React-Native。一...