Android中listview实现头部悬浮

先看效果,其实实现起来的逻辑并不难:

大概的逻辑就是给listview添加两个headview,并且将listview的父布局设置为帧布局,隐藏好一个需要一直显示在头部的标题栏,当firstVisibleItem>=1的时候,显示出那个隐藏好的标题栏即可。

  先来看看布局文件,这是第一个头布局headview1:

<?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" >
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="#302015"></LinearLayout>
</LinearLayout>
  这是第二个头布局headview2:

<?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="fill_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@drawable/qq"/>
</LinearLayout>
这是主布局,在帧布局中隐藏了一个emptyimg和一个要显示的标题栏:

<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:orientation="vertical"
    tools:context="com.example.listviewtest.MainActivity" >


    <FrameLayout 
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >
    </ListView>
            <LinearLayout
            android:id="@+id/invis"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
           android:visibility="gone"
            >

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/qq"
                android:scaleType="fitXY"
                />
        </LinearLayout>
                  <ImageView 
        android:id="@+id/enpty_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/radio_big_hubble"
        />  
</FrameLayout>
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="horizontal"
    android:layout_weight="2">
                  <Button
        android:id="@+id/button1"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:text="clear" />
                  <Button
        android:id="@+id/button2"
        android:layout_marginLeft="10dp"
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:text="reset" />

    
</LinearLayout>
   
</LinearLayout>

下面来看看具体实现的代码,这里我们添加了两个按钮,来实现清空adapter数据,以及恢复数据的方法,用来验证EmptyView功能:

package com.example.listviewtest;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.support.v7.app.ActionBarActivity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;


public class MainActivity extends ActionBarActivity  implements android.view.View.OnClickListener{

	private ListView mListView;
	private Button clearBtn;
	private Button resetBtn;
	private ListViewAdapter mAdapter;
	private ArrayList<String> mList;
	private float mFirstY;
	private float mSecondY;
	private float mCurrentY;
	private int mTouchSlop;
	private int direction;
	private LinearLayout invis;
	private View headview1;
	private View headview2;

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


	private void UintUI() {
		mListView=(ListView) this.findViewById(R.id.listView1);
		clearBtn=(Button)this.findViewById(R.id.button1);
		resetBtn=(Button)this.findViewById(R.id.button2);
		invis=(LinearLayout)this.findViewById(R.id.invis);
		clearBtn.setOnClickListener(this);
		resetBtn.setOnClickListener(this);
		mListView.setEmptyView(findViewById(R.id.enpty_img));  //设置当适配器为空的时候的图片
		mAdapter=new ListViewAdapter(mList,MainActivity.this);
		mListView.setAdapter(mAdapter);
		headview1=View.inflate(this, R.layout.headview, null);
		mListView.addHeaderView(headview1);  //添加头文件1
		headview2=View.inflate(this, R.layout.headview2, null);
		mListView.addHeaderView(headview2);//添加头文件2

		SimpleDateFormat sdf=new SimpleDateFormat("yy-MM-dd  HH:mm:ss");  //时间格式
		sdf.format(System.currentTimeMillis());

		mTouchSlop=ViewConfiguration.get(this).getScaledTouchSlop();

		mListView.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					mFirstY=event.getY();
					break;
				case MotionEvent.ACTION_UP:
					mSecondY=event.getY();
					break;
				case MotionEvent.ACTION_MOVE:
					mCurrentY=event.getY();
					if((mCurrentY-mFirstY)>mTouchSlop){
						direction=0;//down
					}
					else if((mCurrentY-mFirstY)<mTouchSlop){
						direction=1;//up
					}
					if (direction==1) {

					}else if(direction==0){

					}
					break;
				}
				return false;
			}
		});


		mListView.setOnScrollListener(new AbsListView.OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				if(firstVisibleItem>=1){   //当第一个headview消失的时候,显示的其实不是第二个headview,而是长相和它一样的一个图片
					invis.setVisibility(View.VISIBLE); 
				}
				else{
					invis.setVisibility(View.GONE);
				}


			}
		});
	}



	public ArrayList<String> getData(){
		mList=new ArrayList<String>();
		for (int i=0;i<15;i++){
			mList.add("this is No:"+i);
		}
		return mList;
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()){
		case R.id.button1:
			mList.clear();
			mListView.removeHeaderView(headview1);
			mListView.removeHeaderView(headview2);
			Log.e("list  size","is"+ mList.size());
			mAdapter.notifyDataSetChanged();
			break;
		case R.id.button2:
			getData();
			Log.e("list is", mList.get(5));
			UintUI();
			mAdapter.notifyDataSetChanged();
			mListView.setAdapter(mAdapter);
			break;
		}
	}
}

为listview添加适配器:

package com.example.listviewtest;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ListViewAdapter   extends BaseAdapter {

	@Override
	public int getItemViewType(int position) {
		return super.getItemViewType(position);
	}
	
	@Override
	public int getViewTypeCount() {
		return super.getViewTypeCount();
	}

	private ArrayList<String> list;
	private Context context;

	public ListViewAdapter(ArrayList<String> mList , Context context){
		this.list=mList;
		this.context=context;
	}
	
	@Override
	public int getCount() {
		return list.size();
	}

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

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		View view=LayoutInflater.from(context).inflate(R.layout.item, null);
		TextView mTextView=(TextView)view.findViewById(R.id.item_text);
		mTextView.setText(list.get(position));
		return view;
	}

	public class ViewHolder{
		public TextView mText;
	}

}




你可能感兴趣的:(Android中listview实现头部悬浮)