Android之ListView

1:簡單列表(ArrayAdapter)

1:運行的結果:

2:首先在MyListView里面創建一個按鈕,點擊的時候進行跳轉。

這里讓我吃驚的是,Button里面可以直接設置onClick = .java里面的方法。

也即是點擊這個按鈕之后就會去調用這個方法。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MyListView"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginRight="10dp"android:orientation="vertical"android:paddingLeft="10dp"><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="Array列表"android:onClick="toArrayListTest"android:textAllCaps="false"/></LinearLayout>
</ScrollView>

3:在MyListView.java里面編寫跳轉代碼

package com.findyou.mylistview;import android.content.Intent;
import android.os.Bundle;
import android.view.View;import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;public class MyListView extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_my_list_view);}public void toArrayListTest(View view) {Intent intent = new Intent(this, ArrayListActivity.class);startActivity(intent);}
}

這里用的也是Intent, 第一個寫的這packageContext, 第二個表示你要跳轉的目的。然后就開啟跳轉startActivity(傳入intent)。

4:在ArrayListActivity里面先準備好 ListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".ArrayListActivity"android:orientation="vertical"><ListViewandroid:id="@+id/lv"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout>

?5:在ArrayListActivity.java里面編寫核心代碼

首先去拿到.xml界面里面的ListView,然后創建一個列表進行填充數據。再需要一個適配器,適配器里面需要的參數有,你這個填充的需要什么樣的格式?(用的是官方的),以及填充的數據是什么?然后把獲得是ListView進行setAdapter,啟動。

對ListView里面的Item進行點擊和長按監聽。里面重寫的參數是包括position的。

package com.findyou.mylistview;import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;import java.util.ArrayList;
import java.util.List;public class ArrayListActivity extends AppCompatActivity {private ListView listView;// 下面這個去提供數據private List<String> mStringList;private ArrayAdapter<String> mArrayAdapter; // 需要一個適配器@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_array_list);listView = findViewById(R.id.lv);// 數據的初始化mStringList = new ArrayList<>();for(int i = 1; i <= 50;  ++ i ) {mStringList.add("這是條目: " + i);}// 第二個參數表示的是 這個的布局是什么?mArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, mStringList);listView.setAdapter(mArrayAdapter); // 填充數據// 設置點擊的監聽listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {Toast.makeText(ArrayListActivity.this, "你點擊了" + position, Toast.LENGTH_SHORT).show();}});// 設置長按的監聽listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {Toast.makeText(ArrayListActivity.this, "你長按了" + position, Toast.LENGTH_SHORT).show();return true;}});}
}

2:simpleList列表(圖文)

?1:運行的結果:

2:首先也是寫一個activity_simple_list.xml, 寫一個大的ListView先撐大

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".SimpleListActivity"android:orientation="vertical"><ListViewandroid:id="@+id/lv"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout>

3:在MyListView.java里面補充跳轉代碼

    public void toSimplArrayListTest(View view) {Intent intent = new Intent(this, SimpleListActivity.class);startActivity(intent);}

4:在SimpleListActivity.java里面配置適配器(adapter)等?

package com.findyou.mylistview;import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
;
import androidx.appcompat.app.AppCompatActivity;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class SimpleListActivity extends AppCompatActivity {private ListView listView;private SimpleAdapter simpleAdapter;private List<Map<String, Object> >list; // 存放數據的private int[] imags =  {R.drawable.test1,R.drawable.test2,R.drawable.test3,R.drawable.test4,R.drawable.test5,R.drawable.test6,R.drawable.test7,R.drawable.test8,R.drawable.test9,};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_simple_list);listView = findViewById(R.id.lv);list = new ArrayList<>();for(int i = 1; i <= 50; i ++ ) {Map<String, Object> mp = new HashMap<>();mp.put("img", imags[i % 9]);mp.put("title", "標題" + i);mp.put("content", "內容" + i);list.add(mp); // 存放全部的數據}simpleAdapter = new SimpleAdapter(this, list,R.layout.list_item_layout,new String[] {"img", "title", "content"},new int[] {R.id.iv_img, R.id.tv_title, R.id.tv_content});// 上面的倒數兩個參數 表示是 從哪到哪 from to, to 表示的是xml里面的idlistView.setAdapter(simpleAdapter);}
}

這里適配器simpleAdapter,里面的參數

this: 表示的是當前

list:? 存放數據的(data), 但是這個里面的要求是List<Map<String, *>> 的形式。

R.layout.list_item_layout: 這個是每一個item的布局是什么樣的。

?new String[] {"img", "title", "content"}: 表示你data里面的數據注意順序(from)。

new int[] 表示的R.id,你要插入到的模板里面的哪一個id(to)。

?R.layout.list_item_layout里面的代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingLeft="10dp"android:paddingTop="5dp"android:paddingRight="10dp"android:paddingBottom="5dp"><ImageViewandroid:id="@+id/iv_img"android:layout_width="100dp"android:layout_height="100dp"android:scaleType="centerCrop"android:src="@drawable/ic_launcher_background"/><TextViewandroid:id="@+id/tv_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_toRightOf="@id/iv_img"android:ellipsize="end"android:maxLines="1"android:text="雨中漫步"android:textSize="20sp"android:textStyle="bold"/><TextViewandroid:id="@+id/tv_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/tv_title"android:layout_marginLeft="10dp"android:layout_toRightOf="@id/iv_img"android:ellipsize="end"android:maxLines="3"android:text="人生就像一場旅行,不必在乎目的地,在乎的是沿途的風景以及看風景的心情,讓心靈去旅行"android:textSize="16sp"/></RelativeLayout>

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/85246.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/85246.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/85246.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python(十四)

1.type函數和init_subclass_ init_subclass_ 2.元類 類就是用來創建對象的模版&#xff0c;類是由type創造而來的&#xff0c;元類就是創建類的模版&#xff0c;type可以用來創造類&#xff0c;因為type本身就是一個元類&#xff0c;使用元類來創造類&#xff0c;元類之間也有…

當前用戶的Git全局配置情況:git config --global --list

通過config命令可以查詢當前用戶的全局配置情況。這些配置項定義了 Git 在全局范圍內的行為&#xff0c;包括如何處理大文件、SSL 證書驗證以及提交時的用戶信息。 git config --global --list http.sslVerifyfalse 這個配置項禁用了 SSL 證書驗證。這在與自簽名證書的 Git 服…

負載均衡群集---Haproxy

目錄 一、HAproxy 一、概念 二、核心作用 三、主要功能特性 四、應用場景 五、優勢與特點 二、 案例分析 1. 案例概述 2. 案例前置知識點 &#xff08;1&#xff09;HTTP 請求 &#xff08;2&#xff09;負載均衡常用調度算法 &#xff08;3&#xff09;常見的 web …

html5視頻播放器和微信小程序如何實現視頻的自動播放功能

在HTML5中實現視頻自動播放需設置autoplay和muted屬性&#xff08;瀏覽器策略要求靜音才能自動播放&#xff09;&#xff0c;并可添加loop循環播放、playsinline同層播放等優化屬性。微信小程序通過<video>組件的autoplay屬性實現自動播放&#xff0c;同時支持全屏按鈕、…

OpenHarmony定制系統組合按鍵(一)

一、開發環境 系統版本&#xff1a;OpenHarmony 4.0.10.13 設備平臺&#xff1a;rk3568 SDK版本&#xff1a;fullSDK 4.0.10.13 DevEco Studio版本&#xff1a;4.1.0.400 二、需求背景 定制OpenHarmony 系統組合按鍵功能&#xff0c;例如仿Android Power VOL_Up組合鍵實現截…

相機定屏問題分析四:【cameraserver 最大request buffer超標】后置視頻模式預覽定屏閃退至桌面

【關注我,后續持續新增專題博文,謝謝!!!】 上一篇我們講了:相機定屏問題分析三:【配流ConfigStream失敗】外屏打開相機視頻照片人像來回切換后,相機頁面卡死,點擊沒反應9055522 這一篇我們開始講: 相機定屏問題分析四:【cameraserver 最大request buffer超…

從 PyTorch 到 TensorFlow Lite:模型訓練與推理

一、方案介紹 研發階段&#xff1a;利用 PyTorch 的動態圖特性進行快速原型驗證&#xff0c;快速迭代模型設計。 靈活性與易用性&#xff1a;PyTorch 是一個非常靈活且易于使用的深度學習框架&#xff0c;特別適合研究和實驗。其動態計算圖特性使得模型的構建和調試變得更加直…

4.2.5 Spark SQL 分區自動推斷

在本節實戰中&#xff0c;我們學習了Spark SQL的分區自動推斷功能&#xff0c;這是一種提升查詢性能的有效手段。通過創建具有不同分區的目錄結構&#xff0c;并在這些目錄中放置JSON文件&#xff0c;我們模擬了一個分區表的環境。使用Spark SQL讀取這些數據時&#xff0c;Spar…

數據結構:導論

目錄 什么是“第一性原理”&#xff1f; 什么是“數據結構”&#xff1f; 數據結構解決的根本問題是什么&#xff1f; 數據結構的兩大分類 數據結構的基本操作 數據結構與算法的關系 學習數據結構的底層目標 什么是“第一性原理”&#xff1f; 在正式進入數據結構之前&…

汽車制造場景下Profibus轉Profinet網關核心功能與應用解析

在當今工業自動化的浪潮中&#xff0c;各種通訊協議層出不窮&#xff0c;而其中PROFIBUS與PROFINET作為兩種主流的工業通信標準&#xff0c;它們之間的轉換需求日益增長。特別是對于那些希望實現老舊設備與現代化網絡無縫對接的企業來說&#xff0c;一個高效、穩定的網關產品顯…

qt ubuntu 20.04 交叉編譯

一、交叉編譯環境搭建 1.下載交叉編譯工具鏈&#xff1a;https://developer.arm.com/downloads/-/gnu-a 可以根據自己需要下載對應版本&#xff0c;當前最新版本是10.3, 筆者使用10.3編譯后的glibc.so版本太高&#xff08;glibc_2.3.3, glibc_2.3.4, glibc_2.3.5&#xff09;…

在Babylon.js中創建3D文字:簡單而強大的方法

引言 在3D場景中添加文字是許多WebGL項目的常見需求。Babylon.js提供了多種創建3D文字的方法&#xff0c;其中使用TextBlock結合平面網格是一種簡單而高效的方式。本文將介紹如何使用Babylon.js的GUI系統在3D空間中創建美觀的文字效果。 方法概述 Babylon.js的GUI系統允許我…

油桃TV v20250519 一款電視端應用網站聚合TV播放器 支持安卓4.1

油桃TV v20250519 一款電視端應用網站聚合TV播放器 支持安卓4.1 應用簡介&#xff1a; 油桃TV是一款開源電視端應用網站聚合瀏覽器&#xff0c;它把大家常見需求的一些網站都整合到了這個應用上&#xff0c;并進行了電視端…

Perl單元測試實戰指南:從Test::Class入門到精通的完整方案

閱讀原文 前言:為什么Perl開發者需要重視單元測試? "這段代碼昨天還能運行,今天就出問題了!"——這可能是每位Perl開發者都經歷過的噩夢。在沒有充分測試覆蓋的情況下,即使是微小的改動也可能導致系統崩潰。單元測試正是解決這一痛點的最佳實踐,它能幫助我們在…

OpenCv高階(十三)——人臉檢測

文章目錄 前言一、人臉檢測—haar特征二、人臉檢測---級聯分類器1、級聯分類器2、如何訓練級聯分類器3、已存在的級聯分類器 三、代碼分析1、人臉檢測的簡單使用2、人臉微笑檢測&#xff08;1&#xff09; 初始化視頻源&#xff08;2&#xff09;主循環處理每一幀&#xff08;3…

無線通信模塊簡介

QuecPython 是運行在無線通信模塊上的開發框架。對于首次接觸物聯網開發的用戶而言&#xff0c;無線通信模塊可能是一個相對陌生的概念。本文主要針對無線通信和蜂窩網絡本身&#xff0c;以及模塊的概念、特性和開發方式進行簡要的介紹。 無線通信和蜂窩網絡 物聯網對無線通信…

Unity 中實現首尾無限循環的 ListView

之前已經實現過&#xff1a; Unity 中實現可復用的 ListView-CSDN博客文章瀏覽閱讀5.6k次&#xff0c;點贊2次&#xff0c;收藏27次。源碼已放入我的 github&#xff0c;地址&#xff1a;Unity-ListView前言實現一個列表組件&#xff0c;表現方面最核心的部分就是重寫布局&…

【C++】 類和對象(上)

1.類的定義 1.1類的定義格式 ? class為定義類的關鍵字&#xff0c;后跟一個類的名字&#xff0c;{}中為類的主體&#xff0c;注意類定義結束時后?分號不能省 略。類體中內容稱為類的成員&#xff1a;類中的變量稱為類的屬性或成員變量;類中的函數稱為類的?法或 者成員函數。…

Transformer架構詳解:從Attention到ChatGPT

Transformer架構詳解&#xff1a;從Attention到ChatGPT 系統化學習人工智能網站&#xff08;收藏&#xff09;&#xff1a;https://www.captainbed.cn/flu 文章目錄 Transformer架構詳解&#xff1a;從Attention到ChatGPT摘要引言一、Attention機制&#xff1a;Transformer的…

Rock9.x(Linux)安裝Redis7

&#x1f49a;提醒&#xff1a;1&#xff09;注意權限問題 &#x1f49a; 查是否已經安裝了gcc gcc 是C語言編譯器&#xff0c;Redis是用C語言開發的&#xff0c;我們需要編譯它。 gcc --version如果沒有安裝gcc&#xff0c;那么我們手動安裝 安裝GCC sudo dnf -y install…