[android] 练习使用ListView(一)
2023-02-18 15:47:42 时间
练习使用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>
相关文章
- 基于Yii2接口的在线答题小程序
- [开源推荐] EasyWeChat开源微信SDK支持公众号 小程序 微信支付 开放平台
- 【今天吃什么】uniApp和微信小程序同款炫酷摇一摇来告诉你!
- 【会说话的小鸟】给你的微信小程序加一个宠物吧!
- 【视频刷刷刷】五分钟带你用uniapp做一个刷抖音的小程序
- java开发的师生评教小程序学生对老师评价老师对班级评价打分题单选题意见框系统选课系统
- java和springboot开发祭祀小程序扫墓系统代祭小程序
- Android平台GB28181设备接入模块摄像头采集方向不对怎么办?
- 小程序wx.showToast真机不显示?
- 如何使用Android可视化埋点
- 如何从零开始集成DTM Android SDK
- 论文解读(DGB)《Self-supervised Graph Representation Learning via Bootstrapping》
- 论文解读(BGRL)《Large-Scale Representation Learning on Graphs via Bootstrapping》
- 浅谈android端的字符串加密
- Android tint颜色渲染
- Android字体渐变效果
- 关于airtest群控制ios
- BootstrapTable 动态显示/隐藏某一行
- Android Studio 中 添加作者 时间 之类的 注释
- 解决android studio 无法更新插件的问题(某些)