androidinflate詳解 androidlistview的用法

多條告白如次劇本只需引入一次
遲疑了幾天 , 感觸仍舊把這個教程寫一下吧 。固然在網上仍舊之一次全國代表大會堆了 , 然而這是我進修的過程我感觸我仍舊該記載下來 , 此后也不妨溫故而知新 。
ListView在Android稠密控件中占領比擬要害的位置 , 也是口試官景仰發問的控件之一 , 更加是對于它的本能優化 。這一塊我想著把它留到結果再說 , 咱們先來談談ListView的大略運用 , 究竟什么貨色都是由淺入深的嘛 。
開始咱們要先創造一個名目 , 翻開Android studio點擊File—New—New Project創造一個名為ListViewTest的名目 。接著找到res—layout文獻夾下的activity_main.xml , 翻開它而且在內里增添ListView控件如次:
<ListViewandroid:id="@+id/listview"android:layout_width="match_parent"android:layout_height="match_parent"></ListView>此時你即使運轉名目你會創造內里什么都沒有 , 和剛發端創造的這個名目時沒多大辨別 , 由于咱們還沒有往內里增添數據而且在View中實行它 。以是咱們回到MainActivity這個類內里經過findViewById()這個本領找到這個控件而且實行 。咱們先設置一個名為data的一維字符串數組 , 用來寄存咱們的假數據 。而后經過興建一個ArrayAdapter并按照訴求擺設它 , 再Adapte經過setAdapter給ListView , 代碼如次:
private String data[] = {"aa","bb","cc","dd","aa","bb","cc","dd","aa","bb","cc","dd","aa","bb","cc","dd"};//假數據@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ListView listView = (ListView) findViewById(R.id.listview);//在視圖中找到ListViewArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);//興建并擺設ArrayAapeterlistView.setAdapter(adapter); }點擊運轉名目你就能看到一個大略的ListView:
此刻看到了界面了然而離咱們的預見仍舊有點差異 , 咱們蓄意的是除去能看還能點擊相應某些事變 , 所以咱們再為它增添一個監聽點擊的本領 。代碼如次:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {switch (i){case 0:Toast.makeText(MainActivity.this,"你點擊了"+i+"按鈕",Toast.LENGTH_SHORT).show();break;//當咱們點擊某一項就能吐司咱們點了哪一項case 1:Toast.makeText(MainActivity.this,"你點擊了"+i+"按鈕",Toast.LENGTH_SHORT).show();break;case 2:Toast.makeText(MainActivity.this,"你點擊了"+i+"按鈕",Toast.LENGTH_SHORT).show();break;case 3:Toast.makeText(MainActivity.this,"你點擊了"+i+"按鈕",Toast.LENGTH_SHORT).show();break;case 4:Toast.makeText(MainActivity.this,"你點擊了"+i+"按鈕",Toast.LENGTH_SHORT).show();break;}}});這邊我就給了5項Item做了相應 , 固然也不妨讓每一項都有相應的 , 有愛好本人不妨去試驗 。如許一個特殊大略的ListView就實行了 , 接下來咱們來深刻一點點 。
此刻咱們要定制一個有圖片有筆墨有采用框的ListView , 如何做呢?之一個咱們確定要把數據改一下 , 然而咱們確定不大概說把數組data改成二維數據就不妨的 , 由于圖片不是字符串的情勢啊 。那要包括字符串又能包括圖片的數據 *** 有什么呢?這時候Bean類就展示了 , 咱們不妨把那些數據封裝到一個Bean類內里啊 , 當咱們須要的功夫就徑直拿出來就好 。說做就做而后咱們設置一個myBean類 , 代碼如次:
public class myBean {private String text;//用來放筆墨的private int ImageID;//用來放圖片的public myBean(String text,int imageID){this.ImageID = imageID;this.text = text;}public String getText() {return text;}public void setText(String text) {this.text = text;}public int getImageID() {return ImageID;}public void setImageID(int imageID) {ImageID = imageID;}}而后咱們就不妨經過初始化連接的New一個一個的數據了 , 然而咱們如何放進ListView內里呢?由于咱們方才用的是體例的ArrayAdapter來適配到ListView的 , 咱們以至連要適配的XML的界面都沒 。那咱們先去做個咱們要適配的界面去看看 , 所以:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent" android:layout_height="match_parent"><LinearLayoutandroid:id="@+id/ll_view"android:gravity="center"android:layout_margin="10dp"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"><ImageViewandroid:background="@mipmap/ic_launcher"android:id="@+id/headimage"android:layout_width="wrap_content"android:layout_height="wrap_content" /><TextViewandroid:layout_marginLeft="20dp"android:layout_weight="1"android:text="你是SB"android:id="@+id/headtext"android:layout_width="0dp"android:layout_height="wrap_content" /><RadioGroupandroid:id="@+id/radioBtn"android:orientation="horizontal"android:layout_width="wrap_content"android:layout_height="wrap_content"><RadioButtonandroid:text="打他"android:id="@+id/radio2"android:layout_width="wrap_content"android:layout_height="wrap_content" /><RadioButtonandroid:text="不打"android:id="@+id/radio1"android:layout_width="wrap_content" android:layout_height="wrap_content" /></RadioGroup></LinearLayout></LinearLayout>所以咱們把之前的R.layout.simple_list_item_1這XML換成咱們徑直做的 , 運路途序你就會創造步調崩了 。嘿嘿 , 不重要這是平常的由于咱們傳入的數據都沒用適配到咱們的界面上 。以是咱們就只能本人寫過一個適配重來適配咱們本人的數據 。

推薦閱讀