欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android超詳細(xì)講解組件AdapterView的使用

 更新時(shí)間:2022年03月31日 14:36:45   作者:AdapterView  
AdapterView組件是一組重要的組件,AdapterView本身是一個(gè)抽象基類,它派生的子類在用法上十分相似,從AdapterView派生出的三個(gè)子類:AdsListView、AdsSpinner、AdapterViewAnimator,這3個(gè)子類依然是抽象的,實(shí)際運(yùn)用時(shí)需要它們的子類

概述 

      在Android應(yīng)用開發(fā)中,AdapterView是一類常用且非常重要的組件。我們常見的以列表的形式顯示信息的組件就是AdapterView的子類,稱為L(zhǎng)istview;我們經(jīng)常以網(wǎng)格方式瀏覽圖片縮略圖的組件也是AdapterView的子類,被稱為GridView;以下拉列表形式顯示可選項(xiàng)的組件也是AdapterView的子類,稱為Spinner;還有等等它們都是AdapterView的子類。

介紹AdapterView的編程模式 

     用Android的ListView組件以列表顯示Android系統(tǒng)中已經(jīng)安裝的所有程序信息。ListView,顧名思義,就是通過列表的形式向用戶展示信息。就像你手機(jī)設(shè)置里面的列表,它包含了你所有應(yīng)用程序的圖標(biāo), 應(yīng)用程序名稱(類似下圖)和入口Activity的類名。當(dāng)顯示的內(nèi)容超出物理屏幕可用區(qū)域時(shí),它還可以進(jìn)行滾動(dòng),就跟上期我們說的ScrollView的效果一樣。

       如下圖:黃色大框的部分是一個(gè)ListView,深藍(lán)色小框框住的部分是一個(gè)列表中的一個(gè)列表項(xiàng),因此在程序中要使用ListView顯示信息,必須要做一下的工作。

(1)在界面布局中包含一個(gè)ListView組件

(2)對(duì)在列表中顯示的列表項(xiàng)進(jìn)行布局

(3)設(shè)計(jì)一個(gè)實(shí)現(xiàn)了Adapter接口的類,用于為L(zhǎng)istView組件提供需要顯示的數(shù)據(jù)。

Adapter 

     剛剛提到的列表組件(ListView),網(wǎng)格組件(GridView)和下拉列表組件(Spinner),它們都是Adapter的子類,這些組件只負(fù)責(zé)顯示數(shù)據(jù),而對(duì)于這些要顯示的數(shù)據(jù)則必須通過稱為Adapter的接口來進(jìn)行管理。以使用ListView顯示數(shù)據(jù)為例,AdapterView和Adapter接口的關(guān)系如下圖:

Adapter常用方法及其含義:

方法名字含義
int getCount()返回要顯示的數(shù)據(jù)集中的數(shù)據(jù)總數(shù)
Object getItem(int position)返回?cái)?shù)據(jù)集中指定位置的數(shù)據(jù)對(duì)象
long getItemId(int position)返回?cái)?shù)據(jù)集中指定位置的數(shù)據(jù)的ID

View getView(int position,

View convertView,

ViewGroup parent)

將指定位置的數(shù)據(jù)構(gòu)建成一個(gè)可以顯示在AdapterView中的組件,并返回AdapterView進(jìn)行顯示

ListView使用

activity_main.xml 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:stretchColumns="2"
    >
 
    <ListView
        android:id="@+id/lv1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
</RelativeLayout>

選擇New→Layout resoure file 創(chuàng)建

 item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:stretchColumns="2"
    >
 
    <ListView
        android:id="@+id/lv1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
 
</RelativeLayout>

myAdapater.java

package com.example.demo03_22;
 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
 
 
public class myAdapater extends BaseAdapter {
    private String[] books={"Java程序設(shè)計(jì)","Android應(yīng)用開發(fā)","oracle數(shù)據(jù)庫管理指南","JavaWeb程序設(shè)計(jì)","軟件工程之系統(tǒng)工程師之路"};
    LayoutInflater inflater;
    int id_item;
    public myAdapater(Context context,int id_item){
        this.id_item=id_item;
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return books.length;
    }
 
    @Override
    public Object getItem(int i) {
        return books[i];
    }
 
    @Override
    public long getItemId(int i) {
        return i;
    }
 
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        TextView tv;
        tv=(TextView) inflater.inflate(id_item,viewGroup,false);
        tv.setText(books[i]);
        return tv;
    }
}

MainActivity.java

package com.example.demo03_22;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.widget.ListView;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        ListView lv=(ListView) this.findViewById(R.id.lv1);
        myAdapater myAdapater=new myAdapater(this, R.layout.item);
        lv.setAdapter(myAdapater);
    }
}

測(cè)試結(jié)果:

 改進(jìn):添加圖片

activity_main.xml  

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:stretchColumns="2"
    >
 
    <ListView
        android:id="@+id/lv1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
 
</RelativeLayout>

 item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/id_booknames"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
 
 
    <ImageView
        android:id="@+id/book_phone"
        android:layout_width="80dp"
        android:layout_height="80dp"
 
        />
    <TextView
        android:id="@+id/book_name"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        />
 
 
 
</LinearLayout>

myAdapater.java

package com.example.demo03_22;
 
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
 
 
public class myAdapater extends BaseAdapter {
    private BookItem[] books={new BookItem("陳某人",R.drawable.dog),
            new BookItem("周某人",R.drawable.dog),
            new BookItem("鐘某人", R.drawable.dog),
            new BookItem("林某人",R.drawable.dog),
            new BookItem("濤某人",R.drawable.dog)};
 
    LayoutInflater inflater;
    int id_item;
    public myAdapater(Context context,int id_item){
        this.id_item=id_item;
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return books.length;
    }
 
    @Override
    public Object getItem(int position) {
        return books[position];
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @SuppressLint("ViewHolder")@Override
    public View getView(int position, View view, ViewGroup parent) {
        LinearLayout LL=(LinearLayout)inflater.inflate(id_item,parent,false)
                ;
        ImageView iv=(ImageView)LL.findViewById(R.id.book_phone);
        iv.setImageResource(books[position].photo);
 
        TextView tv;
        tv=(TextView)LL.findViewById(R.id.book_name);
        tv.setText(books[position].name);
 
        return LL;
    }
/**
 *  定義一個(gè)圖片類
  */
    private class BookItem{
        String name;
        int photo;
 
        public BookItem(String name,int photo){
            this.name=name;
            this.photo=photo;
        }
    }
}

MainActivity.java

package com.example.demo03_22;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        ListView lv=(ListView) this.findViewById(R.id.lv1);
        myAdapater myAdapater=new myAdapater(this, R.layout.item);
        lv.setAdapter(myAdapater);
        lv.setOnItemClickListener(this);
    }
 
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView textView=(TextView)view.findViewById(R.id.book_name);
        String name=(String)textView.getText();
        String text="確定選擇"+name+"今晚打火鍋嗎";
        Toast.makeText(this,text,Toast.LENGTH_LONG).show();
    }
}

到此這篇關(guān)于Android超詳細(xì)講解組件AdapterView的使用的文章就介紹到這了,更多相關(guān)Android AdapterView內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論