文章目录
适配器、实体、条目、意图的含义和之间的关系:
- 页面(.java):当前操作的java活动页面。
- 条目(.xml):每条将要重复生成某项功能的单个模板。
- 实体(.java):数据集合,实现了存储默认数据和传出数据并有构造新函数。。
- 适配器(.java):继承了相关的适配器,重写适配器方法,将实体内数据做一个统计,最后将对象传递给页面的XML节点对象。
-
- 如果要重复实现某个功能,就可以使用适配器。
有哪些常用适配器:
- 数组适配器ArrayAdapter
- 简单适配器SimpleAdapter
- 基础适配器BaseAdapter
- 翻页适配器PagerAdapter
- 碎片适配器FragmentPagerAdapter
一、数组适配器ArrayAdapter
下例演示的是List View控件,还可以使用Spinner
- 页面:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity2 extends AppCompatActivity {
private String[] positList = {"重庆","湖北","四川","贵州","新疆","甘肃","西藏","湖南"};//实体数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//适配器
ArrayAdapter adapter = new ArrayAdapter<>(this,R.layout.itrm_array,positList);
//传递给具体节点
ListView list1 = findViewById(R.id.list1);
list1.setAdapter(adapter);
}
}
- 页面XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
- 条目
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="20dp"
android:textColor="@color/black"
android:gravity="center">
</TextView>
- 数组适配器是简单的适配器,没有之一,但是数据适配器只能做一些单一的文字数据,没有精美的包装盒。
- 实现步骤:
-
- 第一步:创建数据实体。
-
- 第二步:创建条目文件。
-
- 第三步:创建适配器。
-
- 第四步:将适配器传递给具体的节点。
另外,关于条目文件,系统已提供了几种item的样式,一般直接使用系统的即可,不需要自定义了。如:
- android.R.layout.simple_list_item_1:单行的文本,效果如示例一样。
- android.R.layout.simple_list_item_checked:带选择标识的。
- android.R.layout.simple_list_item_multiple_choice:带复选框。
二、简单适配器SimpleAdapter
注意静态文件的文件名和路径
- 页面:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity3 extends AppCompatActivity {
//实体数据
private int[] imgs = {R.drawable.pic_1,R.drawable.pic_1,R.drawable.pic_1,R.drawable.pic_1,};
private String[] tits = {"标题1","标题2","标题3","标题4"};
private String[] strs={"文本信息1","文本信息2","文本信息3","文本信息4",};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
//适配器对象,键名和id必须一一对应
SimpleAdapter adapter =new SimpleAdapter(this,getlist(),R.layout.item_simple,
new String[]{"img","title","strs"},
new int[]{R.id.img_simple,R.id.txt_simple,R.id.txt_simple2});
//操作节点
ListView list2 = findViewById(R.id.list2);
list2.setAdapter(adapter);
}
//适配器将数据集合
private List<? extends Map<String,?>> getlist() {
ArrayList<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < imgs.length; i++) {
Map<String, Object> itme = new HashMap<>();
itme.put("img",imgs[i]);
itme.put("title",tits[i]);
itme.put("strs",strs[i]);
list.add(itme);
}
return list;
}
}
- 页面XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical">
<ListView
android:id="@+id/list2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</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="100dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_simple"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="100dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="7"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/txt_simple"
android:layout_width="match_parent"
android:layout_weight="2"
android:layout_height="0dp"
android:textSize="10dp"/>
<TextView
android:id="@+id/txt_simple2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:textSize="10dp"/>
</LinearLayout>
</LinearLayout>
- 简单适配器才是永远的神,既简单,又好看,没有之一。
- 实现效果图:
三、基础适配器BaseAdapter
(必须在适配器里面继承和重写BaseAdapter抽象类)
- 页面
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import com.example.myapplication.baseAdapter.MyAdapter1;
import com.example.myapplication.bean.MyData;
public class MainActivity4 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
//实例化适配器
MyAdapter1 adapter1 = new MyAdapter1(this, MyData.getList());
//操作页面节点
ListView list3 = findViewById(R.id.list3);
list3.setAdapter(adapter1);
}
}
- 实体
package com.example.myapplication.bean;
import com.example.myapplication.R;
import java.util.ArrayList;
public class MyData {
//数据实体
private static int[] imgs = {R.drawable.pic_1,R.drawable.pic_1,R.drawable.pic_1,R.drawable.pic_1,};
private static String[] tits = {"标题1","标题2","标题3","标题4"};
private static String[] strs={"文本信息1","文本信息2","文本信息3","文本信息4",};
//成员变量
public final int img;
public final String tit;
public final String str;
//构造方法
public MyData(int img, String tit, String str) {
this.img = img;
this.tit = tit;
this.str = str;
}
//得到数据
public static ArrayList<MyData> getList() {
ArrayList<MyData> list = new ArrayList<>();
for (int i = 0; i < imgs.length; i++) {
list.add(new MyData(imgs[i],tits[i],strs[i]));
}
return list;
}
}
- 适配器(必须继承和重写BaseAdapter抽象类)
package com.example.myapplication.baseAdapter;
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.TextView;
import com.example.myapplication.R;
import com.example.myapplication.bean.MyData;
import java.util.ArrayList;
public class MyAdapter1 extends BaseAdapter {
private final Context mContext;
private final ArrayList<MyData> mlist;
public MyAdapter1(Context mContext, ArrayList<MyData> mlist) {
this.mContext = mContext;
this.mlist = mlist;
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View view1= LayoutInflater.from(mContext).inflate(R.layout.itme_base,null,false);
ImageView img_base = view1.findViewById(R.id.img_base);
TextView txt_base = view1.findViewById(R.id.txt_base);
TextView txt_base2 = view1.findViewById(R.id.txt_base2);
img_base.setImageResource(mlist.get(i).img);
txt_base.setText(mlist.get(i).str);
txt_base2.setText(mlist.get(i).tit);
return view1;
}
}
- 页面XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<ListView
android:id="@+id/list3"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
- 这是一个难点,比较重要,后面的适配器原理十分类似,实现的效果可以比简单视频器多,缺点是编写麻烦。
四、翻页适配器PagerAdapter
翻页适配器只在ViewPager上使用
- 页面:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import com.example.myapplication.baseAdapter.MyAdapter2;
import com.example.myapplication.bean.MyData2lbt;
import java.util.ArrayList;
public class MainActivity5 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
//适配器
MyAdapter2 adapter2 = new MyAdapter2(this,MyData2lbt.getData());
//操作节点
ViewPager viewPager1 =findViewById(R.id.viewPager1);
viewPager1.setAdapter(adapter2);
}
}
- 实体
package com.example.myapplication.bean;
import com.example.myapplication.R;
import java.util.ArrayList;
public class MyData2lbt {
public static int[] imgs = {R.drawable.pic_1,R.drawable.pic_1,R.drawable.pic_1,R.drawable.pic_1,};
public int img;
public MyData2lbt(int img) {
this.img = img;
}
public static ArrayList<MyData2lbt> getData() {
ArrayList<MyData2lbt> list = new ArrayList<>();
for (int i = 0; i < imgs.length; i++) {
list.add(new MyData2lbt(imgs[i]));
}
return list;
}
}
- 适配器
package com.example.myapplication.baseAdapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import com.example.myapplication.bean.MyData2lbt;
import java.util.ArrayList;
import java.util.List;
public class MyAdapter2 extends PagerAdapter {
Context mContext;
ArrayList<MyData2lbt> mlist;
List<ImageView> imgs = new ArrayList<>();//保存图片的数据
public MyAdapter2(Context mContext, ArrayList<MyData2lbt> mlist) {
this.mContext = mContext;
this.mlist = mlist;
for (MyData2lbt i:mlist){
ImageView item = new ImageView(mContext);
//创建条目模板
item.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
item.setImageResource(i.img);
imgs.add(item);
}
}
@Override
public int getCount() {
return mlist.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
container.addView(imgs.get(position));
return imgs.get(position);
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(imgs.get(position));
}
}
- 页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
五、碎片适配器FragmentPagerAdapter
1、静态注册
- 作用:封装标签,复用代码。
- 标签必须有name和id属性。
条目:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="20dp">
<ImageView
android:id="@+id/img_fragmeet1"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="100dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="100dp"
android:orientation="vertical"
android:layout_weight="3">
<TextView
android:id="@+id/txt_fragmeet1"
android:layout_width="match_parent"
android:layout_weight="2"
android:layout_height="0dp"/>
<TextView
android:id="@+id/txt_fragmeet2"
android:layout_weight="3"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
</LinearLayout>
适配器:
package com.example.myapplication.fragmeet;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.myapplication.R;
public class fragmeet extends Fragment {
Context mContext;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mContext = getActivity();//获取活动页面的上下文
//得到条目和设置条目上标签的属性
View view = LayoutInflater.from(mContext).inflate(R.layout.item_fragmeet,null,false);
ImageView img_fragmeet1 = view.findViewById(R.id.img_fragmeet1);
TextView txt_fragmeet1 = view.findViewById(R.id.txt_fragmeet1);
TextView txt_fragmeet2 = view.findViewById(R.id.txt_fragmeet2);
img_fragmeet1.setImageResource(R.drawable.pic_1);
txt_fragmeet1.setText("文本");
txt_fragmeet2.setText("文本2");
return view;
}
}
(在活动页面的XML中直接使用)使用:
<fragment
android:id="@+id/frag"
android:name="com.example.myapplication.fragmeet.fragmeet"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginBottom="20dp" />