适配器及其应用

适配器的定义以及使用适配器的几种情况

适配器是沟通数据与视图的桥梁,用于对要进行显示的数据进行处理,并通过与视图对象的绑定从而将数据显示到视图对象中。
Adapter应用场合:ListView以列表形式显示数据,GridView以网格形式显示数据,ViewPager以分页形式显示数据

ListView以列表形式显示数据

我们以一个简单的列表显示数据来看,首先,我们需要一个ListView的布局:

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

    </ListView>

主要功能有:ListView中元素的排序,ListView中元素的分块显示,ListView右侧导航,ListView的单击事件。所以我们需要一个适配器,创建一个ListAdapter继承BaseAdapter父类:

public class ListAdapter extends BaseAdapter {

    private Context context;
    private List<Student>studentList;

    public ListAdapter(Context context, List<Student> studentList) {
        this.context = context;
        this.studentList = studentList;
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v=null;

        v= LayoutInflater.from(context).inflate(R.layout.item_list,null);

        TextView tvname= (TextView) v.findViewById(R.id.tvname);
        TextView tvadress= (TextView) v.findViewById(R.id.tvadress);
        TextView tvage= (TextView) v.findViewById(R.id.tvage);

        tvname.setText(studentList.get(position).getUsername());
        tvadress.setText(studentList.get(position).getAdress());
        tvage.setText(studentList.get(position).getAge());

        return v;
    }
}

在适配器中,我们需要添加一个视图,如上代码中的item_list,用来显示列表排版格式:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/tvname"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/tvadress"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/tvage"/>

</LinearLayout>

我们需要在activity页面中调用适配器,首先,初始化适配器:

private ListView listView;
private List<Student>studentList =new ArrayList<>();

接下来就是适配器的使用:

listView= (ListView) findViewById(R.id.lv_list);

        Student s1=new Student("张三","南昌","20");
        Student s2=new Student("张三","南昌","20");
        Student s3=new Student("张三","南昌","20");
        Student s4=new Student("张三","南昌","20");
        Student s5=new Student("张三","南昌","20");
        Student s6=new Student("张三","南昌","20");

        studentList.add(s1);
        studentList.add(s2);
        studentList.add(s3);
        studentList.add(s4);
        studentList.add(s5);
        studentList.add(s6);

        ListAdapter adapter=new ListAdapter(this,studentList);
        listView.setAdapter(adapter);

附上结果截图:
这里写图片描述

GridView以网格形式显示数据

GridView适配器使用方法与ListView很类似,同意是GridView布局:

<GridView
        android:id="@+id/grd"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:horizontalSpacing="5dp"
        android:numColumns="3"
        android:verticalSpacing="5dp"></GridView>

布局文件:

<?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"
    android:background="#55999999"
    android:gravity="center"
    android:padding="5dp">

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/many"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/img"
        android:textColor="#ffffff"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/many"
        android:textColor="#ffffff"
        android:textSize="15sp" />

</RelativeLayout>

适配器代码与上面的适配器基本差不多:

public class GridAdapter extends BaseAdapter {

    private Context context;
    private List<Menugride>menugrideList;
    public GridAdapter(Context context,List<Menugride>menugrideList){
        this.context=context;
        this.menugrideList=menugrideList;
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view=null;

        view= LayoutInflater.from(context).inflate(R.layout.menu_item,null);

        ImageView img= (ImageView) view.findViewById(R.id.img);
        TextView many= (TextView) view.findViewById(R.id.many);
        TextView name= (TextView) view.findViewById(R.id.name);

        img.setImageResource(menugrideList.get(position).getImg());
        many.setText(menugrideList.get(position).getMany()+"");
        name.setText(menugrideList.get(position).getName());

        return view;
    }
}

接下来是activity代码:

public class GridViewActivity extends AppCompatActivity {
    private GridView gridView;
    private List<Menugride>menugrideList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_view);

        gridView= (GridView) findViewById(R.id.grd);

        Menugride m1=new Menugride(R.mipmap.icon_local_music,3,"我的音乐");
        Menugride m2=new Menugride(R.mipmap.icon_favorites,4,"我的最爱");
        Menugride m3=new Menugride(R.mipmap.icon_folder_plus,4,"文件夹");
        Menugride m4=new Menugride(R.mipmap.icon_artist_plus,4,"歌手");
        Menugride m5=new Menugride(R.mipmap.icon_list_item_sort,5,"风格");
        Menugride m6=new Menugride(R.mipmap.icon_album_plus,8,"专辑");
        Menugride m7=new Menugride(R.mipmap.icon_download,9,"我的下载");
        Menugride m8=new Menugride(R.mipmap.icon_library_music_circle,7,"音乐圈");
        Menugride m9=new Menugride(R.mipmap.icon_home_custom,8,"定制首页");
        menugrideList.add(m1);
        menugrideList.add(m2);
        menugrideList.add(m3);
        menugrideList.add(m4);
        menugrideList.add(m5);
        menugrideList.add(m6);
        menugrideList.add(m7);
        menugrideList.add(m8);
        menugrideList.add(m9);

        GridAdapter adapter=new GridAdapter(this,menugrideList);
        gridView.setAdapter(adapter);

    }
}

结果截图:
这里写图片描述

ViewPager以分页形式显示数据

ViewPager用以实现activity的滑动,需要使用到适配器,同时,还需要fragment碎片,先创建两个fragment碎片:

<FrameLayout 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.example.asus.myapplication.fragment.FragmentA">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="大笨妞" />

</FrameLayout>
<FrameLayout 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.example.asus.myapplication.fragment.FragmentB">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="大傻妞" />

</FrameLayout>

布局页面中设置ViewPager标签:

<android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/view_pager">

    </android.support.v4.view.ViewPager>

ViewPager的简易适配器:

public class FragmentAdapter extends FragmentPagerAdapter {

    private List<Fragment>fragmentList;

    public FragmentAdapter(FragmentManager fm,List<Fragment>fragmentList) {
        super(fm);
        this.fragmentList=fragmentList;
    }

    @Override
    public Fragment getItem(int position) {
        return fragmentList.get(position);
    }

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

在activity中的java代码以及适配器的调用:

public class ViewPagerActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private List<Fragment>fragmentList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);

        viewPager= (ViewPager) findViewById(R.id.view_pager);

        FragmentA a=new FragmentA();
        FragmentB b=new FragmentB();
        fragmentList.add(a);
        fragmentList.add(b);

        FragmentAdapter fragmentAdapter=new FragmentAdapter(getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(fragmentAdapter);
    }
}

从上面可以总结出:

Adapter工作原理的理解:对于将要显示在AdapterLayout中的数据Data,Adapter会将数据存放在每个Item中,Item由存放对应类型的View控件来承载,最后Adapter将每个View控件显示在AdapterLayout中,从而实现了数据的显示。通俗来讲:如果将Adapter的工作看成是包饺子,那么Data数据就相当于是饺子馅,对应Adapter中的每个Item对象,而承载Item对象的View控件相当于饺子皮,Adapter相当于包饺子的人,负责数据的包装,最后将数据呈现在AdapterLayout中,AdapterLayout相当于是盛放饺子的盘子。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值