因為功能還沒有完全實現,所以效果不是太好,不過呢,還是能實現,左右滑動時候切換界面和顯示QQ好友列表的簡單功能!!好了,廢話不多說了看看實現的過程吧!
1.首先看看布局
主布局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>
4.下拉更新過程實現的布局refresh.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="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>
5.下拉更新的列表顯示的布局list_item.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="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>
6.接著看看還沒有完善的QQ聊天信息的布局main3.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/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();
- }
-
- }
-
- }
9.接著需要myExpendableadapler.java
- 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);
- }
-
- }
13.親,由於用到Activity,要注意注冊哦!!
- <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聯系人的界面,第五張是還沒有完善聊天消息的界面,第六張是左右滑動切換界面的實現效果