依然是一个listView的Java文件

 1 public class ListViewActivity extends Activity {
2 private ListView lv1;
3 @Override
4 protected void onCreate(@Nullable Bundle savedInstenceState) {
5
6 super.onCreate(savedInstenceState);
7 setContentView(R.layout.activity_listview);
8 lv1=findViewById(R.id.lv_1);
9 lv1.setAdapter(new MylistAdapter(ListViewActivity.this));
10 lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
11 @Override
12 public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
13 Toast.makeText(ListViewActivity.this, "点击pos:"+i, Toast.LENGTH_SHORT).show();
14 }
15 });
16 lv1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
17 @Override
18 public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
19 Toast.makeText(ListViewActivity.this, "长按pos:"+i, Toast.LENGTH_SHORT).show();
20 return true;//这里如果返回false的话,处理完长按事件后还会处理点击事件,也就是说会弹出两个提示框,为了让他只显示一个对话框改为true
21 }
22 });
23 }
24 }

对应的activity_listview.xml文件

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:layout_width="match_parent"
3 android:layout_height="match_parent">
4
5 <ListView
6 android:id="@+id/lv_1"
7 android:layout_width="match_parent"
8 android:layout_height="wrap_content"
9 android:listSelector="@drawable/list_item"/>
10 <!-- 这里listSelector设置的是点击样式,效果-->
11
12 </LinearLayout>

其中设置的点击效果xml文件为

1 <selector xmlns:android="http://schemas.android.com/apk/res/android">
2 <item android:state_pressed="true" android:drawable="@color/purple_200"/>
3 <item android:drawable="@color/touming"/>
4 </selector>

这里面state_pressed和state_checked是不一样的,checked是选中的意思,pressed是按压时候的效果,注意区分。

然后layout_list_item.xml文件是设置list中每一个元素的样式结构的文件

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:layout_width="match_parent"
3 android:layout_height="match_parent"
4 android:orientation="horizontal"
5 android:padding="10dp">
6 <ImageView
7 android:id="@+id/imv"
8 android:layout_width="100dp"
9 android:layout_height="100dp"
10 android:scaleType="centerCrop"
11 android:background="#000"/>
12 <LinearLayout
13 android:layout_width="match_parent"
14 android:layout_height="wrap_content"
15 android:orientation="vertical"
16 android:paddingLeft="10dp">
17 <TextView
18 android:id="@+id/tv_title"
19 android:layout_width="wrap_content"
20 android:layout_height="wrap_content"
21 android:text="你好你好"
22 android:textColor="@color/black"
23 android:textSize="20sp"/>
24 <TextView
25 android:id="@+id/tv_time"
26 android:layout_width="wrap_content"
27 android:layout_height="wrap_content"
28 android:text="2022-01-28"
29 android:textColor="@color/greydrak"
30 android:paddingTop="10dp"
31 android:textSize="17sp"/>
32 <TextView
33 android:id="@+id/tv_content"
34 android:layout_width="wrap_content"
35 android:layout_height="wrap_content"
36 android:text="你好,我是内容"
37 android:textColor="@color/greydrak"
38 android:paddingTop="10dp"
39 android:textSize="17sp"/>
40 </LinearLayout>
41 </LinearLayout>

然后建立MyListAdapter的Java文件来定义列表中每个元素的具体结构的值:

 1 public class MylistAdapter extends BaseAdapter {
2 private Context context;
3 private LayoutInflater layoutInflater;
4 public MylistAdapter(Context context){
5 this.context=context;
6 layoutInflater=LayoutInflater.from(context);
7 }
8 @Override
9 public int getCount() {
10 return 10;
11 }//列表长度
12
13 @Override
14 public Object getItem(int i) {
15 return null;
16 }//不怎么用
17
18 @Override
19 public long getItemId(int i) {
20 return 0;
21 }//不怎么用
22
23 static class ViewHolder{
24 public ImageView imageview;
25 public TextView tvTitle,tvTime,tvContent;
26 }
27
28 @Override
29 public View getView(int i, View view, ViewGroup viewGroup) {
30 ViewHolder holder=null;
31 if(view==null){
32 view=layoutInflater.inflate(R.layout.layout_list_item,null);
33 holder=new ViewHolder();
34 holder.imageview=view.findViewById(R.id.imv);
35 holder.tvTitle=view.findViewById(R.id.tv_title);
36 holder.tvContent=view.findViewById(R.id.tv_content);
37 holder.tvTime=view.findViewById(R.id.tv_time);
38 view.setTag(holder);
39 }else{
40 holder= (ViewHolder) view.getTag();
41 }
42 //给控件赋值
43 holder.tvTitle.setText("我是大标题");
44 holder.tvTime.setText("2066-06-6");
45 holder.tvContent.setText("说点啥呢啥呢");
46 Glide.with(context).load("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png").into(holder.imageview);
47 return view;
48 }
49 }

最新文章

  1. 密码学初级教程(三)公钥密码RSA
  2. String,StringBuffer与StringBuilder的区别??[转]
  3. [Effective JavaScript 笔记] 第5条:避免对混合类型使用==运算符
  4. 如何自定义Liferay 7 portal的Log in登录界面
  5. ECMA中关于if与else的关系的一句英文,感觉比较经典
  6. iOS-Storyboad动态刷新
  7. uoj Goodbye Jiawu
  8. HttpServletRequest对象(一)
  9. linux中/etc/profile、/etc/profile.d/、/etc/bashrc、~/.bashrc、~/.bash_profile、~/.bash_logout的作用与区别
  10. Java NIO 学习笔记五 缓冲区补充
  11. virtio 简介
  12. 探索Java9 模块系统和反应流
  13. IDEA 自动生成Hibernate实体类和Mapping文件
  14. HBase学习笔记2 - HBase shell常用命令
  15. MySQL数据库crash的问题分析
  16. LeetCode – All Nodes Distance K in Binary Tree
  17. elasticsearch:shard 和 replica 机制
  18. notepad++下载32位,安装插件管理
  19. Python break 语句
  20. Oracle基本概念与数据导入

热门文章

  1. 平滑增加Nginx模块
  2. linux关闭防火墙与开启防火墙命令
  3. HashMap相关
  4. 使用gige2500万相机时遇见的问题(条纹以及取图过久)
  5. CentOS-7部署Docker容器
  6. open policy agent 语法总结
  7. Jenkins--持续集成及Jenkins介绍
  8. python语法_1基础语法概述
  9. 如何写出优雅又地道的Python代码?【转载】
  10. Netty核心原理