[android] 练习使用ListView(一)
2023-02-18 15:47:23 时间
练习使用ListView,BaseAdapter,先展示文字的,再练习图片的
MainActivity.java
package com.android.test; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; public class MainActivity extends Activity { private ListView lv_images; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv_images = (ListView) findViewById(R.id.lv_images); ListAdapter adapter = new ImageAdapter(); lv_images.setAdapter(adapter); } /** * 适配器 * @author taoshihan * */ class ImageAdapter extends BaseAdapter { @Override public View getView(int position, View convertView, ViewGroup parent) { String url=(String) getItem(position); View view; if(convertView!=null){ view=convertView; }else{ view=View.inflate(getApplicationContext(), R.layout.image_item, null); } TextView tv_image=(TextView) view.findViewById(R.id.tv_image); tv_image.setText(url); return view; } @Override public int getCount() { // TODO Auto-generated method stub return Images.IMAGE_URLS.length; } @Override public Object getItem(int position) { return Images.IMAGE_URLS[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } } static class Images{ public final static String[] IMAGE_URLS = new String[]{ "//img-my.csdn.net/uploads/201508/05/1438760758_3497.jpg", "//img-my.csdn.net/uploads/201508/05/1438760758_6667.jpg", "//img-my.csdn.net/uploads/201508/05/1438760757_3588.jpg", "//img-my.csdn.net/uploads/201508/05/1438760756_3304.jpg", "//img-my.csdn.net/uploads/201508/05/1438760755_6715.jpeg", "//img-my.csdn.net/uploads/201508/05/1438760726_5120.jpg", "//img-my.csdn.net/uploads/201508/05/1438760726_8364.jpg", "//img-my.csdn.net/uploads/201508/05/1438760725_4031.jpg", "//img-my.csdn.net/uploads/201508/05/1438760724_9463.jpg", "//img-my.csdn.net/uploads/201508/05/1438760724_2371.jpg", "//img-my.csdn.net/uploads/201508/05/1438760707_4653.jpg" }; } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lv_images"></ListView> </LinearLayout>
image_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/tv_image" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
相关文章
- Android添加布局和按键
- Android添加背景图片和设置app图标
- Android开发的开始
- 微信小程序自定义弹窗组件 action-sheet
- 微信小程序轮播图demo
- 小程序轮播图指示器(跑马灯)样式
- 小程序修改元素样式方法
- 微信小程序上拉触底事件函数onReachBottom不触发的解决方案
- 微信小程序 函数防抖和函数节流
- 微信小程序cover-view、view标签内文字无法自动换行的解决办法
- 原生微信小程序实现tab切换效果demo
- 微信小程序修改data数据的方法
- 微信小程序显示多个空格 适用表单对齐
- 微信小程序返回上一页传值方法
- 微信小程序获取用户基本信息的几种方式
- 微信小程序 内容评论-回复评论-回复回复的实现
- 微信小程序 picker-从底部弹起的滚动选择器
- 微信小程序input框输入值获取
- 微信小程序事件对象currentTarget和target属性的区别
- 企业微信 iOS 工程演进之路(一)- 组件化