Android模仿QQ的左右滑动切换界面和下拉更新的效果
转自http://www.linuxidc.com/Linux/2012-08/67207.htm主布局main.xmlxml version="1.0" encoding="utf-8"?> LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" android:layou
转自http://www.linuxidc.com/Linux/2012-08/67207.htm
主布局main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <HorizontalScrollView
- android:id="@+id/horizontalscrollview"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@drawable/item"
- android:fadingEdge="@null"
- android:scrollbars="none" >
- <LinearLayout
- android:id="@+id/linearlayout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- >
- </LinearLayout>
- </HorizontalScrollView>
- <android.support.v4.view.ViewPager
- android:id="@+id/viewpaper"
- android:layout_width="fill_parent"
- android:layout_height="match_parent"
- android:layout_weight="1" >
- </android.support.v4.view.ViewPager>
- </LinearLayout>
2,接着看看QQonline.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:background="@drawable/image2">
- <ExpandableListView
- android:id="@+id/elistview"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_marginLeft="5dp"
- />
- </LinearLayout>
3.接着看看下拉更新的布局pullrefresh.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/image1"
- android:orientation="vertical" >
- <com.wang.pullrefresh.MyListView
- android:id="@+id/listView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" >
- <RelativeLayout
- android:id="@+id/head_contentLayout"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:paddingLeft="30dp" >
- <FrameLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_centerVertical="true" >
- <ImageView
- android:id="@+id/head_arrowImageView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:src="@drawable/down" />
- <ProgressBar
- android:id="@+id/head_progressBar"
- style="?android:attr/progressBarStyleSmall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:visibility="gone" />
- </FrameLayout>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:gravity="center_horizontal"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/head_tipsTextView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="下拉可以刷新"
- android:textSize="15dp" />
- <TextView
- android:id="@+id/head_lastUpdatedTextView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="上次更新时间:"
- android:textSize="12dp" />
- </LinearLayout>
- </RelativeLayout>
- </LinearLayout>
- <?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="wrap_content"
- android:gravity="center_vertical"
- android:orientation="horizontal" >
- <ImageView
- android:id="@+id/imageView_item"
- android:layout_width="60dp"
- android:layout_height="60dp"
- android:layout_marginLeft="5dp"
- android:src="@drawable/qq" />
- <TextView
- android:id="@+id/textView_item"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:textColor="#FFFFFF"
- />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:background="@drawable/image3">
- </LinearLayout>
7.接着看看主活动的实现过程ViewPaper1Activity.java
- package com.wang;
- import java.util.ArrayList;
- import android.app.ActivityGroup;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.Bundle;
- import android.support.v4.view.PagerAdapter;
- import android.support.v4.view.ViewPager;
- import android.support.v4.view.ViewPager.OnPageChangeListener;
- import android.view.Gravity;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.View.OnClickListener;
- import android.widget.HorizontalScrollView;
- import android.widget.LinearLayout;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.TextView;
- import android.widget.LinearLayout.LayoutParams;
- public class ViewPaper1Activity extends ActivityGroup implements
- OnClickListener {
- private RadioGroup radioGroup;
- private String array[] = { "QQ空间", "QQ", "QQ消息" };
- private LinearLayout linearLayout;
- private ArrayList<TextView> textViews;
- private ViewPager viewPager;
- private ArrayList<View> pageViews;
- private HorizontalScrollView horizontalScrollView;
- /***
- *初始化视图
- *
- *
- */
- void InItView() {
- // 构造一个新的ArrayList实例对象
- pageViews = new ArrayList<View>();
- /**
- * 开始一个新的活动中运行的组织。 每一个活动你开始必须有一个独一无二的字符串标识与其相关联
- * **/
- View view1 = getLocalActivityManager().startActivity("main1",
- new Intent(this, pullrefreshtest.class)).getDecorView();
- View view2 = getLocalActivityManager().startActivity("main2",
- new Intent(this, QQonline.class)).getDecorView();
- View view3 = getLocalActivityManager().startActivity("main3",
- new Intent(this, main3.class)).getDecorView();
- // 添加指定的对象在文章末尾的ArrayList。
- pageViews.add(view1);
- pageViews.add(view2);
- pageViews.add(view3);
- }
- /***
- * 初始化话标题栏
- */
- void InItTitle() {
- // 获取窗口管理器显示自定义窗口,去掉默认显示对象。得到1/3屏幕宽度
- int width = getWindowManager().getDefaultDisplay().getWidth() / 3;
- for (int i = 0; i < array.length; i++) {
- // 声明一个radioButton对象
- RadioButton radioButton = new RadioButton(this, null);
- radioButton.setText(array[i]);
- radioButton.setWidth(width);
- radioButton.setHeight(70);
- // 集水平对齐文本和垂直重力的时候将会使用有额外的空间在TextView超出要求的文本本身
- radioButton.setGravity(Gravity.CENTER);
- // 添加子视图。如果没有布局参数对孩子已经设置,默认参数对于这个ViewGroup上设置的孩子。
- radioGroup.addView(radioButton);
- }
- }
- /***
- * 初始化文本
- */
- void initTextView() {
- // 声明一个ArrayList对象
- textViews = new ArrayList<TextView>();
- // 获取窗口管理器显示自定义窗口,去掉默认显示对象。得到1/3屏幕宽度
- int width = getWindowManager().getDefaultDisplay().getWidth() / 3;
- int height = 60;
- for (int i = 0; i < array.length; i++) {
- TextView textView = new TextView(this);
- textView.setText(array[i]);
- textView.setTextSize(17);
- textView.setWidth(width);
- textView.setHeight(height - 30);
- textView.setGravity(Gravity.CENTER);
- textView.setId(i);
- // 设置文本的监听事件
- textView.setOnClickListener(this);
- textViews.add(textView);
- LinearLayout.LayoutParams layoutParams = new LayoutParams(
- LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- layoutParams.width = 1;
- layoutParams.height = height - 40;
- layoutParams.gravity = Gravity.CENTER;
- // 添加子视图。如果没有布局参数对孩子已经设置,默认参数对于这个ViewGroup上设置的孩子。
- linearLayout.addView(textView);
- }
- }
- /***
- * 选中后的处理
- */
- public void select(int id) {
- for (int i = 0; i < array.length; i++) {
- if (id == i) {
- // 得到图片的资源
- Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
- R.drawable.itembg);
- // 设置背景图
- textViews.get(id).setBackgroundDrawable(
- new BitmapDrawable(bitmap));
- viewPager.setCurrentItem(i);
- }
- else {
- textViews.get(i).setBackgroundDrawable(new BitmapDrawable());
- }
- }
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 去除标题栏
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- // 取消状态栏,充满全屏
- this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.main);
- // 实例化组件
- linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
- viewPager = (ViewPager) findViewById(R.id.viewpaper);
- horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontalscrollview);
- // 调用方法实现
- initTextView();
- select(0);
- InItView();
- viewPager.setAdapter(new PagerAdapter() {
- // 得到数目
- public int getCount() {
- return pageViews.size();
- }
- @Override
- public boolean isViewFromObject(View view, Object object) {
- return view == object;
- }
- @Override
- public int getItemPosition(Object object) {
- // TODO Auto-generated method stub
- return super.getItemPosition(object);
- }
- @Override
- public void destroyItem(View view, int id, Object arg2) {
- // TODO Auto-generated method stub
- ((ViewPager) view).removeView(pageViews.get(id));
- }
- // 获取每一个item的id
- @Override
- public Object instantiateItem(View view, int id) {
- ((ViewPager) view).addView(pageViews.get(id));
- return pageViews.get(id);
- }
- });
- // 页面改变时候的监听事件
- viewPager.setOnPageChangeListener(new OnPageChangeListener() {
- @Override
- public void onPageSelected(int arg0) {
- select(arg0);
- }
- @Override
- public void onPageScrolled(int arg0, float arg1, int arg2) {
- }
- @Override
- public void onPageScrollStateChanged(int arg0) {
- }
- });
- }
- @Override
- public void onClick(View v) {
- select(v.getId());
- }
- }
8.接着看看QQonline.java 的实现
- package com.wang;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.ContextMenu;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.ContextMenu.ContextMenuInfo;
- import android.widget.ExpandableListAdapter;
- import android.widget.ExpandableListView;
- import android.widget.Toast;
- import android.widget.ExpandableListView.OnChildClickListener;
- import android.widget.ExpandableListView.OnGroupClickListener;
- import android.widget.ExpandableListView.OnGroupCollapseListener;
- import android.widget.ExpandableListView.OnGroupExpandListener;
- public class QQonline extends Activity {
- // 创建一个上下文菜单的方法
- public void onCreateContextMenu(ContextMenu menu, View v,
- ContextMenuInfo menuInfo) {
- super.onCreateContextMenu(menu, v, menuInfo);
- // 一个垂直滚动的两级列表。取得菜单项
- ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
- // 获得这个类型 的位置
- int type = ExpandableListView
- .getPackedPositionType(info.packedPosition);
- // 取得所在组的索引
- int group = ExpandableListView
- .getPackedPositionGroup(info.packedPosition);
- // 取得子菜单的索引
- int child = ExpandableListView
- .getPackedPositionGroup(info.packedPosition);
- Toast.makeText(QQonline.this,
- "类型 =" + type + " 分组:" + group + " 子选项:" + child,
- Toast.LENGTH_LONG).show();
- }
- private ExpandableListView eListView = null;
- private ExpandableListAdapter adapter = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- // 去除标题,
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- // 取消状态栏,充满全屏
- this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- super.onCreate(savedInstanceState);
- super.setContentView(R.layout.qqonline);
- // 实例化组件
- this.eListView = (ExpandableListView) findViewById(R.id.elistview);
- // 声明一个adapter对象
- adapter = new myExpendableadapler(this);
- // 设置适配器提供了数据
- this.eListView.setAdapter(this.adapter);
- // 注册一个上下文菜单显示给定的视图(多个视图可以显示上下文菜单)。
- super.registerForContextMenu(this.eListView);
- // 设置点击时候触发的事件 1,子选项点击事件 2。父选项单击事件 3.分组打开事件 4.分组关闭事件
- this.eListView.setOnChildClickListener(new ChildClickListener());
- this.eListView.setOnGroupClickListener(new GroupClickListener());
- this.eListView.setOnGroupExpandListener(new GroupExpandListener());
- this.eListView.setOnGroupCollapseListener(new GroupCollapseListener());
- }
- // /1,子选项点击事件
- private class ChildClickListener implements OnChildClickListener {
- public boolean onChildClick(ExpandableListView parent, View v,
- int groupPosition, int childPosition, long id) {
- Toast
- .makeText(
- QQonline.this,
- "子选项被选中,所的组:" + groupPosition + " 子选项的位置:"
- + childPosition, Toast.LENGTH_LONG).show();
- return false;
- }
- }
- // 2。父选项单击事件
- private class GroupClickListener implements OnGroupClickListener {
- public boolean onGroupClick(ExpandableListView parent, View v,
- int groupPosition, long id) {
- Toast.makeText(QQonline.this, "分组选项被选中,所在组: " + groupPosition,
- Toast.LENGTH_LONG).show();
- return false;
- }
- }
- // 3.分组打开事件
- private class GroupExpandListener implements OnGroupExpandListener {
- public void onGroupExpand(int groupPosition) {
- // TODO Auto-generated method stub
- Toast.makeText(QQonline.this, "打开分组,所在组:" + groupPosition,
- Toast.LENGTH_LONG).show();
- }
- }
- // 4.分组关闭事件
- private class GroupCollapseListener implements OnGroupCollapseListener {
- public void onGroupCollapse(int groupPosition) {
- Toast.makeText(QQonline.this, "关闭分组,所在组:" + groupPosition,
- Toast.LENGTH_LONG).show();
- }
- }
- }
- package com.wang;
- import android.content.Context;
- import android.view.Gravity;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AbsListView;
- import android.widget.BaseExpandableListAdapter;
- import android.widget.TextView;
- public class myExpendableadapler extends BaseExpandableListAdapter {
- // 组名称
- private String[] group = new String[] { "在线好友", "高中同学", "南阳理工同学", "寝室哥们" };
- // 子选项的名字
- private String[][] child = new String[][] {
- { "李彦宏", "马云", "雷军", "周鸿祎", "唐骏", "赵威威", "李冕", "马海刚", "李斌", "刘洪刚",
- "马玉龙", "刘慧敏", "王文龙" },
- { "赵威威", "李冕", "马海刚", "李斌", "刘洪刚", "马玉龙", "刘慧敏", "王文龙" },
- { "张紫阳", "王亚光", "刘一鸣", "崔芳芳", "董卫杰", "刘永乐" },
- { "武云册", "王俊伟", "王杰", "付永振", "杜新峰", "应志超" } };
- private Context context = null;
- // 构造函数
- public myExpendableadapler(Context context) {
- this.context = context;
- }
- public Object getChild(int groupPosition, int childPosition) {
- return this.child[groupPosition][childPosition];
- }
- public long getChildId(int groupPosition, int childPosition) {
- return childPosition;
- }
- private TextView buildTextView() {
- // LayoutParams AbsListView扩展提供一个位置来保存视图类型。
- AbsListView.LayoutParams params = new AbsListView.LayoutParams(
- ViewGroup.LayoutParams.FILL_PARENT, 40);
- TextView textView = new TextView(this.context);
- textView.setLayoutParams(params);
- // 大小
- textView.setTextSize(15.0f);
- textView.setGravity(Gravity.LEFT + 3);
- textView.setPadding(40, 8, 3, 3);
- return textView;
- }
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent) {
- TextView textView = new TextView(this.context);
- // 得到每组的子选项并转换成字符串
- textView
- .setText(this.getChild(groupPosition, childPosition).toString());
- return textView;
- }
- // 统计子选项的个数
- public int getChildrenCount(int groupPosition) {
- // TODO Auto-generated method stub
- return this.child[groupPosition].length;
- }
- // 得到复选项的位置
- public Object getGroup(int groupPosition) {
- // TODO Auto-generated method stub
- return this.group[groupPosition];
- }
- // 得到复选项的个数
- public int getGroupCount() {
- // TODO Auto-generated method stub
- return this.group.length;
- }
- // 得到复选项的id
- public long getGroupId(int groupPosition) {
- // TODO Auto-generated method stub
- return groupPosition;
- }
- public View getGroupView(int groupPosition, boolean isExpanded,
- View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- TextView textView = this.buildTextView();
- textView.setText(this.getGroup(groupPosition).toString());
- return textView;
- }
- // 是否子选项和父选项id是稳定在改变底层数据。
- public boolean hasStableIds() {
- // TODO Auto-generated method stub
- return true;
- }
- // p判断子选项是否可以选择
- public boolean isChildSelectable(int groupPosition, int childPosition) {
- // TODO Auto-generated method stub
- return true;
- }
- }
10.有两个界面还没有添加功能pullrefreshtest.java
- package com.wang;
- import java.util.LinkedList;
- import com.wang.pullrefresh.MyListView;
- import com.wang.pullrefresh.MyListView.OnRefreshListener;
- import android.app.Activity;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.BaseAdapter;
- import android.widget.TextView;
- public class pullrefreshtest extends Activity {
- private LinkedList<String> data;
- private BaseAdapter adapter;
- public void onCreate(Bundle savedInstanceState) {
- // 去除标题栏
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- // 设置全屏,取消状态栏
- this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- super.onCreate(savedInstanceState);
- setContentView(R.layout.pullrefresh);
- data = new LinkedList<String>();
- for (int i = 40; i < 50; i++) {
- // 输出list——item上的数据
- data.add(String.valueOf("暑假第" + i + "天,在南阳理工,想家ing ..."));
- }
- final MyListView listView = (MyListView) findViewById(R.id.listView);
- adapter = new BaseAdapter() {
- // 得到一个视图,显示在指定位置上的数据在数据集,可以创建一个视图从XML布局文件
- public View getView(int position, View convertView, ViewGroup parent) {
- // 上下文全局应用程序对象
- convertView = LayoutInflater.from(getApplicationContext())
- .inflate(R.layout.list_item, null);
- // 实例化组件
- TextView textView = (TextView) convertView
- .findViewById(R.id.textView_item);
- // 设置文本本内容
- textView.setText(data.get(position));
- return convertView;
- }
- // 得到行相关联id列表中指定的位置。
- public long getItemId(int position) {
- return position;
- }
- // 获得相关的数据项中的指定位置的数据集
- public Object getItem(int position) {
- return data.get(position);
- }
- // 获得项目在数据集适配器的个数。
- public int getCount() {
- return data.size();
- }
- };
- listView.setAdapter(adapter);
- listView.setonRefreshListener(new OnRefreshListener() {
- public void onRefresh() {
- new AsyncTask<Void, Void, Void>() {
- // ...b表示多个参数
- protected Void doInBackground(Void... params) {
- try {
- //
- Thread.sleep(1000);
- } catch (Exception e) {
- e.printStackTrace();
- }
- // 增加一条数据到list中
- data.addFirst("刷新后内容:每天都是新的一天!!!,親!要努力奋斗哦!!!");
- return null;
- }
- protected void onPostExecute(Void result) {
- adapter.notifyDataSetChanged();
- listView.onRefreshComplete();
- }
- }.execute(null);
- }
- });
- }
- }
11.接着是下拉列表更新的实现过程MyListView.java
- package com.wang.pullrefresh;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import com.wang.R;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.View.MeasureSpec;
- import android.view.animation.LinearInterpolator;
- import android.view.animation.RotateAnimation;
- import android.widget.AbsListView;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.ListView;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- import android.widget.AbsListView.OnScrollListener;
- public class MyListView extends ListView implements OnScrollListener {
- private static final String TAG = "listview";
- private final static int RELEASE_To_REFRESH = 0;
- private final static int PULL_To_REFRESH = 1;
- private final static int REFRESHING = 2;
- private final static int DONE = 3;
- private final static int LOADING = 4;
- // 实际的padding的距离与界面上偏移距离的比例
- private final static int RATIO = 3;
- private LayoutInflater inflater;
- private LinearLayout headView;
- private TextView tipsTextview;
- private TextView lastUpdatedTextView;
- private ImageView arrowImageView;
- private ProgressBar progressBar;
- private RotateAnimation animation;
- private RotateAnimation reverseAnimation;
- // 用于保证startY的值在一个完整的touch事件中只被记录一次
- private boolean isRecored;
- private int headContentWidth;
- private int headContentHeight;
- private int startY;
- private int firstItemIndex;
- private int state;
- private boolean isBack;
- private OnRefreshListener refreshListener;
- private boolean isRefreshable;
- public MyListView(Context context) {
- super(context);
- // 调用下面初始化的函数
- init(context);
- }
- public MyListView(Context context, AttributeSet attrs) {
- super(context, attrs);
- // 调用下面初始化的函数
- init(context);
- }
- private void init(Context context) {
- // 获得LayoutInflater从给定的上下文。
- inflater = LayoutInflater.from(context);
- // 实例化布局XML文件转换成相应的视图对象。
- headView = (LinearLayout) inflater.inflate(R.layout.refresh, null);
- arrowImageView = (ImageView) headView
- .findViewById(R.id.head_arrowImageView);
- // 设置最小宽度 和高度
- arrowImageView.setMinimumWidth(70);
- arrowImageView.setMinimumHeight(50);
- // 实例化布局XML文件转换成相应的视图对象。
- progressBar = (ProgressBar) headView
- .findViewById(R.id.head_progressBar);
- tipsTextview = (TextView) headView.findViewById(R.id.head_tipsTextView);
- lastUpdatedTextView = (TextView) headView
- .findViewById(R.id.head_lastUpdatedTextView);
- // 调用下拉刷新的方法
- measureView(headView);
- // d得到原始高度和宽度
- headContentHeight = headView.getMeasuredHeight();
- headContentWidth = headView.getMeasuredWidth();
- // 设置填充。视图可能添加的空间要求显示滚动条,这取决于风格和知名度的滚动条
- headView.setPadding(0, -1 * headContentHeight, 0, 0);
- headView.invalidate();
- // 标签用来识别一个日志消息的来源。标识类或活动日志调用发生
- Log.v("size", "width:" + headContentWidth + " height:"
- + headContentHeight);
- // 添加一个固定视图出现在列表的顶部
- addHeaderView(headView, null, false);
- // 设置监听事件
- setOnScrollListener(this);
- // 动画效果实现下拉和松开时候图片的 180度 旋转 注意0, -180,和他是有区别的 -180, 0,
- animation = new RotateAnimation(0, -180,
- RotateAnimation.RELATIVE_TO_SELF, 0.5f,
- RotateAnimation.RELATIVE_TO_SELF, 0.5f);
- // 设置加速度曲线为这个动画。默认值为一个线性插值。
- animation.setInterpolator(new LinearInterpolator());
- animation.setDuration(300);
- // 如果fillAfter是真的,转换,该动画执行完成时将会持续下去
- animation.setFillAfter(true);
- reverseAnimation = new RotateAnimation(-180, 0,
- RotateAnimation.RELATIVE_TO_SELF, 0.5f,
- RotateAnimation.RELATIVE_TO_SELF, 0.5f);
- // 设置加速度曲线为这个动画。默认值为一个线性插值。
- reverseAnimation.setInterpolator(new LinearInterpolator());
- reverseAnimation.setDuration(300);
- // 如果fillAfter是真的,转换,该动画执行完成时将会持续下去
- reverseAnimation.setFillAfter(true);
- // 设置状态
- state = DONE;
- // 设置不可刷新状态
- isRefreshable = false;
- }
- // 回调方法时要调用的列表或网格已经滚动。这将完成之后调用的滚动方法
- public void onScroll(AbsListView arg0, int firstVisiableItem, int arg2,
- int arg3) {
- firstItemIndex = firstVisiableItem;
- }
- /*
- * 回调方法调用而列表视图或网格视图被滚动。 如果这个视图被滚动,将调用此方法在接下来的一局画卷的呈现 *
- */
- public void onScrollStateChanged(AbsListView arg0, int arg1) {
- }
- // 触摸事件监听
- public boolean onTouchEvent(MotionEvent event) {
- // 判断是否可以刷新
- if (isRefreshable) {
- // 根据动作相应不同的方法
- switch (event.getAction()) {
- // 当按住屏幕向下拉屏幕的时候
- case MotionEvent.ACTION_DOWN:
- if (firstItemIndex == 0 && !isRecored) {
- isRecored = true;
- startY = (int) event.getY();
- Log.v(TAG, "在下拉的时候记录当前位置‘");
- }
- break;
- // 当按住屏幕向上松屏幕的时候
- case MotionEvent.ACTION_UP:
- if (state != REFRESHING && state != LOADING) {
- if (state == DONE) {
- }
- if (state == PULL_To_REFRESH) {
- state = DONE;
- changeHeaderViewByState();
- Log.v(TAG, "由下拉刷新状态,到done状态");
- }
- if (state == RELEASE_To_REFRESH) {
- state = REFRESHING;
- changeHeaderViewByState();
- onRefresh();
- Log.v(TAG, "由松开刷新状态,到done状态");
- }
- }
- isRecored = false;
- isBack = false;
- break;
- // 当按住屏幕移动时候
- case MotionEvent.ACTION_MOVE:
- int tempY = (int) event.getY();
- if (!isRecored && firstItemIndex == 0) {
- Log.v(TAG, "在move时候记录下位置");
- isRecored = true;
- startY = tempY;
- }
- if (state != REFRESHING && isRecored && state != LOADING) {
- /***
- * , 当前的位置一直是在head,否则如果当列表超出屏幕的话, 当在上推的时候,列表会同时进行滚动
- */
- // 可以松手去刷新了
- if (state == RELEASE_To_REFRESH) {
- setSelection(0);
- // 往上推了,推到了屏幕足够掩盖head的程度,但是还没有推到全部掩盖的地步
- if (((tempY - startY) / RATIO < headContentHeight)
- && (tempY - startY) > 0) {
- state = PULL_To_REFRESH;
- changeHeaderViewByState();
- Log.v(TAG, "由松开刷新状态转变到下拉刷新状态");
- }
- // 一下子推到顶了
- else if (tempY - startY <= 0) {
- state = DONE;
- // 调用改变时候的方法,更新UI
- changeHeaderViewByState();
- Log.v(TAG, "由松开刷新状态转变到done状态");
- } else {
- }
- }
- // 还没有到达显示松开刷新的时候
- if (state == PULL_To_REFRESH) {
- setSelection(0);
- // 下拉到可以进入RELEASE_TO_REFRESH的状态
- if ((tempY - startY) / RATIO >= headContentHeight) {
- state = RELEASE_To_REFRESH;
- isBack = true;
- // 调用改变时候的方法,更新UI
- changeHeaderViewByState();
- Log.v(TAG, "由done或者下拉刷新状态转变到松开刷新");
- }
- // 上推到顶了
- else if (tempY - startY <= 0) {
- state = DONE;
- // 调用改变时候的方法,更新UI
- changeHeaderViewByState();
- Log.v(TAG, "由DOne或者下拉刷新状态转变到done状态");
- }
- }
- // done状态下
- if (state == DONE) {
- if (tempY - startY > 0) {
- state = PULL_To_REFRESH;
- // 调用改变时候的方法,更新UI
- changeHeaderViewByState();
- }
- }
- // 更新headView的size
- if (state == PULL_To_REFRESH) {
- headView.setPadding(0, -1 * headContentHeight
- + (tempY - startY) / RATIO, 0, 0);
- }
- // 更新headView的paddingTop
- if (state == RELEASE_To_REFRESH) {
- headView.setPadding(0, (tempY - startY) / RATIO
- - headContentHeight, 0, 0);
- }
- }
- break;
- }
- }
- return super.onTouchEvent(event);
- }
- // 当状态改变时候,调用该方法,以更新界面
- private void changeHeaderViewByState() {
- // 根据当前的状态进行判断
- switch (state) {
- // 下拉时候,松开既可刷新
- case RELEASE_To_REFRESH:
- // 设置视图 VISIBLE 可见 ,GONE 不可见
- arrowImageView.setVisibility(View.VISIBLE);
- progressBar.setVisibility(View.GONE);
- tipsTextview.setVisibility(View.VISIBLE);
- lastUpdatedTextView.setVisibility(View.VISIBLE);
- // 现在开始指定的动画。
- arrowImageView.clearAnimation();
- arrowImageView.startAnimation(animation);
- tipsTextview.setText("松开既可刷新");
- Log.v(TAG, "当前状态,松开即可刷新");
- break;
- // 开始时候,下拉刷新
- case PULL_To_REFRESH:
- // 设置视图 VISIBLE 可见 ,GONE 不可见
- progressBar.setVisibility(View.GONE);
- tipsTextview.setVisibility(View.VISIBLE);
- lastUpdatedTextView.setVisibility(View.VISIBLE);
- // 现在开始指定的动画。
- arrowImageView.clearAnimation();
- arrowImageView.setVisibility(View.VISIBLE);
- if (isBack) {
- isBack = false;
- // 现在开始指定的动画。
- arrowImageView.clearAnimation();
- arrowImageView.startAnimation(reverseAnimation);
- tipsTextview.setText("下拉刷新");
- } else {
- tipsTextview.setText("下拉刷新");
- }
- Log.v(TAG, "当前状态,下拉刷新");
- break;
- case REFRESHING:
- headView.setPadding(0, 0, 0, 0);
- // 设置视图 VISIBLE 可见 ,GONE 不可见
- progressBar.setVisibility(View.VISIBLE);
- // 现在开始指定的动画。
- arrowImageView.clearAnimation();
- arrowImageView.setVisibility(View.GONE);
- tipsTextview.setText("正在刷新...");
- lastUpdatedTextView.setVisibility(View.VISIBLE);
- Log.v(TAG, "当前状态,正在刷新...");
- break;
- case DONE:
- // 设置填充。视图可能添加的空间要求显示滚动条
- headView.setPadding(0, -1 * headContentHeight, 0, 0);
- // 设置视图 VISIBLE 可见 ,GONE 不可见
- progressBar.setVisibility(View.GONE);
- // 现在开始指定的动画。
- arrowImageView.clearAnimation();
- arrowImageView.setImageResource(R.drawable.down);
- tipsTextview.setText("下拉刷新");
- lastUpdatedTextView.setVisibility(View.VISIBLE);
- Log.v(TAG, "当前状态");
- break;
- }
- }
- public void setonRefreshListener(OnRefreshListener refreshListener) {
- this.refreshListener = refreshListener;
- isRefreshable = true;
- }
- public interface OnRefreshListener {
- public void onRefresh();
- }
- // 设置更新时间
- public void onRefreshComplete() {
- state = DONE;
- //
- SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
- String date = format.format(new Date());
- lastUpdatedTextView.setText("最近更新:" + date);
- changeHeaderViewByState();
- }
- private void onRefresh() {
- if (refreshListener != null) {
- refreshListener.onRefresh();
- }
- }
- // 下拉刷新的
- private void measureView(View child) {
- // v这组布局参数宽度和高度
- ViewGroup.LayoutParams p = child.getLayoutParams();
- if (p == null) {
- // 创建一个新组布局参数指定的宽度(填充)和高度(包裹)。
- p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
- ViewGroup.LayoutParams.WRAP_CONTENT);
- }
- // d得到childWidthSpec(高度或宽度)的子视图
- int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
- int lpHeight = p.height;
- int childHeightSpec;
- if (lpHeight > 0) {
- // 创建一个测量规范基于所提供的大小和模式
- childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
- MeasureSpec.EXACTLY);
- } else {
- childHeightSpec = MeasureSpec.makeMeasureSpec(0,
- MeasureSpec.UNSPECIFIED);
- }
- // 找出一个视图应该多大。父供应约束信息在宽度和高度参数
- child.measure(childWidthSpec, childHeightSpec);
- }
- public void setAdapter(BaseAdapter adapter) {
- // 设置最近刷新的时间
- SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
- String date = format.format(new Date());
- lastUpdatedTextView.setText("最近更新:" + date);
- super.setAdapter(adapter);
- }
- }
12.接着是没有添加聊天功能的main3.java
- package com.wang;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ImageView;
- public class main3 extends Activity {
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main3);
- }
- }
- <activity android:name=".pullrefreshtest"></activity>
- <activity android:name=".QQonline"></activity>
- <activity android:name=".main3"></activity>
14.最后,还有一个重要的问题,本程序需要第三方jar包的支持,所以呢还需要导入第三方android-support-v4.jar包, 不会的参考 http://www.linuxidc.com/Linux/2012-08/67213.htm
下载地址
免费下载地址在 http://linux.linuxidc.com/
用户名与密码都是www.linuxidc.com
具体下载目录在 /2012年资料/8月/3日/Android模仿QQ的左右滑动切换界面和下拉更新的效果/android-support-v4.jar包
15,这样过后就可以看到运行效果咯: 第一张是下拉刷新时候的界面,第二张是松开即可刷新时候的图片,第三张是刷新后的内容,第四张的QQ联系人的界面,第五张是还没有完善聊天消息的界面,第六张是左右滑动切换界面的实现效果
更多推荐
所有评论(0)