Android开发 - (适配器)Adapter类中BaseAdapter实现类详细解析
<h1 id="简介">简介</h1><ul>
<li><strong>提供自定义 Adapter 的基本实现,是其他自定义 Adapter 的基类</strong></li>
</ul>
<h1 id="具体作用">具体作用</h1>
<ul>
<li>
<p><strong>BaseAdapter</strong> 是 <strong>Android 开发</strong>中一个非常重要的<strong>Adapter(适配器)基类</strong>。它提供了<strong>创建自定义适配器的基本实现</strong>,使开发者可以根据具体需求创建<strong>适用于不同视图(如 ListView、GridView)</strong>的数据适配器。以下是 <strong>BaseAdapter 的主要作用</strong>:</p>
<h2 id="提供基本接口实现">提供基本接口实现</h2>
<ul>
<li><strong>BaseAdapter 实现了 ListAdapter 和 SpinnerAdapter 接口,因此适用于 ListView、GridView、Spinner 等组件</strong></li>
</ul>
<h2 id="自定义视图"><strong>自定义视图</strong>:</h2>
<ul>
<li><strong>允许开发者创建自定义的视图来展示数据项,而不是使用简单的内置视图</strong></li>
</ul>
<h2 id="支持复杂布局"><strong>支持复杂布局</strong>:</h2>
<ul>
<li><strong>可以使用自定义布局文件,使每个数据项的展示更加灵活和多样化</strong></li>
</ul>
<h2 id="数据管理"><strong>数据管理</strong>:</h2>
<ul>
<li><strong>负责管理数据集,并在数据发生变化时通知视图进行更新</strong></li>
</ul>
</li>
</ul>
<h1 id="参数方法解析">参数、方法解析</h1>
<ul>
<li>
<p><strong><code>BaseAdapter(Context context, List<T> data)</code></strong>:构造方法;<strong>BaseAdapter</strong>是一个抽象类,它必须自定义一个子类才可以实例化</p>
<pre><code class="language-java">import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private Context context;
private List<String> data;
// 构造方法
public MyAdapter(Context context, List<String> data) {
this.context = context;
this.data = data;
}
@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; // 返回数据项的 ID,通常用位置作为 ID
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
// 如果 convertView 为空,则需要创建新的视图
convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}
// 查找视图中的 TextView
TextView textView = convertView.findViewById(R.id.textView);
// 设置 TextView 的文本为数据项
textView.setText((String) getItem(position));
return convertView; // 返回视图
}
}
//'Arrays.asList'用于将一组对象转化为一个 List。这个方法返回一个固定大小的 List,即列表的大小不能被改变,但你可以对其进行读写操作
List<String> data = Arrays.asList("Item 1", "Item 2", "Item 3");
//实例化
MyAdapter adapter = new MyAdapter(this, data);
</code></pre>
<ul>
<li><strong>参数解析</strong>:
<ul>
<li><strong>context</strong>:<strong>用于访问应用的资源、布局和其他应用环境相关的信息</strong>。在构造方法中传入,用于在适配器类中使用,如在 <strong>LayoutInflater</strong> 中创建视图时需要<strong>上下文对象</strong></li>
<li><strong>data</strong>:<strong>数据源</strong>,<strong>包含需要在列表中显示的数据项</strong>。在构造方法中传入,<strong>用于在适配器类中获取和绑定数据</strong></li>
</ul>
</li>
</ul>
</li>
<li>
<p><strong><code>adapter.getCount()</code></strong>:<strong>返回数据源中项目的总数</strong></p>
</li>
<li>
<p><strong><code>adapter.getItem(int position)</code></strong>:获取<strong>指定位置的数据项</strong></p>
<pre><code class="language-java">int mItem = adapter.getItem(0);
</code></pre>
<ul>
<li><strong>参数解析</strong>:
<ul>
<li><strong>position</strong>:要获取的<strong>项的位置(从 0 开始的索引)</strong></li>
</ul>
</li>
</ul>
</li>
<li>
<p><strong><code>adapter.getItemId(int position)</code></strong>:获取<strong>指定位置数据项的 ID</strong></p>
<pre><code class="language-java">int mItem = adapter.getItemId(0); // 通常返回位置本身作为 ID
</code></pre>
<ul>
<li><strong>参数解析</strong>:
<ul>
<li><strong>position</strong>:要获取<strong>项 ID 的位置(从 0 开始的索引)</strong></li>
</ul>
</li>
</ul>
</li>
<li>
<p><strong><code>adapter.getView(int position, View convertView, ViewGroup parent)</code></strong>:<strong>创建并返回一个表示数据项的视图</strong></p>
<pre><code class="language-java">ListView listView = view.findViewById(android.R.id.listView_1);
View view = adapter.getView(0, null, listView);
</code></pre>
<ul>
<li><strong>参数解析</strong>:
<ul>
<li><strong>position</strong>:<strong>要显示的数据项的位置(从 0 开始的索引)</strong></li>
<li><strong>convertView</strong>:<strong>可重用的视图</strong>,如果该视图不为空,则可以<strong>重用以提高性能(通常是上次使用的视图)</strong></li>
<li><strong>parent</strong>:<strong>ListView</strong> 或 <strong>GridView</strong> 的父视图,用于<strong>生成新的视图</strong></li>
</ul>
</li>
</ul>
</li>
<li>
<p><strong><code>adapter.notifyDataSetChanged()</code></strong>:<strong>通知适配器数据已发生更改,要求视图更新</strong></p>
</li>
</ul>
<h1 id="使用环境解析">使用环境解析</h1>
<h2 id="listview">ListView</h2>
<ul>
<li>
<p><strong>用途</strong>:<strong>显示滚动的列表项</strong>,例如<strong>消息列表</strong>、<strong>联系人列表</strong>等</p>
</li>
<li>
<p><strong>BaseAdapter适用性</strong>:适合用来<strong>创建自定义列表项视图</strong>,<strong>将数据源与 ListView 绑定</strong>。例如,<strong>展示自定义布局的联系人信息或产品列表</strong></p>
</li>
<li>
<p><strong>示例</strong>:如果你需要<strong>显示一个包含图片和文字的复杂列表项</strong>,你<strong>可以使用 BaseAdapter 自定义适配器</strong>来实现</p>
<ul>
<li>
<p><strong>启动类中使用它</strong></p>
<pre><code class="language-java">ListView listView = findViewById(R.id.listView);
MyAdapter adapter = new MyAdapter(this, data);
listView.setAdapter(adapter);
</code></pre>
</li>
</ul>
</li>
</ul>
<h2 id="gridview">GridView</h2>
<ul>
<li>
<p><strong>用途</strong>:显示<strong>网格状的项</strong>,例如<strong>图片库</strong>、<strong>应用图标</strong>等</p>
</li>
<li>
<p><strong>BaseAdapter 适用性</strong>:用于<strong>显示具有相同布局但不同内容的网格项</strong>。例如,<strong>创建一个自定义的图片网格</strong></p>
</li>
<li>
<p><strong>示例</strong>:如果你<strong>有一个包含图片和标题的网格</strong>,<strong>BaseAdapter 可以帮助你创建自定义网格项视图</strong></p>
<ul>
<li>
<p><strong>启动类中使用它</strong></p>
<pre><code class="language-java">GridView gridView = findViewById(R.id.gridView);
MyAdapter adapter = new MyAdapter(this, data);
gridView.setAdapter(adapter);
</code></pre>
</li>
</ul>
</li>
</ul>
<h2 id="spinner">Spinner</h2>
<ul>
<li>
<p><strong>用途</strong>:<strong>显示下拉选择菜单</strong>,例如<strong>选择国家</strong>、<strong>日期</strong>等。</p>
</li>
<li>
<p><strong>BaseAdapter 适用性</strong>:用于<strong>自定义下拉列表项的显示</strong>。例如,<strong>你可以自定义下拉项的布局和数据展示方式</strong></p>
</li>
<li>
<p><strong>示例</strong>:<strong>如果需要在 Spinner 中显示自定义的布局或数据格式</strong>,<strong>BaseAdapter</strong> 是一个合适的选择</p>
<ul>
<li>
<p><strong>启动类中使用它</strong></p>
<pre><code class="language-java">Spinner spinner = findViewById(R.id.spinner);
MyAdapter adapter = new MyAdapter(this, data);
spinner.setAdapter(adapter);
</code></pre>
</li>
</ul>
</li>
</ul>
<h2 id="autocompletetextview">AutoCompleteTextView</h2>
<ul>
<li>
<p><strong>用途</strong>:<strong>提供自动完成的文本输入框</strong>,例如<strong>搜索建议或输入提示</strong></p>
</li>
<li>
<p><strong>BaseAdapter 适用性</strong>:<strong>自定义自动完成项的显示方式和数据源</strong>。例如,<strong>显示带有图片和文字的搜索建议</strong></p>
</li>
<li>
<p><strong>示例</strong>:<strong>如果你需要在 AutoCompleteTextView 中显示复杂的数据项或自定义视图</strong>,可以使用 <strong>BaseAdapter</strong></p>
<ul>
<li>
<p><strong>启动类中使用它</strong></p>
<pre><code class="language-java">AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoCompleteTextView);
MyAdapter adapter = new MyAdapter(this, data);
autoCompleteTextView.setAdapter(adapter);
</code></pre>
</li>
</ul>
</li>
</ul>
<h2 id="总结">总结</h2>
<ul>
<li><strong>BaseAdapter 是用于创建自定义适配器的基类</strong>,适用于<strong>多种 AdapterView</strong>,如 <strong>ListView</strong>、<strong>GridView</strong>、<strong>Spinner</strong> 和 <strong>AutoCompleteTextView</strong>。通过<strong>继承 BaseAdapter 并实现其方法</strong>,你可以<strong>控制如何将数据项绑定到视图中</strong>,从而<strong>实现灵活的数据展示和用户界面自定义</strong>。</li>
</ul>
<h1 id="完整代码示例">完整代码示例</h1>
<ul>
<li>
<p>创建<strong>启动类布局文件<code>res/layout/activity_main.xml</code></strong>,包含一个 <strong>ListView 控件</strong></p>
<pre><code class="language-xml"><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="1dp"
android:divider="@android:color/darker_gray"/>
</RelativeLayout>
</code></pre>
</li>
<li>
<p>创建<strong>布局文件<code>res/layout/list_item.xml</code></strong>,包含一个 <strong>TextView控件</strong></p>
<pre><code class="language-java"><?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="vertical"
android:padding="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"/>
</LinearLayout>
</code></pre>
</li>
<li>
<p><strong>创建一个继承自 BaseAdapter 的自定义适配器类</strong>:<code>MyAdapter.java</code></p>
<pre><code class="language-java">import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private Context context;
private List<String> data;
// 构造方法
public MyAdapter(Context context, List<String> data) {
this.context = context;
this.data = data;
}
@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; // 返回数据项的 ID,通常用位置作为 ID
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
// 如果 convertView 为空,则需要创建新的视图
convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}
// 查找视图中的 TextView
TextView textView = convertView.findViewById(R.id.textView);
// 设置 TextView 的文本为数据项
textView.setText((String) getItem(position));
return convertView; // 返回视图
}
}
</code></pre>
</li>
<li>
<p>在<strong>启动类</strong>中使用<strong>自定义适配器<code>MainActivity.java</code></strong>,在 <strong>MainActivity</strong> 中<strong>实例化自定义适配器</strong>,并将其<strong>设置到 ListView 中</strong></p>
<pre><code class="language-java">import android.os.Bundle;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化数据
List<String> data = Arrays.asList("Item 1", "Item 2", "Item 3");
// 创建自定义适配器MyAdapter
MyAdapter adapter = new MyAdapter(this, data);
// 获取 ListView 并设置适配器
ListView listView = findViewById(R.id.listView);
listView.setAdapter(adapter);
}
}
</code></pre>
</li>
<li>
<p>运行该应用将看到一个 <strong>ListView</strong>,其中<strong>每个列表项显示初始化数据中的字符串</strong>。该应用<strong>使用 BaseAdapter 来自定义适配器</strong>,并将其<strong>绑定到 ListView 中</strong>以显示数据。这个示例<strong>展示了如何创建和使用 BaseAdapter</strong>,<strong>并在实际开发中可以根据需要进行扩展和定制</strong></p>
</li>
</ul><br><br>
来源:https://www.cnblogs.com/ajunjava/p/18336590
頁:
[1]