Android 開(kāi)發(fā)技術(shù) 第四課

概要

這節(jié)課我們講了ListView自定義Adapter的方法,只要繼承BaseAdapter實(shí)現(xiàn)我們自己的類(lèi)就可以了。我們還講了使用HTTP協(xié)議連接網(wǎng)絡(luò)的方法,用HttpURLConnection即可。下面就給大家說(shuō)說(shuō)它們的詳細(xì)用法。

BaseAdapter的使用方法

在上節(jié)課中,我們學(xué)到了ArrayAdapterSimpleAdapter,但是這兩個(gè)Adapter不夠靈活,前者只能改變item中的一條文字,后者則能改變item中的多個(gè)圖片,多條文字。但是這樣的功能還是太簡(jiǎn)單,好多情況并不能滿足我們的需求。比如,我們要實(shí)現(xiàn)一個(gè)列表,每行之間的背景色都不同,這樣的需求雖然簡(jiǎn)單,但是使用上述的兩個(gè)Adapter,我們卻并不能很好的實(shí)現(xiàn)。
這時(shí)候,BaseAdapter就登場(chǎng)了。從名字可以看出,他是一個(gè)基本Adapter,里面關(guān)于顯示item的東西都要我們自己實(shí)現(xiàn)。使用BaseAdapter大致需要如下幾步

  1. 創(chuàng)建一個(gè)類(lèi),并繼承BaseAdapter
    創(chuàng)建完,我們會(huì)發(fā)現(xiàn),竟然出現(xiàn)錯(cuò)誤了,怎么回事?因?yàn)?code>BaseAdapter實(shí)現(xiàn)了Adapter接口,所以我們要實(shí)現(xiàn)接口定義的方法。在紅線上按Alt+Enter,選擇Implement Methods,全選所有方法,按OK即可。
  2. 幾個(gè)重寫(xiě)的方法的介紹
  • public int getCount()
    返回?cái)?shù)據(jù)的個(gè)數(shù),或者說(shuō)ListView中,item的個(gè)數(shù)。通常寫(xiě)return list.size();
  • public Object getItem(int position)
    返回item所對(duì)應(yīng)的數(shù)據(jù),通常寫(xiě)return list.get(position);
  • public long getItemId(int position)
    返回item對(duì)用的id,通常寫(xiě)return position;
  • public View getView(int position, View convertView, ViewGroup parent)
    這個(gè)是我們最關(guān)注的方法,在這里我們返回ListView中每個(gè)item對(duì)應(yīng)的View。
  1. 通常需要設(shè)置的類(lèi)內(nèi)變量
    不知道大家發(fā)現(xiàn)沒(méi)有,我們自定義的Adapter并沒(méi)有數(shù)據(jù)源,這怎么辦呢,當(dāng)然是給它當(dāng)變量傳進(jìn)來(lái)啊。
    不過(guò),還有一個(gè)問(wèn)題需要解決,在getView()方法中,我們?cè)趺捶祷豓iew呢,我們只是創(chuàng)建了item對(duì)應(yīng)的布局(layout)啊。這時(shí)候,LayoutInflater這個(gè)類(lèi)就出場(chǎng)了,它可以將我們的布局文件(xxx.xml)轉(zhuǎn)換成對(duì)應(yīng)的View。這么說(shuō),大家可能不太理解,大家可以回憶一下,我們創(chuàng)建的Activity是怎樣顯示的布局文件呢。有人可能會(huì)說(shuō)了,我怎么知道,我根本沒(méi)關(guān)過(guò)它,它自己就顯示出來(lái)了。其實(shí)因?yàn)槲覀冃陆üこ虝r(shí),Android Studio已經(jīng)給我們生成好代碼了,那就是在onCreate()函數(shù)中的setContentView(R.layout.activity_main)方法,其實(shí)它的內(nèi)部就是使用LayoutInflater,把我們的布局文件,轉(zhuǎn)化成對(duì)應(yīng)的View并顯示出來(lái)的。那么具體怎么用呢,我們使用這句話就好了。
View view = LayoutInflater.from(context).inflate(R.layout.xxx, null);

我們可以看出,在自定義的Adapter中,并沒(méi)有Context對(duì)象,所以我們也把它當(dāng)參數(shù)傳進(jìn)來(lái)就哦了。那個(gè)null是什么呢,那個(gè)參數(shù)要為返回的view指定一個(gè)父布局Parent View。我們不用管它,傳null就好了。

  1. 具體演示
    引用一句比較火的話

Talk is cheap. Show me the code

布局文件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="wrap_content"
    android:orientation="horizontal"
    android:padding="16dp">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/head1" />

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:text="周杰倫"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:text="龍卷風(fēng)"
        android:textSize="20sp" />
</LinearLayout>

布局文件activity_main.xml

<?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"
    android:orientation="vertical"
    tools:context="com.sunny.muke_scrollview1.MainActivity">

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

實(shí)體類(lèi)Song.java,可以理解為存放歌曲信息的數(shù)據(jù)結(jié)構(gòu)

package com.sunny.muke_scrollview1;
/**
 * 歌曲實(shí)體類(lèi)
 * Created by Sunny  An on 2016/5/29.
 */
public class Song {
    //歌曲名
    private String songName;
    //歌手名
    private String singer;
    //圖片
    private int picture;

    public String getSongName() {
        return songName;
    }

    public void setSongName(String songName) {
        this.songName = songName;
    }

    public String getSinger() {
        return singer;
    }

    public void setSinger(String singer) {
        this.singer = singer;
    }

    public int getPicture() {
        return picture;
    }

    public void setPicture(int picture) {
        this.picture = picture;
    }
}

自定義的適配器CustomAdapter.java

package com.sunny.muke_scrollview1;
import android.content.Context;
import android.graphics.Color;
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;
/**
 * 自定義Adapter
 * Created by Sunny  An on 2016/5/29.
 */
public class CustomAdapter extends BaseAdapter {
    private List<Song> data;
    private Context context;
    private LayoutInflater inflater;

    public CustomAdapter(List<Song> data, Context context) {
        this.data = data;
        this.context = context;
        inflater = LayoutInflater.from(this.context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //將布局文件轉(zhuǎn)化成View
        View view = inflater.inflate(R.layout.item, null);
        //找控件
        TextView tvSongName = (TextView) view.findViewById(R.id.tv1);
        TextView tvSinger = (TextView) view.findViewById(R.id.tv2);
        ImageView ivPic = (ImageView) view.findViewById(R.id.iv);
        //設(shè)置數(shù)據(jù)
        Song song = data.get(position);
        tvSongName.setText(song.getSongName());
        tvSinger.setText(song.getSinger());
        ivPic.setBackgroundResource(song.getPicture());
        //設(shè)置背景
        if (position % 2 == 0) {
            view.setBackgroundColor(Color.LTGRAY);
        } else {
            view.setBackgroundColor(Color.DKGRAY);
        }
        return view;
    }
}

MainActivity.java中的初始化代碼

List<Song> data = new ArrayList<>();
for (int i = 0; i < 100; i++) {
    Song song = new Song();
    song.setSinger("歌手" + i);
    song.setSongName("歌曲" + i);
    if (i % 2 == 0) {
        song.setPicture(R.drawable.head1);
    } else {
        song.setPicture(R.drawable.head2);
    }
    data.add(song);
}
ListView listView = (ListView) findViewById(R.id.listView);
CustomAdapter adapter = new CustomAdapter(data, MainActivity.this);
listView.setAdapter(adapter);
  1. 最終效果
    效果圖

    實(shí)現(xiàn)了,有木有。但是,這個(gè)LIstView在性能上來(lái)說(shuō),并不是最優(yōu)的,更好的使用方法,請(qǐng)參見(jiàn)文末的鏈接。

HttpURLConnection的使用方法

這個(gè)我就不說(shuō)太多了,都是Java的知識(shí),大家看看書(shū),或者看文末鏈接吧,我就把代碼貼出來(lái)了。需要注意的是,要在AndroidManifest.xml中加入網(wǎng)絡(luò)權(quán)限,否則應(yīng)用不能聯(lián)網(wǎng)。同時(shí),使用HttpURLConnection訪問(wèn)網(wǎng)絡(luò)的操作不能放到UI線程中,請(qǐng)創(chuàng)建非UI線程。

<uses-permission android:name="android.permission.INTERNET" />

使用GET方式訪問(wèn)百度頁(yè)面

new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            URL url = new URL("https://m.baidu.com/");
            //返回值是URLConnection,我們要做一次強(qiáng)制轉(zhuǎn)換
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //設(shè)置連接超時(shí)時(shí)間5秒
            connection.setConnectTimeout(5 * 1000);
            //設(shè)置請(qǐng)求方式為GET
            connection.setRequestMethod("GET");
            //包裝輸入流
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line;
            //獲取所有文本
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            //把他打印出來(lái)
            Log.v("result",sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}).start();

運(yùn)行結(jié)果


補(bǔ)充鏈接

Java中entity(實(shí)體類(lèi))的寫(xiě)法規(guī)范
BaseAdapter的逗逼、普通、文藝寫(xiě)法
LayoutInflater的獲取與使用
Java Thread 總結(jié)
HTTP協(xié)議的介紹
HttpURLConnection在 Java 幫助文檔 中的介紹
Java輸入輸出流

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容