适配器及适配器控件
适配器
什么是适配器:把数据转换为适配器控件所能使用的
- SimpleAdapter(可以显示多个控件)
第一个参数填上下文对象
第二个为数据源(而且数据源必须是map对象组成的集合 (list
SimpleAdapter adapter =new SimpleAdapter(content,data,resource,from,to);
- ArrayAdapter(只能显示单一的TextView)
第一个参数填上下文对象
第二个参数填每个item的布局文件(注意ArrayAdapter的item布局文件只能是以TextView作为根节点的 且只能匹配一个TextView)
第三个参数填item中的内容 可以是数组或者集合
ArrayAdapter<String> adapter=new ArrayAdapter<>(content,resouce,data);
- PagerAdapter
必须重写下面的四个方法
- instantiateItem(ViewGroup, int)
- destroyItem(ViewGroup, int, Object)
- getCount()
- isViewFromObject(View, Object)
private class ViewPagerAdapter extends PagerAdapter {
// 获取要滑动的控件的数量,在这里我们以滑动的广告栏为例,那么这里就应该是展示的广告图片的ImageView数量
@Override
public int getCount() {
return images.size();
}
// 来判断显示的是否是同一张图片,这里我们将两个参数相比较返回即可
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
// PagerAdapter只缓存三张要显示的图片,如果滑动的图片超出了缓存的范围,就会调用这个方法,将图片销毁
@Override
public void destroyItem(ViewGroup view, int position, Object object) {
view.removeView(images.get(position));
}
// 当要显示的图片可以进行缓存的时候,会调用这个方法进行显示图片的初始化,我们将要显示的ImageView加入到ViewGroup中,然后作为返回值返回即可
@Override
public Object instantiateItem(ViewGroup view, int position) {
view.addView(images.get(position));
return images.get(position);
}
}
- BaseAdapter(自定义适配器,重点)
1.创建布局文件
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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.cbt.learnbaseadapter.MainActivity">
<ListView
android:id="@+id/lv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
item.xml (ListView中每条信息的显示布局)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv_image"
android:src="@mipmap/ic_launcher"
android:layout_width="60dp"
android:layout_height="60dp"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_toEndOf="@id/iv_image"
android:text="Title"
android:gravity="center"
android:textSize="25sp"/>
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/iv_image"
android:layout_below="@id/tv_title"
android:text="Content"
android:textSize="20sp"/>
</RelativeLayout>
2.创建数据源
ItemBean.java
package com.example.lyj.myapplication;
/**
* Created by caobotao on 15/12/20.
*/
public class ItemBean {
public int itemImageResId;//图像资源ID
public String itemTitle;//标题
public String itemContent;//内容
public ItemBean(int itemImageResId, String itemTitle, String itemContent) {
this.itemImageResId = itemImageResId;
this.itemTitle = itemTitle;
this.itemContent = itemContent;
}
}
MainActivity.java
package com.example.lyj.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView mListView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<ItemBean> itemBeanList = new ArrayList<>();
for (int i = 0;i < 20; i ++){
itemBeanList.add(new ItemBean(R.mipmap.ic_launcher, "标题" + i, "内容" + i));
}
mListView = (ListView) findViewById(R.id.lv_main);
//设置ListView的数据适配器
mListView.setAdapter(new MyAdapter(this,itemBeanList));
}
}
3.创建BaseAdapter
public View getView(int position, View convertView, ViewGroup parent) {//如果view未被实例化过,缓存池中没有对应的缓存
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item,null);
}
/**
* 找到item布局文件中对应的控件
*/
ImageView imageView = (ImageView) convertView.findViewById(R.id.iv_image);
TextView titleTextView = (TextView) convertView.findViewById(R.id.tv_title);
TextView contentTextView = (TextView) convertView.findViewById(R.id.tv_content);
//获取相应索引的ItemBean对象
ItemBean bean = mList.get(position);
/**
* 设置控件的对应属性值
*/
imageView.setImageResource(bean.itemImageResId);
titleTextView.setText(bean.itemTitle);
contentTextView.setText(bean.itemContent);
return convertView;
}
适配器控件
- ListView (列表视图)
方法
addHeaderView(View v) 设置在ListView顶部
addFooterView(View v ) 设置在ListView底部
setAdapter(ListAdapter adapter) 设置ListView的适配器
package com.example.lyj.myapplication;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class ListActivityDemo extends ListActivity {
private String[] presidents={"北京","深圳","济南","广州","海南","香港","澳门"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ListView listview=getListView();
//添加一个TextView作为表头
TextView tvHeader=new TextView(ListActivityDemo.this);
tvHeader.setText("城市列表头");
listview.addHeaderView(tvHeader);
//添加一个TextView作为表尾
TextView tvFooter=new TextView(ListActivityDemo.this);
tvFooter.setText("城市列表尾");
listview.addFooterView(tvFooter);
listview.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,presidents));
}
@Override
protected void onListItemClick(ListView parent, View view, int position, long id) {
Toast.makeText(this, "You have selected "+presidents[position], Toast.LENGTH_SHORT).show();
}
}
效果图:
- GridView(网格视图)
Activity类
public class MainActivity extends Activity {
private GridView gridView;
private List<Map<String, Object>> dataList;
private SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridview);
//初始化数据
initData();
String[] from={"img","text"};
int[] to={R.id.img,R.id.text};
adapter=new SimpleAdapter(this, dataList, R.layout.gridview_item, from, to);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this);
builder.setTitle("提示").setMessage(dataList.get(arg2).get("text").toString()).create().show();
}
});
}
void initData() {
//图标
int icno[] = { R.drawable.i1, R.drawable.i2, R.drawable.i3,
R.drawable.i4, R.drawable.i5, R.drawable.i6, R.drawable.i7,
R.drawable.i8, R.drawable.i9, R.drawable.i10, R.drawable.i11, R.drawable.i12 };
//图标下的文字
String name[]={"时钟","信号","宝箱","秒钟","大象","FF","记事本","书签","印象","商店","主题","迅雷"};
dataList = new ArrayList<Map<String, Object>>();
for (int i = 0; i <icno.length; i++) {
Map<String, Object> map=new HashMap<String, Object>();
map.put("img", icno[i]);
map.put("text",name[i]);
dataList.add(map);
}
}
}
主布局文件activity_main.xml
<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:background="#000"
tools:context="com.example.l7.MainActivity" >
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="80dp"
android:stretchMode="spacingWidthUniform"
android:numColumns="3"
/>
</LinearLayout>
列表项布局gridview_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/img"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_gravity="center"
android:textColor="#FFF"
android:text="文字"
/>
</LinearLayout>
效果图:
- ViewPager(用于实现页面间的切换)
1. ViewPager类直接继承了ViewGroup类,所有它是一个容器类,可以在其中添加其他的view类。
2. ViewPager类需要一个PagerAdapter适配器类给它提供数据。
3. ViewPager经常和Fragment一起使用,并且提供了专门的FragmentPagerAdapter和FragmentStatePagerAdapter类供Fragment中的ViewPager使用。
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/vp_main_viewPage">
</android.support.v4.view.ViewPager>
</LinearLayout>
RelativeLayout.xml页面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:background="#ff0000"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="我是Relative界面"
android:textSize="30sp"
android:gravity="center"/>
</RelativeLayout>
LinearLayout.xml页面
<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"
tools:context="com.zking.administrator.myapplication.MainActivity">
<TextView
android:background="#00ff00"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="我是Linear界面"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>
FrameLayout.xml页面
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:background="#0000ff"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="我是Frame界面"
android:textSize="30sp"
android:gravity="center"/>
</FrameLayout>
Java代码
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2017/6/13.
*/
public class HHActivity extends AppCompatActivity {
private int Layouts[] = {R.layout.activity_main,R.layout.activity_frame,R.layout.activity_relative};
private ViewPager vp_main_viewPage;
private List<View> view_list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hh);
vp_main_viewPage = (ViewPager) findViewById(R.id.vp_main_viewPage);
for (int i = 0; i <Layouts.length ; i++) {
View v = getLayoutInflater().inflate(Layouts[i],null);
view_list.add(v);
}
vp_main_viewPage.setAdapter(new MyAdapter());
}
class MyAdapter extends PagerAdapter{
@Override
public int getCount() {
return Layouts.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View v = view_list.get(position);
container.addView(v);
return v;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View v = view_list.get(position);
container.removeView(v);
}
}
}