Android第二周(第二部分)-listview

ListView

1.基础

listview一般都是撑满屏幕match_parent
页面一定要在AndroidManifest中进行注册
Adapter
setAdapter进行listview和adapter绑定

1.修改AndroidManifest.xml注册ListViewDemoActivity类
"1.0" encoding="utf-8"?>
package="com.geekband.Test01"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionName="1.0.0">

    "true"
        android:icon="@mipmap/geekband"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        ".SplashActivity">
            
                "android.intent.action.MAIN"/>

                "android.intent.category.LAUNCHER"/>
            
        


        ".MainActivity"
            android:icon="@drawable/abc_ic_ab_back_mtrl_am_alpha">
        

        ".ListViewDemoActivity"
            android:icon="@drawable/abc_ic_ab_back_mtrl_am_alpha">
        

    


2.创建PhoneBookAdapter,为页面类提供数据来源
public class PhoneBookAdapter extends BaseAdapter {

    private Context mContext;

    private LayoutInflater mLayoutInflater;

    private String[] mNames = {"小明", "小花"};

    public PhoneBookAdapter(Context context) {
         mContext = context;
         mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // 有多少条数据
        return mNames.length;
    }

    @Override
    public Object getItem(int position) {
        // 返回某一条数据对象
        return mNames[position];
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 返回一个视图
         convertView = mLayoutInflater.inflate(R.layout.item_phone_book_friend, null);

        TextView nameTextView = (TextView) convertView.findViewById(R.id.name_text_view);
        nameTextView.setText(mNames[position]);
        return convertView;
    }
}

3.创建页面布局activity_listview_demo.xml
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    "wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/phone_book"/>

    "@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    


4.创建页面类,页面类中使用ListView并绑定前面创建的Adapter
public class ListViewDemoActivity extends Activity {

    private ListView mPhoneBookListView;

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

        mPhoneBookListView = (ListView) findViewById(R.id.list_view);

        PhoneBookAdapter phoneBookAdapter = new PhoneBookAdapter(ListViewDemoActivity.this);
        mPhoneBookListView.setAdapter(phoneBookAdapter);


    }
}
5.创建item_phone_book_friend.xml
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    "@+id/name_text_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>



关键元素
1.修改AndroidManifest.xml文件,注册Activity类
2.创建Adapter类,为页面类提供数据来源
3.创建布局文件
4.创建Activity类,按照生命周期写7大方法,其中在onCreate()方法中至少要完成绑定布局的工作;另外为ListView控件绑定Adapter
5.创建子视图布局item_phone_book_friend.xml
6.Adapter类中通过重写getView()方法来将子视图添加到ListView内,

LayoutInflater mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = mLayoutInflater.inflate(R.layout.item_phone_book_friend, null);

7.Adapter类中通过重写getCount()方法确定在ListView中显示多少个子视图。

2.进阶

为listview整体或者具体项目绑定点击或者长按事件
更新listview中数据,修改adapter中数据;调用notifyDataSetChanged()方法

3.优化

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item_phone_book_friend, null);
            viewHolder = new ViewHolder();
            // 获取控件
            viewHolder.nameTextView = (TextView) convertView.findViewById(R.id.name_text_view);
            viewHolder.ageTextView = (TextView) convertView.findViewById(R.id.age_text_view);
            viewHolder.avatarImageView = (ImageView) convertView.findViewById(R.id.avatar_image_view);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // 和数据之间进行绑定
        viewHolder.nameTextView.setText(mUserInfos.get(position).getUserName());
        viewHolder.ageTextView.setText(String.valueOf(mUserInfos.get(position).getAge()));
        viewHolder.avatarImageView.setImageResource(R.drawable.ic_launcher);

        return convertView;

    }


    class ViewHolder {
        TextView nameTextView;
        TextView ageTextView;
        ImageView avatarImageView;
    }
  1. 只加载一次子视图即mLayoutInflater.inflate(R.layout.item_phone_book_friend, null);只执行一次
  2. 由于每次看到页面都要执行getView,因此需要缓存子视图中的控件,以免重复findViewByID

你可能感兴趣的:(android)