转载请声明出处http://blog.csdn.net/zhongkejingwang/article/details/38340701
说到下拉刷新控件,网上版本有很多,很多软件也都有下拉刷新功能。有一个叫XListView的,我看别人用过,没看过是咋实现的,看这名字估计是继承自ListView修改的,不过效果看起来挺丑的,也没什么扩展性,太单调了。看了QQ2014的列表下拉刷新,发现挺好看的,我喜欢,贴一下图看一下qq的下拉刷新效果:
不错吧?嗯,是的。一看就知道实现方式不一样。咱们今天就来实现一个下拉刷新控件。由于有时候不仅仅是ListView需要下拉刷新,ExpandableListView和GridView也有这个需求,由于ListView,GridView都是AbsListView的子类,ExpandableListView是ListView的子类所以也是AbsListView的子类。所以我的思路是自定义一个对所有AbsListView的子类通用的下拉管理布局,叫PullToRefreshLayout,如果需要GridView,只需要在布局文件里将ListView换成GridView就行了,ExpandableListView也一样,不需要再继承什么GridView啊ListView啊乱七八糟的。
看上图,主要工作就是定义黑色大布局,红色部分是不下拉的时候的可见部分,可以是任意的AbsListView的子类(GridView,ListView,ExpandableListView等等)。其实我已经写好了,先看一下效果图:
正常拉法:
强迫症拉法:
上面是ListView的,下面是GridView的
再来看一下ExpandableListView的下拉刷新效果:
可以看到,点击事件和长按事件都能正常触发而不会误触发,在使用ExpandableListView的时候需要注意禁止展开时自动滚动,否则会出现bug。后面会提供demo源码下载,可以根据自己的需求去修改。
下面讲解PullToRefreshLayout的实现,在贴完整的源码之前先理解整个类的大概思路:
- public class PullToRefreshLayout extends RelativeLayout implements OnTouchListener
- {
-
-
- public float moveDeltaY = 0;
-
- private boolean canPull = true;
-
-
- private void hideHead()
- {
-
- }
-
-
- public void refreshFinish(int refreshResult)
- {
-
- }
-
- private void changeState(int to)
- {
-
- }
-
-
-
-
-
-
- @Override
- public boolean dispatchTouchEvent(MotionEvent ev)
- {
- switch (ev.getActionMasked())
- {
- case MotionEvent.ACTION_DOWN:
-
-
- break;
- case MotionEvent.ACTION_MOVE:
-
-
-
-
- break;
- case MotionEvent.ACTION_UP:
-
- default:
- break;
- }
-
- return super.dispatchTouchEvent(ev);
- }
-
-
-
-
-
-
-
-
- @Override
- protected void dispatchDraw(Canvas canvas)
- {
-
- }
-
-
-
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b)
- {
-
- }
-
-
- @Override
- public boolean onTouch(View v, MotionEvent event)
- {
-
- }
- }
可以看到,这里复写了ViewGroup的dispatchTouchEvent,这样就可以掌控事件的分发,如果不了解这个方法可以看一下这篇Android事件分发、View事件Listener全解析。之所以要控制事件分发是因为我们不可能知道手指down在AbsListView上之后将往上滑还是往下拉,所以down事件会分发给AbsListView的,但是在move的时候就需要看情况了,因为我们不想在下拉的同时AbsListView也在滑动,所以在下拉的时候不分发move事件,但这样问题又来了,前面AbsListView已经接收了down事件,如果这时候不分发move事件给它,它会触发长按事件或者点击事件,所以在这里还需要清除AbsListView消息列表中的callback。
onLayout用于重新布置下拉头和AbsListView的位置的,这个不难理解。
理解了大概思路之后,看一下PullToRefreshLayout完整的源码吧~
- package com.jingchen.pulltorefresh;
-
- import java.lang.reflect.Field;
- import java.util.Timer;
- import java.util.TimerTask;
-
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.LinearGradient;
- import android.graphics.Paint;
- import android.graphics.Paint.Style;
- import android.graphics.RectF;
- import android.graphics.Shader.TileMode;
- import android.os.Handler;
- import android.os.Message;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.View.OnTouchListener;
- import android.view.ViewGroup;
- import android.view.animation.AnimationUtils;
- import android.view.animation.LinearInterpolator;
- import android.view.animation.RotateAnimation;
- import android.widget.AbsListView;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
-
-
-
-
-
-
- public class PullToRefreshLayout extends RelativeLayout implements OnTouchListener
- {
- public static final String TAG = "PullToRefreshLayout";
-
- public static final int PULL_TO_REFRESH = 0;
-
- public static final int RELEASE_TO_REFRESH = 1;
-
- public static final int REFRESHING = 2;
-
- public static final int DONE = 3;
-
- private int state = PULL_TO_REFRESH;
-
- private OnRefreshListener mListener;
-
- public static final int REFRESH_SUCCEED = 0;
-
- public static final int REFRESH_FAIL = 1;
-
- private View headView;
-
- private View contentView;
-
- private float downY, lastY;
-
- public float moveDeltaY = 0;
-
- private float refreshDist = 200;
- private Timer timer;
- private MyTimerTask mTask;
-
- public float MOVE_SPEED = 8;
-
- private boolean isLayout = false;
-
- private boolean canPull = true;
-
- private boolean isTouchInRefreshing = false;
-
- private float radio = 2;
-
- private RotateAnimation rotateAnimation;
-
- private RotateAnimation refreshingAnimation;
-
- private View pullView;
-
- private View refreshingView;
-
- private View stateImageView;
-
- private TextView stateTextView;
-
-
-
- Handler updateHandler = new Handler()
- {
-
- @Override
- public void handleMessage(Message msg)
- {
-
- MOVE_SPEED = (float) (8 + 5 * Math.tan(Math.PI / 2 / getMeasuredHeight() * moveDeltaY));
- if (state == REFRESHING && moveDeltaY <= refreshDist && !isTouchInRefreshing)
- {
-
- moveDeltaY = refreshDist;
- mTask.cancel();
- }
- if (canPull)
- moveDeltaY -= MOVE_SPEED;
- if (moveDeltaY <= 0)
- {
-
- moveDeltaY = 0;
- pullView.clearAnimation();
-
- if (state != REFRESHING)
- changeState(PULL_TO_REFRESH);
- mTask.cancel();
- }
-
- requestLayout();
- }
-
- };
-
- public void setOnRefreshListener(OnRefreshListener listener)
- {
- mListener = listener;
- }
-
- public PullToRefreshLayout(Context context)
- {
- super(context);
- initView(context);
- }
-
- public PullToRefreshLayout(Context context, AttributeSet attrs)
- {
- super(context, attrs);
- initView(context);
- }
-
- public PullToRefreshLayout(Context context, AttributeSet attrs, int defStyle)
- {
- super(context, attrs, defStyle);
- initView(context);
- }
-
- private void initView(Context context)
- {
- timer = new Timer();
- mTask = new MyTimerTask(updateHandler);
- rotateAnimation = (RotateAnimation) AnimationUtils.loadAnimation(context, R.anim.reverse_anim);
- refreshingAnimation = (RotateAnimation) AnimationUtils.loadAnimation(context, R.anim.rotating);
-
- LinearInterpolator lir = new LinearInterpolator();
- rotateAnimation.setInterpolator(lir);
- refreshingAnimation.setInterpolator(lir);
- }
-
- private void hideHead()
- {
- if (mTask != null)
- {
- mTask.cancel();
- mTask = null;
- }
- mTask = new MyTimerTask(updateHandler);
- timer.schedule(mTask, 0, 5);
- }
-
-
-
-
- public void refreshFinish(int refreshResult)
- {
- refreshingView.clearAnimation();
- refreshingView.setVisibility(View.GONE);
- switch (refreshResult)
- {
- case REFRESH_SUCCEED:
-
- stateImageView.setVisibility(View.VISIBLE);
- stateTextView.setText(R.string.refresh_succeed);
- stateImageView.setBackgroundResource(R.drawable.refresh_succeed);
- break;
- case REFRESH_FAIL:
-
- stateImageView.setVisibility(View.VISIBLE);
- stateTextView.setText(R.string.refresh_fail);
- stateImageView.setBackgroundResource(R.drawable.refresh_failed);
- break;
- default:
- break;
- }
-
- new Handler()
- {
- @Override
- public void handleMessage(Message msg)
- {
- state = PULL_TO_REFRESH;
- hideHead();
- }
- }.sendEmptyMessageDelayed(0, 1000);
- }
-
- private void changeState(int to)
- {
- state = to;
- switch (state)
- {
- case PULL_TO_REFRESH:
-
- stateImageView.setVisibility(View.GONE);
- stateTextView.setText(R.string.pull_to_refresh);
- pullView.clearAnimation();
- pullView.setVisibility(View.VISIBLE);
- break;
- case RELEASE_TO_REFRESH:
-
- stateTextView.setText(R.string.release_to_refresh);
- pullView.startAnimation(rotateAnimation);
- break;
- case REFRESHING:
-
- pullView.clearAnimation();
- refreshingView.setVisibility(View.VISIBLE);
- pullView.setVisibility(View.INVISIBLE);
- refreshingView.startAnimation(refreshingAnimation);
- stateTextView.setText(R.string.refreshing);
- break;
- default:
- break;
- }
- }
-
-
-
-
-
-
- @Override
- public boolean dispatchTouchEvent(MotionEvent ev)
- {
- switch (ev.getActionMasked())
- {
- case MotionEvent.ACTION_DOWN:
- downY = ev.getY();
- lastY = downY;
- if (mTask != null)
- {
- mTask.cancel();
- }
-
-
-
-
- if (ev.getY() < moveDeltaY)
- return true;
- break;
- case MotionEvent.ACTION_MOVE:
-
- if (canPull)
- {
-
- moveDeltaY = moveDeltaY + (ev.getY() - lastY) / radio;
- if (moveDeltaY < 0)
- moveDeltaY = 0;
- if (moveDeltaY > getMeasuredHeight())
- moveDeltaY = getMeasuredHeight();
- if (state == REFRESHING)
- {
-
- isTouchInRefreshing = true;
- }
- }
- lastY = ev.getY();
-
- radio = (float) (2 + 2 * Math.tan(Math.PI / 2 / getMeasuredHeight() * moveDeltaY));
- requestLayout();
- if (moveDeltaY <= refreshDist && state == RELEASE_TO_REFRESH)
- {
-
- changeState(PULL_TO_REFRESH);
- }
- if (moveDeltaY >= refreshDist && state == PULL_TO_REFRESH)
- {
- changeState(RELEASE_TO_REFRESH);
- }
- if (moveDeltaY > 8)
- {
-
- clearContentViewEvents();
- }
- if (moveDeltaY > 0)
- {
-
- return true;
- }
- break;
- case MotionEvent.ACTION_UP:
- if (moveDeltaY > refreshDist)
-
- isTouchInRefreshing = false;
- if (state == RELEASE_TO_REFRESH)
- {
- changeState(REFRESHING);
-
- if (mListener != null)
- mListener.onRefresh();
- } else
- {
-
- }
- hideHead();
- default:
- break;
- }
-
- return super.dispatchTouchEvent(ev);
- }
-
-
-
-
- private void clearContentViewEvents()
- {
- try
- {
- Field[] fields = AbsListView.class.getDeclaredFields();
- for (int i = 0; i < fields.length; i++)
- if (fields[i].getName().equals("mPendingCheckForLongPress"))
- {
-
- fields[i].setAccessible(true);
- contentView.getHandler().removeCallbacks((Runnable) fields[i].get(contentView));
- } else if (fields[i].getName().equals("mTouchMode"))
- {
-
- fields[i].setAccessible(true);
- fields[i].set(contentView, -1);
- }
-
- ((AbsListView) contentView).getSelector().setState(new int[]
- { 0 });
- } catch (Exception e)
- {
- Log.d(TAG, "error : " + e.toString());
- }
- }
-
-
-
-
-
-
- @Override
- protected void dispatchDraw(Canvas canvas)
- {
- super.dispatchDraw(canvas);
- if (moveDeltaY == 0)
- return;
- RectF rectF = new RectF(0, 0, getMeasuredWidth(), moveDeltaY);
- Paint paint = new Paint();
- paint.setAntiAlias(true);
-
- LinearGradient linearGradient = new LinearGradient(0, moveDeltaY, 0, moveDeltaY - 26, 0x66000000, 0x00000000, TileMode.CLAMP);
- paint.setShader(linearGradient);
- paint.setStyle(Style.FILL);
-
- canvas.drawRect(rectF, paint);
- }
-
- private void initView()
- {
- pullView = headView.findViewById(R.id.pull_icon);
- stateTextView = (TextView) headView.findViewById(R.id.state_tv);
- refreshingView = headView.findViewById(R.id.refreshing_icon);
- stateImageView = headView.findViewById(R.id.state_iv);
- }
-
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b)
- {
- if (!isLayout)
- {
-
- headView = getChildAt(0);
- contentView = getChildAt(1);
-
- contentView.setOnTouchListener(this);
- isLayout = true;
- initView();
- refreshDist = ((ViewGroup) headView).getChildAt(0).getMeasuredHeight();
- }
- if (canPull)
- {
-
- headView.layout(0, (int) moveDeltaY - headView.getMeasuredHeight(), headView.getMeasuredWidth(), (int) moveDeltaY);
- contentView.layout(0, (int) moveDeltaY, contentView.getMeasuredWidth(), (int) moveDeltaY + contentView.getMeasuredHeight());
- }else super.onLayout(changed, l, t, r, b);
- }
-
- class MyTimerTask extends TimerTask
- {
- Handler handler;
-
- public MyTimerTask(Handler handler)
- {
- this.handler = handler;
- }
-
- @Override
- public void run()
- {
- handler.sendMessage(handler.obtainMessage());
- }
-
- }
-
- @Override
- public boolean onTouch(View v, MotionEvent event)
- {
-
- AbsListView alv = null;
- try
- {
- alv = (AbsListView) v;
- } catch (Exception e)
- {
- Log.d(TAG, e.getMessage());
- return false;
- }
- if (alv.getCount() == 0)
- {
-
- canPull = true;
- } else if (alv.getFirstVisiblePosition() == 0 && alv.getChildAt(0).getTop() >= 0)
- {
-
- canPull = true;
- } else
- canPull = false;
- return false;
- }
- }
代码中的注释已经写的很清楚了。
既然PullToRefreshLayout已经写好了,接下来就来使用这个Layout实现下拉刷新~
首先得写个OnRefreshListener接口来回调刷新操作:
- public interface OnRefreshListener {
- void onRefresh();
- }
就一个刷新操作的方法,待会儿让Activity实现这个接口就可以在Activity中执行刷新操作了。
看一下MainActivity的布局:
- <com.jingchen.pulltorefresh.PullToRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/refresh_view"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
-
- <include layout="@layout/refresh_head" />
-
-
- <ListView
- android:id="@+id/content_view"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/white"
- android:divider="@color/gray"
- android:dividerHeight="1dp" >
- </ListView>
-
- </com.jingchen.pulltorefresh.PullToRefreshLayout>
PullToRefreshLayout只能包含两个子控件:refresh_head和content_view。
看一下refresh_head的布局:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/head_view"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/light_blue" >
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:paddingBottom="20dp"
- android:paddingTop="20dp" >
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true" >
-
- <ImageView
- android:id="@+id/pull_icon"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_marginLeft="60dp"
- android:background="@drawable/pull_icon_big" />
-
- <ImageView
- android:id="@+id/refreshing_icon"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_marginLeft="60dp"
- android:background="@drawable/refreshing"
- android:visibility="gone" />
-
- <TextView
- android:id="@+id/state_tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:text="@string/pull_to_refresh"
- android:textColor="@color/white"
- android:textSize="16sp" />
-
- <ImageView
- android:id="@+id/state_iv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_marginRight="8dp"
- android:layout_toLeftOf="@id/state_tv"
- android:visibility="gone" />
- </RelativeLayout>
- </RelativeLayout>
-
- </RelativeLayout>
可以根据需要修改refresh_head的布局然后在PullToRefreshLayout中处理,但是相关View的id要和PullToRefreshLayout中用到的保持同步!
接下来是MainActivity的代码:
- package com.jingchen.pulltorefresh;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.support.v4.view.PagerAdapter;
- import android.support.v4.view.ViewPager;
- import android.support.v4.view.ViewPager.OnPageChangeListener;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.view.animation.AnimationUtils;
- import android.view.animation.LinearInterpolator;
- import android.view.animation.RotateAnimation;
- import android.widget.AbsListView;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.AdapterView.OnItemLongClickListener;
- import android.widget.BaseExpandableListAdapter;
- import android.widget.ExpandableListView;
- import android.widget.ExpandableListView.OnChildClickListener;
- import android.widget.ExpandableListView.OnGroupClickListener;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
-
-
-
-
-
-
-
- public class MainActivity extends Activity implements OnRefreshListener, OnClickListener
- {
- private AbsListView alv;
- private PullToRefreshLayout refreshLayout;
- private View loading;
- private RotateAnimation loadingAnimation;
- private TextView loadTextView;
- private MyAdapter adapter;
- private boolean isLoading = false;
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }
-
- private void init()
- {
- alv = (AbsListView) findViewById(R.id.content_view);
- refreshLayout = (PullToRefreshLayout) findViewById(R.id.refresh_view);
- refreshLayout.setOnRefreshListener(this);
- initListView();
-
- loadingAnimation = (RotateAnimation) AnimationUtils.loadAnimation(this, R.anim.rotating);
-
- LinearInterpolator lir = new LinearInterpolator();
- loadingAnimation.setInterpolator(lir);
- }
-
-
-
-
- private void initListView()
- {
- List<String> items = new ArrayList<String>();
- for (int i = 0; i < 30; i++)
- {
- items.add("这里是item " + i);
- }
-
- View headView = getLayoutInflater().inflate(R.layout.listview_head, null);
- ((ListView) alv).addHeaderView(headView, null, false);
-
- View footerView = getLayoutInflater().inflate(R.layout.load_more, null);
- loading = footerView.findViewById(R.id.loading_icon);
- loadTextView = (TextView) footerView.findViewById(R.id.loadmore_tv);
- ((ListView) alv).addFooterView(footerView, null, false);
- footerView.setOnClickListener(this);
- adapter = new MyAdapter(this, items);
- alv.setAdapter(adapter);
- alv.setOnItemLongClickListener(new OnItemLongClickListener()
- {
-
- @Override
- public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
- {
- Toast.makeText(MainActivity.this, "LongClick on " + parent.getAdapter().getItemId(position), Toast.LENGTH_SHORT).show();
- return true;
- }
- });
- alv.setOnItemClickListener(new OnItemClickListener()
- {
-
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id)
- {
- Toast.makeText(MainActivity.this, " Click on " + parent.getAdapter().getItemId(position), Toast.LENGTH_SHORT).show();
- }
- });
- }
-
-
-
-
- private void initGridView()
- {
- List<String> items = new ArrayList<String>();
- for (int i = 0; i < 30; i++)
- {
- items.add("这里是item " + i);
- }
- adapter = new MyAdapter(this, items);
- alv.setAdapter(adapter);
- alv.setOnItemLongClickListener(new OnItemLongClickListener()
- {
-
- @Override
- public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
- {
- Toast.makeText(MainActivity.this, "LongClick on " + parent.getAdapter().getItemId(position), Toast.LENGTH_SHORT).show();
- return true;
- }
- });
- alv.setOnItemClickListener(new OnItemClickListener()
- {
-
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id)
- {
- Toast.makeText(MainActivity.this, " Click on " + parent.getAdapter().getItemId(position), Toast.LENGTH_SHORT).show();
- }
- });
- }
-
-
-
-
- private void initExpandableListView()
- {
- ((ExpandableListView) alv).setAdapter(new ExpandableListAdapter(this));
- ((ExpandableListView) alv).setOnChildClickListener(new OnChildClickListener()
- {
-
- @Override
- public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
- {
- Toast.makeText(MainActivity.this, " Click on group " + groupPosition + " item " + childPosition, Toast.LENGTH_SHORT).show();
- return true;
- }
- });
- ((ExpandableListView) alv).setOnItemLongClickListener(new OnItemLongClickListener()
- {
-
- @Override
- public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
- {
- Toast.makeText(MainActivity.this, "LongClick on " + parent.getAdapter().getItemId(position), Toast.LENGTH_SHORT).show();
- return true;
- }
- });
- ((ExpandableListView) alv).setOnGroupClickListener(new OnGroupClickListener()
- {
-
- @Override
- public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id)
- {
- if (parent.isGroupExpanded(groupPosition))
- {
-
- parent.collapseGroup(groupPosition);
- } else
- {
-
- parent.expandGroup(groupPosition);
- }
- return true;
- }
- });
- }
-
- @Override
- public void onRefresh()
- {
-
- new Handler()
- {
- @Override
- public void handleMessage(Message msg)
- {
- refreshLayout.refreshFinish(PullToRefreshLayout.REFRESH_SUCCEED);
- }
- }.sendEmptyMessageDelayed(0, 5000);
- }
-
- @Override
- public void onClick(View v)
- {
- switch (v.getId())
- {
- case R.id.loadmore_layout:
- if (!isLoading)
- {
- loading.setVisibility(View.VISIBLE);
- loading.startAnimation(loadingAnimation);
- loadTextView.setText(R.string.loading);
- isLoading = true;
- }
- break;
- default:
- break;
- }
-
- }
-
- class ExpandableListAdapter extends BaseExpandableListAdapter
- {
- private String[] groupsStrings;
-
- private String[][] groupItems;
- private Context context;
-
- public ExpandableListAdapter(Context context)
- {
- this.context = context;
- groupsStrings = new String[8];
- for (int i = 0; i < groupsStrings.length; i++)
- {
- groupsStrings[i] = new String("这里是group " + i);
- }
- groupItems = new String[8][8];
- for (int i = 0; i < groupItems.length; i++)
- for (int j = 0; j < groupItems[i].length; j++)
- {
- groupItems[i][j] = new String("这里是group " + i + "里的item " + j);
- }
- }
-
- @Override
- public int getGroupCount()
- {
- return groupsStrings.length;
- }
-
- @Override
- public int getChildrenCount(int groupPosition)
- {
- return groupItems[groupPosition].length;
- }
-
- @Override
- public Object getGroup(int groupPosition)
- {
- return groupsStrings[groupPosition];
- }
-
- @Override
- public Object getChild(int groupPosition, int childPosition)
- {
- return groupItems[groupPosition][childPosition];
- }
-
- @Override
- public long getGroupId(int groupPosition)
- {
- return groupPosition;
- }
-
- @Override
- public long getChildId(int groupPosition, int childPosition)
- {
- return childPosition;
- }
-
- @Override
- public boolean hasStableIds()
- {
- return true;
- }
-
- @Override
- public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
- {
- View view = LayoutInflater.from(context).inflate(R.layout.list_item_layout, null);
- TextView tv = (TextView) view.findViewById(R.id.name_tv);
- tv.setText(groupsStrings[groupPosition]);
- return view;
- }
-
- @Override
- public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
- {
- View view = LayoutInflater.from(context).inflate(R.layout.list_item_layout, null);
- TextView tv = (TextView) view.findViewById(R.id.name_tv);
- tv.setText(groupItems[groupPosition][childPosition]);
- return view;
- }
-
- @Override
- public boolean isChildSelectable(int groupPosition, int childPosition)
- {
- return true;
- }
-
- }
-
- }
在MainActivity中判断contentView是ListView的话给ListView添加了FooterView实现点击加载更多的功能。这只是一个演示PullToRefreshLayout使用的demo,可以参照一下修改。我已经在里面写了ListView,GridView和ExpandableListView的初始化方法,根据自己使用的是哪个来调用吧。那么这是ListView的下拉刷新和加载更多。如果我要GridView也有下拉刷新功能呢?那就把MainActivity的布局换成这样:
- <com.jingchen.pulltorefresh.PullToRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/refresh_view"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
-
- <include layout="@layout/refresh_head" />
-
- <GridView
- android:id="@+id/content_view"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/white"
- android:columnWidth="90dp"
- android:gravity="center"
- android:horizontalSpacing="10dp"
- android:numColumns="auto_fit"
- android:stretchMode="columnWidth"
- android:verticalSpacing="15dp" >
- </GridView>
-
- </com.jingchen.pulltorefresh.PullToRefreshLayout>
如果是ExpandableListView则把布局改成这样:
- <com.jingchen.pulltorefresh.PullToRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/refresh_view"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
-
- <include layout="@layout/refresh_head" />
-
- <ExpandableListView
- android:id="@+id/content_view"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/white" >
- </ExpandableListView>
-
- </com.jingchen.pulltorefresh.PullToRefreshLayout>
怎么样?很简单吧?简单易用,不用再去继承修改了~
点击下载源码