最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android BaseAdapter適配器詳解用法

 更新時(shí)間:2021年10月26日 10:42:09   作者:青素i  
BaseAdapter是最基礎(chǔ)的Adapter類,也是最實(shí)用最常用的一個(gè)類,但是相比于ArrayAdapter之類的,對(duì)初學(xué)者來說卻比較難理解。所以本篇文章在這里介紹一下BaseAdapter

ListView和GridView

ListView,列表視圖,是Android中最重要的組件之一,幾乎每個(gè)Android應(yīng)用中都會(huì)使用ListView。是一個(gè)以垂直方式在項(xiàng)目中顯示View視圖的列表。
GridView,網(wǎng)格試視圖
一、列數(shù)
android:numColumns=“3”
二、水平的距離
android:verticalSpacing=“10dp”
二、垂直的距離
android:horizontalSpacing=“10dp”

顯示與緩存機(jī)制

ListView、GridView等控件可以展示大量的數(shù)據(jù)信息。如果ListView有100條信息,但是屏幕的尺寸是有限的,一屏幕只能顯示下圖中的10條。當(dāng)向上滑動(dòng)ListView的時(shí)候,條目1被滑出了屏幕區(qū)域,那么系統(tǒng)就會(huì)將條目2回收到Recycler中,即View緩沖池中,而將要顯示的條目11則會(huì)從緩存池中取出布局文件,并重新設(shè)置好條目11需要顯示的數(shù)據(jù),并放入需要顯示的位置。這就是ListView的緩沖機(jī)制,需要時(shí)才顯示,顯示完就被會(huì)收到緩存。

BaseAdapter

BaseAdapter是一種Adapter,在Android中,Adapter為適配器,可以構(gòu)建數(shù)據(jù)源與視圖展示的橋梁,從而讓數(shù)據(jù)源與視圖展示相互關(guān)聯(lián),同時(shí)又解除耦合。
繼承此類來實(shí)現(xiàn)BaseAdapter的四個(gè)方法:

public int getCount(): 適配器中數(shù)據(jù)集的數(shù)據(jù)個(gè)數(shù);

public Object getItem(int position): 獲取數(shù)據(jù)集中與索引對(duì)應(yīng)的數(shù)據(jù)項(xiàng);

public long getItemId(int position): 獲取指定行對(duì)應(yīng)的ID;

public View getView(int position,View convertView,ViewGroup parent): 獲取沒一行Item的顯示內(nèi)容。

使用演示

實(shí)現(xiàn)簡單的微信主頁面

布局

ListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.qingsu.weixin.WeiXinStart"
    android:orientation="vertical"
    >


    <Toolbar
        android:layout_width="match_parent"
        android:layout_height="40dp"
        >
    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_gravity="right"
        android:layout_marginRight="15dp"
        android:background="@mipmap/jia"
        />
    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginRight="15dp"
        android:layout_gravity="right"
        android:background="@mipmap/chaxun"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="24dp"
        android:layout_marginLeft="50dp"
        android:gravity="center"
        android:text="微信(999)"
        android:textSize="16sp"
        android:textColor="#000"
        />
    </Toolbar>

    <ListView
        android:id="@+id/lsViewWeiXin"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />


</LinearLayout>

所加條目

<?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="match_parent"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:orientation="horizontal"
            >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                >
                <ImageView
                    android:id="@+id/imgAvatar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/touxiang0" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="4"
                android:orientation="vertical"
                android:gravity="center_vertical"
                >
                <TextView
                    android:id="@+id/tvUserName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="這是網(wǎng)名"
                    android:textColor="#000"
                    android:textSize="14sp"
                    />
                <TextView
                    android:id="@+id/tvMsg"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="3dp"
                    android:text="最初的消息"
                    android:textSize="10sp"
                    />
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

創(chuàng)建數(shù)據(jù)源

JavaBean存放數(shù)據(jù)

public class ItemBean {
    private String userName;
    private String Msg;
    private int imgId;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getMsg() {
        return Msg;
    }

    public void setMsg(String msg) {
        Msg = msg;
    }

    public int getImgId() {
        return imgId;
    }

    public void setImgId(int imgId) {
        this.imgId = imgId;
    }

    public ItemBean(String userName, String msg, int imgId) {
        this.userName = userName;
        Msg = msg;
        this.imgId = imgId;
    }
    
}

初始化數(shù)據(jù)源

 ArrayList<ItemBean> list = new ArrayList<ItemBean>();
 ListView listView = findViewById(R.id.lsViewWeiXin);
        int[] imageNmae = {R.mipmap.touxiang0, R.mipmap.touxiang1, R.mipmap.touxiang2, R.mipmap.touxiang3,
                R.mipmap.touxiang4, R.mipmap.touxiang5, R.mipmap.touxiang6, R.mipmap.touxiang7,
                R.mipmap.touxiang8, R.mipmap.touxiang9, R.mipmap.touxiang10, R.mipmap.touxiang11,
                R.mipmap.touxiang12, R.mipmap.touxiang13, R.mipmap.touxiang14, R.mipmap.touxiang15,
                R.mipmap.touxiang16, R.mipmap.touxiang17, R.mipmap.touxiang18, R.mipmap.touxiang19,

        };

        for (int i = 0; i < 19; i++) {
            String userName = "微信好友" + i;
            String userMsg = "這是一條很長很長很長的語音消息建議您刪除好友" + i;
            list.add(new ItemBean(userName, userMsg, imageNmae[i]));
        }

設(shè)置條目的單機(jī)和長按事件

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(WeiXinStart.this, "第" + position + "個(gè)條目被點(diǎn)擊了", Toast.LENGTH_SHORT).show();
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(WeiXinStart.this, "第" + position + "個(gè)條目被點(diǎn)長按了", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

創(chuàng)建BaseAdapter及設(shè)置緩存

一般采用內(nèi)部類的形式進(jìn)行適配器的自定義
緩存方式
創(chuàng)建ViewHolder類,創(chuàng)建布局映射關(guān)系;
判斷convertView,為空則創(chuàng)建,并設(shè)置tag,不為空則通過tag取出ViewHolder;
給ViewHolder的控件設(shè)置數(shù)據(jù)

    class MyAdapter extends BaseAdapter {

  
        @Override
        //ListView需要顯示的數(shù)據(jù)數(shù)量
        public int getCount() {
            return list.size();
        }

        @Override
        //指定的索引對(duì)應(yīng)的數(shù)據(jù)項(xiàng)
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
         //指定的索引對(duì)應(yīng)的數(shù)據(jù)項(xiàng)ID
        public long getItemId(int position) {
            return position;
        }

        @Override
        //返回每一項(xiàng)的顯示內(nèi)容
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = new ViewHolder();

            //如果view未被實(shí)例化過,緩存池中沒有對(duì)應(yīng)的緩存
            if(convertView == null){
            //進(jìn)行地址初始化
                convertView = LayoutInflater.from(WeiXinStart.this).inflate(R.layout.itemweixin_list,null);
                viewHolder.tvName = convertView.findViewById(R.id.tvUserName);
                viewHolder.tvMsg = convertView.findViewById(R.id.tvMsg);
                viewHolder.imgAvatar = convertView.findViewById(R.id.imgAvatar);
                convertView.setTag(viewHolder);//通過setTag將convertView與viewHolder關(guān)聯(lián)
            } else {
                //如果緩存池中有對(duì)應(yīng)的view緩存,則直接通過getTag取出viewHolder
                viewHolder = (ViewHolder)convertView.getTag();
            }
            // 取出bean對(duì)象
            ItemBean itemBean = list.get(position);
            // 設(shè)置控件的數(shù)據(jù)
            viewHolder.tvName.setText(itemBean.getUserName());
            viewHolder.tvMsg.setText(itemBean.getMsg());
            viewHolder.imgAvatar.setImageResource(itemBean.getImgId());
            return convertView;
        }



        //用于緩存
        class ViewHolder{
            //修改的組件
            TextView tvName,tvMsg;
            ImageView imgAvatar;
        }
    }

設(shè)置適配器

      //創(chuàng)建適配器對(duì)象
        MyAdapter myAdapter = new MyAdapter();
        //設(shè)置適配器
        listView.setAdapter(myAdapter);

完整主代碼

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.qingsu.bean.ItemBean;
import com.qingsu.yingguday05.R;

import java.util.ArrayList;

public class WeiXinStart extends AppCompatActivity {

    ListView listView;
    ArrayList<ItemBean> list = new ArrayList<ItemBean>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wei_xin_start);
        myBaseAdapter();
    }

    public  void myBaseAdapter() {
        //初始化數(shù)據(jù)
        listView = findViewById(R.id.lsViewWeiXin);
        int[] imageNmae = {R.mipmap.touxiang0, R.mipmap.touxiang1, R.mipmap.touxiang2, R.mipmap.touxiang3,
                R.mipmap.touxiang4, R.mipmap.touxiang5, R.mipmap.touxiang6, R.mipmap.touxiang7,
                R.mipmap.touxiang8, R.mipmap.touxiang9, R.mipmap.touxiang10, R.mipmap.touxiang11,
                R.mipmap.touxiang12, R.mipmap.touxiang13, R.mipmap.touxiang14, R.mipmap.touxiang15,
                R.mipmap.touxiang16, R.mipmap.touxiang17, R.mipmap.touxiang18, R.mipmap.touxiang19,

        };

        for (int i = 0; i < 19; i++) {
            String userName = "微信好友" + i;
            String userMsg = "這是一條很長很長很長的語音消息建議您刪除好友" + i;
            list.add(new ItemBean(userName, userMsg, imageNmae[i]));
        }

        //創(chuàng)建適配器對(duì)象
        MyAdapter myAdapter = new MyAdapter();
        listView.setAdapter(myAdapter);

		//單機(jī)事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(WeiXinStart.this, "第" + position + "個(gè)條目被點(diǎn)擊了", Toast.LENGTH_SHORT).show();
            }
        });

		//長按事件
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(WeiXinStart.this, "第" + position + "個(gè)條目被點(diǎn)長按了", Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }

    class MyAdapter extends BaseAdapter {

  
        @Override
        //ListView需要顯示的數(shù)據(jù)數(shù)量
        public int getCount() {
            return list.size();
        }

        @Override
        //指定的索引對(duì)應(yīng)的數(shù)據(jù)項(xiàng)
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
         //指定的索引對(duì)應(yīng)的數(shù)據(jù)項(xiàng)ID
        public long getItemId(int position) {
            return position;
        }

        @Override
        //返回每一項(xiàng)的顯示內(nèi)容
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = new ViewHolder();

            //如果view未被實(shí)例化過,緩存池中沒有對(duì)應(yīng)的緩存
            if(convertView == null){
            //進(jìn)行地址初始化
                convertView = LayoutInflater.from(WeiXinStart.this).inflate(R.layout.itemweixin_list,null);
                viewHolder.tvName = convertView.findViewById(R.id.tvUserName);
                viewHolder.tvMsg = convertView.findViewById(R.id.tvMsg);
                viewHolder.imgAvatar = convertView.findViewById(R.id.imgAvatar);
                convertView.setTag(viewHolder);//通過setTag將convertView與viewHolder關(guān)聯(lián)
            } else {
                //如果緩存池中有對(duì)應(yīng)的view緩存,則直接通過getTag取出viewHolder
                viewHolder = (ViewHolder)convertView.getTag();
            }
            // 取出bean對(duì)象
            ItemBean itemBean = list.get(position);
            // 設(shè)置控件的數(shù)據(jù)
            viewHolder.tvName.setText(itemBean.getUserName());
            viewHolder.tvMsg.setText(itemBean.getMsg());
            viewHolder.imgAvatar.setImageResource(itemBean.getImgId());
            return convertView;
        }



        //用于緩存
        class ViewHolder{
            //修改的組件
            TextView tvName,tvMsg;
            ImageView imgAvatar;
        }
    }



}

演示

在這里插入圖片描述

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

相關(guān)文章

  • android ToolBar的簡單使用

    android ToolBar的簡單使用

    這篇文章主要為大家詳細(xì)介紹了android ToolBar的簡單使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Flutter中灰屏問題的原因及解決方法

    Flutter中灰屏問題的原因及解決方法

    生產(chǎn)中的 flutter 應(yīng)用程序中的灰屏是一種通用占位符,當(dāng)框架遇到問題無法渲染預(yù)期用戶界面時(shí)就會(huì)顯示,所以基本上是出現(xiàn)問題時(shí)的后備指示器,本文給大家介紹了Flutter中灰屏問題的原因及解決方法,需要的朋友可以參考下
    2024-06-06
  • android 自定義view實(shí)現(xiàn)彩虹進(jìn)度條功能

    android 自定義view實(shí)現(xiàn)彩虹進(jìn)度條功能

    實(shí)現(xiàn)一個(gè)彩虹色進(jìn)度條功能,不說明具體用途大家應(yīng)該能猜到,想找別人造的輪子,但是沒有合適的,所以決定自己實(shí)現(xiàn)一個(gè),下面小編通過實(shí)例代碼給大家分享android 自定義view實(shí)現(xiàn)彩虹進(jìn)度條功能,感興趣的朋友一起看看吧
    2024-06-06
  • 如何正確使用Android線程詳解

    如何正確使用Android線程詳解

    線程是程序員進(jìn)階的一道重要門檻。除了了解各類開線程的API之外,更需要理解線程本身到底是個(gè)什么樣的存在,并行是否真的高效?系統(tǒng)是怎么樣去調(diào)度線程的?開線程的方式那么多,什么樣的姿勢(shì)才正確?下面通過本文來好好再學(xué)習(xí)下。
    2016-08-08
  • Android Selector獲取焦點(diǎn)后文本背景修改的實(shí)現(xiàn)代碼

    Android Selector獲取焦點(diǎn)后文本背景修改的實(shí)現(xiàn)代碼

    這篇文章主要介紹了Android Selector獲取焦點(diǎn)后文本背景修改的實(shí)現(xiàn)代碼,本文通過demo展示和實(shí)現(xiàn)代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Android編程實(shí)現(xiàn)將壓縮數(shù)據(jù)庫文件拷貝到安裝目錄的方法

    Android編程實(shí)現(xiàn)將壓縮數(shù)據(jù)庫文件拷貝到安裝目錄的方法

    這篇文章主要介紹了Android編程實(shí)現(xiàn)將壓縮數(shù)據(jù)庫文件拷貝到安裝目錄的方法,涉及Android處理壓縮文件的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • Flutter仿網(wǎng)易實(shí)現(xiàn)廣告卡片3D翻轉(zhuǎn)效果

    Flutter仿網(wǎng)易實(shí)現(xiàn)廣告卡片3D翻轉(zhuǎn)效果

    在逛網(wǎng)易新聞時(shí),發(fā)現(xiàn)列表中的廣告在你滑動(dòng)的時(shí)候會(huì)有一個(gè)3D旋轉(zhuǎn)的交互引你的注意。本文將利用Flutter實(shí)現(xiàn)這一效果,感興趣的可以了解一下
    2022-04-04
  • Android中簡單的電話管理與短信管理App編寫實(shí)例

    Android中簡單的電話管理與短信管理App編寫實(shí)例

    這篇文章主要介紹了Android中簡單的電話管理與短信管理App編寫實(shí)例,包括監(jiān)聽電話的呼叫狀態(tài)以及短信群發(fā)聯(lián)系人選擇等基本功能的實(shí)現(xiàn),代碼突出要點(diǎn),需要的朋友可以參考下
    2016-04-04
  • flutter ExpansionTile 層級(jí)菜單的實(shí)現(xiàn)

    flutter ExpansionTile 層級(jí)菜單的實(shí)現(xiàn)

    這篇文章主要介紹了flutter ExpansionTile 層級(jí)菜單的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Android實(shí)現(xiàn)音樂播放器鎖屏頁

    Android實(shí)現(xiàn)音樂播放器鎖屏頁

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)音樂播放器鎖屏頁,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12

最新評(píng)論

黄大仙区| 望城县| 乳源| 丹巴县| 锦屏县| 九龙县| 渝中区| 大兴区| 托里县| 信阳市| 和平区| 汉源县| 万源市| 松原市| 阿拉善左旗| 岳阳县| 建平县| 宁阳县| 延安市| 沅陵县| 共和县| 平遥县| 林口县| 祁阳县| 江源县| 吕梁市| 申扎县| 玉田县| 芷江| 连云港市| 津市市| 台南县| 太原市| 黄骅市| 唐海县| 涪陵区| 喀喇沁旗| 五华县| 介休市| 江孜县| 鹤岗市|