Android_listView_addHeadAndFoot

layout.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

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

</RelativeLayout>

listview_head.xml

<?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="match_parent"
    android:orientation="vertical" >
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@drawable/img"
        />

</LinearLayout>

listview_foot.xml

<?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="match_parent"
    android:orientation="vertical" >
    <Button
        android:id="@+id/btn_foot"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Don‘t touch me!"
        android:textSize="20sp"/>

</LinearLayout>

laitview_item_name.xml

<?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="match_parent"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"/>

</LinearLayout>

main.java

package com.example.day09_listview_addhead;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 添加头和脚
 * 1.//添加头--在setAdapter之前
 *     listView.addHeaderView(headView);//头
 *     listView.addFooterView(footView);//脚
 * 2.注意:
 *    //如果listView添加了头,单击事件的item的索引会加一
 *    listView.setOnItemClickListener(new ONItemCickListener)
 * @author my
 *
 */

public class MainActivity extends Activity {

    protected static final String TAG = "MainActivity";
    private ListView listView;
    private List<String> data;
    private boolean isBottom = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView);
        data = getData();
        MyAdapter myAdapter = new MyAdapter();
        //添加头--在setAdapter之前
        //将布局文件转换成view
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View headView = inflater.inflate(R.layout.listview_head, null);
        listView.addHeaderView(headView);
        //添加脚
        View footView = inflater.inflate(R.layout.listview_add_foot_view, null);
        //实例化footView的控件
        Button btn_foot = (Button) footView.findViewById(R.id.btn_foot);
        //为按钮设置点击 事件
        btn_foot.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "You are bad girl !", 0).show();
            }
        });
        listView.addFooterView(footView);
        //设置适配器
        listView.setAdapter(myAdapter);

    }
    private List<String> getData(){
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < 100; i++) {
            list.add("杭州----"+i);
        }
        return list;

    }
    class MyAdapter extends BaseAdapter{

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = convertView;
            ViewHolder viewHolder;
            if(view == null){
                view = View.inflate(MainActivity.this, R.layout.listview_item_name,null);
                viewHolder = new ViewHolder();
                viewHolder.name = (TextView)view.findViewById(R.id.tv_name);
                view.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.name.setText(data.get(position));
        return view;
        }

    }
    class ViewHolder{
        private TextView name;
    }

}

时间: 2024-10-15 21:31:51

Android_listView_addHeadAndFoot的相关文章