亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android適配器(Adapter)的概念與自定義

 更新時(shí)間:2021年07月12日 14:33:45   作者:Z|Star  
這篇文章主要給大家介紹了關(guān)于Android適配器(Adapter)的相關(guān)資料,適配器是一個(gè)非常重要的知識(shí)點(diǎn),Adapter是用來(lái)幫助填出數(shù)據(jù)的中間橋梁,本文介紹的非常詳細(xì),需要的朋友可以參考下

一、什么是適配器

在常見(jiàn)軟件中,往往能看到列表一樣的信息流,例如:

如果在xml中將信息寫(xiě)死,那顯然是不合適的,無(wú)法做到上拉刷新的效果。

這個(gè)時(shí)候,就需要用到適配器。

Android中有很多的適配器,首先看看這些適配器的繼承結(jié)構(gòu)

這些適配器中,BaseAdapter用的最多,也用的最熟

二、Adapter基本概念和繼承關(guān)系

三、自定義適配器實(shí)例

1.文件結(jié)構(gòu)

2.xml內(nèi)容

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listview"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">
</ListView>

listview_item.xml:

<?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="100dp"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="15dp">
    <ImageView
        android:id="@+id/img_icon"
        android:layout_width="130dp"
        android:layout_height="80dp"
        android:src="@mipmap/news"/>
    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_weight="1">
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="我是一個(gè)新聞標(biāo)題---- 1"
            android:textColor="#000000"
            android:textSize="18dp" />
        <TextView
            android:id="@+id/tv_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="我是新聞內(nèi)容---- 1"
            android:textColor="#000000"
            android:textSize="14dp" />
    </RelativeLayout>
</LinearLayout>

3.java內(nèi)容

MainActivity

package cn.edu.cdut.testadapter;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.LinkedList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<News> mData = null;
    private Context mContext;
    private NewsAdapter mAdapter = null;
    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        listView = (ListView) findViewById(R.id.listview);
        mData = new LinkedList<News>();
        for (int i = 0; i < 10; i++) {
            mData.add(new News("我是一個(gè)新聞標(biāo)題---- " + i, "我是一個(gè)新聞內(nèi)容---- " + i, R.mipmap.news));
        }
        mAdapter = new NewsAdapter(mData, mContext);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int
                    position, long id) {
                Toast.makeText(mContext, "點(diǎn)擊了第" + position + "條數(shù)據(jù)",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}

News.java

package cn.edu.cdut.testadapter;

public class News {
    private String title;
    private String content;
    private int aIcon;
    public News() {
    }
    public News(String title, String content, int aIcon) {
        this.title = title;
        this.content = content;
        this.aIcon = aIcon;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public int getaIcon() {
        return aIcon;
    }
    public void setaIcon(int aIcon) {
        this.aIcon = aIcon;
    }
}

NewsAdapter

package cn.edu.cdut.testadapter;

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.TextView;

import java.util.List;

public class NewsAdapter extends BaseAdapter {
    private List<News> mData;
    private Context mContext;
    public NewsAdapter(List<News> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
    }
    @Override
    public int getCount() {
        return mData.size();
    }
    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView =
                LayoutInflater.from(mContext).inflate(R.layout.listview_item,
                        parent, false);
        ImageView img_icon = (ImageView)
                convertView.findViewById(R.id.img_icon);
        TextView title = (TextView)
                convertView.findViewById(R.id.tv_title);
        TextView content = (TextView)
                convertView.findViewById(R.id.tv_content);
        img_icon.setBackgroundResource(mData.get(position).getaIcon());
        title.setText(mData.get(position).getTitle());
        content.setText(mData.get(position).getContent());
        return convertView;
    }
}

四、參考資料

https://gitee.com/hwdroid/HelloWorld

總結(jié)

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

相關(guān)文章

  • AndroidHttpClient詳解及調(diào)用示例

    AndroidHttpClient詳解及調(diào)用示例

    本文給大家介紹AndroidHttpClient結(jié)構(gòu)、使用方式及調(diào)用示例詳解,需要的朋友可以參考下
    2015-10-10
  • Kotlin掛起函數(shù)原理示例剖析

    Kotlin掛起函數(shù)原理示例剖析

    這篇文章主要為大家介紹了Kotlin掛起函數(shù)的原理示例剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Android應(yīng)用隱私合規(guī)檢測(cè)實(shí)現(xiàn)方案詳解

    Android應(yīng)用隱私合規(guī)檢測(cè)實(shí)現(xiàn)方案詳解

    這篇文章主要介紹了Android應(yīng)用隱私合規(guī)檢測(cè)實(shí)現(xiàn)方案,我們需要做的就是提前檢測(cè)好自己的應(yīng)用是否存在隱私合規(guī)問(wèn)題,及時(shí)整改過(guò)來(lái),下面提供Xposed Hook思路去檢測(cè)隱私合規(guī)問(wèn)題,建議有Xposed基礎(chǔ)的童鞋閱讀,需要的朋友可以參考下
    2022-07-07
  • Android7.0開(kāi)發(fā)實(shí)現(xiàn)Launcher3去掉應(yīng)用抽屜的方法詳解

    Android7.0開(kāi)發(fā)實(shí)現(xiàn)Launcher3去掉應(yīng)用抽屜的方法詳解

    這篇文章主要介紹了Android7.0開(kāi)發(fā)實(shí)現(xiàn)Launcher3去掉應(yīng)用抽屜的方法,結(jié)合實(shí)例形式分析了Android7.0 Launcher3調(diào)整界面布局的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2017-11-11
  • Android實(shí)用編程技巧代碼總結(jié)

    Android實(shí)用編程技巧代碼總結(jié)

    這篇文章主要介紹了Android實(shí)用編程技巧代碼,總結(jié)分析了Android編程中常見(jiàn)的實(shí)用代碼段,包括圖片、文本、控件等常用操作技巧,需要的朋友可以參考下
    2016-10-10
  • Android發(fā)送xml數(shù)據(jù)給服務(wù)器的方法

    Android發(fā)送xml數(shù)據(jù)給服務(wù)器的方法

    這篇文章主要介紹了Android發(fā)送xml數(shù)據(jù)給服務(wù)器的方法,以實(shí)例形式較為詳細(xì)的分析了Android發(fā)送XML數(shù)據(jù)及接收XML數(shù)據(jù)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-09-09
  • Android Bitmap詳細(xì)介紹

    Android Bitmap詳細(xì)介紹

    Android中Bitmap的常見(jiàn)操作整理一覽,需要的朋友可以參考下
    2012-12-12
  • Android使用插件實(shí)現(xiàn)代碼混淆

    Android使用插件實(shí)現(xiàn)代碼混淆

    這篇文章主要介紹了Android如何使用插件實(shí)現(xiàn)代碼混淆,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-03-03
  • 總結(jié)Android中多線程更新應(yīng)用的頁(yè)面信息的方式

    總結(jié)Android中多線程更新應(yīng)用的頁(yè)面信息的方式

    這篇文章主要介紹了總結(jié)Android中多線程更新應(yīng)用的頁(yè)面信息的方式,文中共總結(jié)了runOnUiThread、Handler、AsyncTask異步以及View直接在UI線程中更新的方法,需要的朋友可以參考下
    2016-02-02
  • Andriod?Studio實(shí)現(xiàn)撥打電話和發(fā)送短信的示例代碼

    Andriod?Studio實(shí)現(xiàn)撥打電話和發(fā)送短信的示例代碼

    這篇文章主要介紹了Andriod?Studio實(shí)現(xiàn)撥打電話和發(fā)送短信功能,Android?Studio中創(chuàng)建項(xiàng)目,然后在該項(xiàng)目中創(chuàng)建一個(gè)Module名稱(chēng)為“IntentDial”,文章結(jié)合實(shí)例步驟給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2022-03-03

最新評(píng)論