Android开发笔记之Adapter用法

Adapter的概念

适配器对象作为视图和该视图的基础数据之间的桥梁。适配器提供对数据项的访问。适配器还负责对数据集中的每个项目进行视图的视图。

三个要素:

Data—连接—Adapter—连接—View

Adapter接口的继承关系如下图:


重写 getView()方法的工作原理:
1.ListView 向 Adapter 对于List中的每一条数据索要一个View ,“give me a view” (getView)。
2.一个新的View 返回并且展示。 



但是出现下面的情况,系统内存无法满足, *ListView里面的条目过多,例如 ,100000万条。其中只有可见的项目存在内存中,其他的在Recycler中。其实我的理解Recyler就是一个队列,用来存储不在屏幕范围内的item,如果item滚出屏幕范围,那么就入队,这里的滚出是完全滚出,即边界等也要完全滚出。如果新的item要滚进来,那么android系统的framework就会查看Recyler是否含有可以重复使用的iew,如果有那么就重新设置该iew 的数据源,然后显示,即出队。

常用的Adapter实现的类有:

(1)ArrayAdapter,支持泛型,可以展示一行字符串。

ArrayAdapterDemo:

使用系统自带的android.R.layout.simple_list_item_1布局文件。

MainActivity.java文件

package edu.jnu.adapterdemo;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {

    private ListView listView;
    private List<String> list;
    private ArrayAdapter arrayAdapter;


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

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

        list = new ArrayList<String>();

        for (int i = 1; i < 100; i++) {
            list.add("item" + i);
        }

        arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,list);
        listView.setAdapter(arrayAdapter);

    }


}



(2).SimpleAdapter可以添加各种各样的组件,灵活易用。


lv_layout.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView
        android:id="@+id/img"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="5dp"

        />
    <LinearLayout

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="22sp"
            />
        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"/>

    </LinearLayout>


</LinearLayout>

MainActivity.java文件

package edu.jnu.adapterdemo;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity implements AdapterView.OnItemClickListener {

    private ListView listView;
    private List<String> list;
    private SimpleAdapter simpleAdapter;


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

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

        list = new ArrayList<String>();

        for (int i = 1; i < 100; i++) {
            list.add("item" + i);
        }
        simpleAdapter = new SimpleAdapter(this,getdata(),R.layout.lv_layout,
                new String[]{"title","info","image"},
                new int[]{R.id.text1,R.id.text2,R.id.img});

        listView.setAdapter(simpleAdapter);
        listView.setOnItemClickListener(this);

    }

    private List<Map<String,Object>> getdata(){
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("title","Title1");
        map.put("info","Info1");
        map.put("image",R.drawable.i1);
        list.add(map);
        map = new HashMap<String,Object>();
        map.put("title","Title2");
        map.put("info","Info2");
        map.put("image",R.drawable.i1);
        list.add(map);
        map = new HashMap<String,Object>();
        map.put("title","Title3");
        map.put("info","Info3");
        map.put("image",R.drawable.i1);
        list.add(map);



        return list;
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this,"pos:"+position,Toast.LENGTH_SHORT).show();
    }
}




(3).BaseAdapter:可以对ListView的性能进行优化,通过重写GetView方法。

采用ViewHolder模式。


lv_layout.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView
        android:id="@+id/img"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="5dp"
        android:layout_weight="1"

        />
    <LinearLayout

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="4"
        >
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="22sp"
            />
        <TextView
            android:id="@+id/info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"/>

    </LinearLayout>
    <Button
        android:id="@+id/imgbtn"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="5dp"
        android:background="@drawable/selector"
        android:gravity="right"
        />


</LinearLayout>


ViewHolder.java文件
package edu.jnu.adapterdemo;

import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by roly on 16/6/28.
 */
final public class ViewHolder {
    public ImageView imageView;
    public TextView title;
    public TextView info;
    public Button viewBtn;
}

MyAdapter.java文件

package edu.jnu.adapterdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by roly on 16/6/28.
 */
class MyAdapter extends BaseAdapter {


    private MainActivity mainActivity;
    private LayoutInflater layoutInflater;

    public MyAdapter(MainActivity mainActivity, Context context) {
        this.mainActivity = mainActivity;
        this.layoutInflater = LayoutInflater.from(context);
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();

            convertView = layoutInflater.inflate(R.layout.lv_layout, null);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.img);
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.info = (TextView) convertView.findViewById(R.id.info);
            viewHolder.viewBtn = (Button) convertView.findViewById(R.id.imgbtn);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.imageView.setBackgroundResource((Integer) mainActivity.mData.get(position).get("image"));
        viewHolder.title.setText((String) mainActivity.mData.get(position).get("title"));
        viewHolder.info.setText((String) mainActivity.mData.get(position).get("info"));

        viewHolder.viewBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mainActivity.showInfo();
            }
        });


        return convertView;
    }
}

MainActivity.java文件

package edu.jnu.adapterdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MainActivity extends Activity {

    private ListView listView;

    List<Map<String,Object>> mData;


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

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

        mData = getdata();

        MyAdapter adapter = new MyAdapter(this, this);

        listView.setAdapter(adapter);

    }

    //数据源
    public List<Map<String,Object>> getdata(){

        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("title","Title1");
        map.put("info","Info1");
        map.put("image",R.drawable.i1);
        list.add(map);

        map = new HashMap<String,Object>();
        map.put("title","Title2");
        map.put("info","Info2");
        map.put("image",R.drawable.i2);
        list.add(map);

        map = new HashMap<String,Object>();
        map.put("title","Title3");
        map.put("info","Info3");
        map.put("image",R.drawable.i3);
        list.add(map);

        return list;
    }

    public void showInfo(){
        new AlertDialog.Builder(this)
                .setTitle("Alert")
                .setMessage("Introduce")
                .setPositiveButton("ok",new DialogInterface.OnClickListener(){

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .show();
    }

}





项目源码:

http://download.csdn.net/detail/ymmqwe/9562334





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值