1.首先创建一个头部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:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/head_contentLayout"
- android:paddingLeft="30dp"
- >
-
-
- <FrameLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_centerVertical="true"
- >
-
-
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- android:src="@drawable/pull_down_arrow"
- android:id="@+id/head_arrowImageView"
- />
-
-
- <ProgressBar
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- style="?android:attr/progressBarStyleSmall"
- android:layout_gravity="center"
- android:id="@+id/head_progressBar"
-
- android:visibility="gone"
- />
-
- </FrameLayout>
-
-
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true"
- android:orientation="vertical"
- android:gravity="center_horizontal"
- >
-
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="下拉刷新"
- android:textColor="@color/white"
- android:textSize="20sp"
- android:id="@+id/head_tipsTextView"
- />
-
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/head_lastUpdatedTextView"
- android:text="上次更新"
- android:textColor="@color/gold"
- android:textSize="10sp"
- />
-
- </LinearLayout>
-
-
- </RelativeLayout>
-
-
- </LinearLayout>
2.然后写一个class:
- package com.laohuai.appdemo.customui.ui;
-
- import java.util.Date;
-
- import com.laohuai.appdemo.customui.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.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.AbsListView.OnScrollListener;
- import android.widget.ProgressBar;
- import android.widget.TextView;
-
- 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;
-
-
- 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;
-
-
- 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) {
- setCacheColorHint(context.getResources().getColor(R.color.transparent));
- inflater = LayoutInflater.from(context);
-
- headView = (LinearLayout) inflater.inflate(R.layout.head, null);
-
- arrowImageView = (ImageView) headView
- .findViewById(R.id.head_arrowImageView);
- arrowImageView.setMinimumWidth(70);
- arrowImageView.setMinimumHeight(50);
- 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);
- 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);
-
- animation = new RotateAnimation(0, -180,
- RotateAnimation.RELATIVE_TO_SELF, 0.5f,
- RotateAnimation.RELATIVE_TO_SELF, 0.5f);
- animation.setInterpolator(new LinearInterpolator());
- animation.setDuration(250);
- 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(200);
- 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, "在down时候记录当前位置‘");
- }
- 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) {
-
-
-
-
- if (state == RELEASE_To_REFRESH) {
-
- setSelection(0);
-
-
- if (((tempY - startY) / RATIO < headContentHeight)
- && (tempY - startY) > 0) {
- state = PULL_To_REFRESH;
- changeHeaderViewByState();
-
- Log.v(TAG, "由松开刷新状态转变到下拉刷新状态");
- }
-
- else if (tempY - startY <= 0) {
- state = DONE;
- changeHeaderViewByState();
-
- Log.v(TAG, "由松开刷新状态转变到done状态");
- }
-
- else {
-
- }
- }
-
- if (state == PULL_To_REFRESH) {
-
- setSelection(0);
-
-
- if ((tempY - startY) / RATIO >= headContentHeight) {
- state = RELEASE_To_REFRESH;
- isBack = true;
- changeHeaderViewByState();
-
- Log.v(TAG, "由done或者下拉刷新状态转变到松开刷新");
- }
-
- else if (tempY - startY <= 0) {
- state = DONE;
- changeHeaderViewByState();
-
- Log.v(TAG, "由DOne或者下拉刷新状态转变到done状态");
- }
- }
-
-
- if (state == DONE) {
- if (tempY - startY > 0) {
- state = PULL_To_REFRESH;
- changeHeaderViewByState();
- }
- }
-
-
- if (state == PULL_To_REFRESH) {
- headView.setPadding(0, -1 * headContentHeight
- + (tempY - startY) / RATIO, 0, 0);
-
- }
-
-
- 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:
- 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:
- 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);
-
- 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);
-
- progressBar.setVisibility(View.GONE);
- arrowImageView.clearAnimation();
- arrowImageView.setImageResource(R.drawable.pull_down_arrow);
- tipsTextview.setText("下拉刷新");
- lastUpdatedTextView.setVisibility(View.VISIBLE);
-
- Log.v(TAG, "当前状态,done");
- break;
- }
- }
-
- public void setonRefreshListener(OnRefreshListener refreshListener) {
- this.refreshListener = refreshListener;
- isRefreshable = true;
- }
-
- public interface OnRefreshListener {
- public void onRefresh();
- }
-
- public void onRefreshComplete() {
- state = DONE;
- lastUpdatedTextView.setText("最近更新:" + new Date().toLocaleString());
- changeHeaderViewByState();
- }
-
- private void onRefresh() {
- if (refreshListener != null) {
- refreshListener.onRefresh();
- }
- }
-
-
- private void measureView(View child) {
- ViewGroup.LayoutParams p = child.getLayoutParams();
- if (p == null) {
- p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
- ViewGroup.LayoutParams.WRAP_CONTENT);
- }
- 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) {
- lastUpdatedTextView.setText("最近更新:" + new Date().toLocaleString());
- super.setAdapter(adapter);
- }
-
- }
3.在main.xml 中调用上面的这个class:
- <?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="#FFFFFF">
-
- <com.laohuai.appdemo.customui.ui.MyListView
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:id="@+id/listView"
- android:listSelector="@android:color/transparent"
- />
-
- </LinearLayout>
4.实现Activity:
- package com.laohuai.appdemo.customui;
-
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
-
- import com.laohuai.appdemo.customui.ui.MyListView;
- import com.laohuai.appdemo.customui.ui.MyListView.OnRefreshListener;
-
- import android.app.Activity;
- import android.content.Context;
- import android.content.res.Resources;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.TextView;
-
- public class MainActivity extends Activity {
- HashMap<String, Object> maps;
- MyListView listView;
-
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- listView = (MyListView) findViewById(R.id.listView);
- getData();
- }
-
- private void getData() {
- List<HashMap<String, Object>> lists = new ArrayList<HashMap<String, Object>>();
-
- for (int i = 0; i < 40; i++) {
-
- maps = new HashMap<String, Object>();
- maps.put("test", "搞定了没有啊");
-
- lists.add(maps);
- }
- final ListAdapter adapter = new ListAdapter(MainActivity.this, lists);
- listView.setAdapter(adapter);
-
- listView.setonRefreshListener(new OnRefreshListener() {
- public void onRefresh() {
- new AsyncTask<Void, Void, Void>() {
- protected Void doInBackground(Void... params) {
- try {
- Thread.sleep(1000);
- } catch (Exception e) {
- e.printStackTrace();
- }
- maps.get("刷新后添加的内容");
- return null;
- }
-
- protected void onPostExecute(Void result) {
- adapter.notifyDataSetChanged();
- listView.onRefreshComplete();
- }
-
- }.execute(null);
- }
- });
- }
-
- class ListAdapter extends BaseAdapter {
-
- private Context mContext;
- private List<HashMap<String, Object>> data;
-
- public ListAdapter(Context mContext, List<HashMap<String, Object>> data) {
- super();
- this.mContext = mContext;
- this.data = data;
- }
-
- public int getCount() {
-
- return data.size();
- }
-
- public Object getItem(int position) {
-
- return position;
- }
-
- public long getItemId(int position) {
-
- return position;
- }
-
- public View getView(int position, View convertView, ViewGroup parent) {
- TextView tv = new TextView(getApplicationContext());
- tv.setText((String)data.get(position).get("test"));
- Resources rs = getResources();
- tv.setTextColor(rs.getColor(R.color.white));
- return tv;
- }
-
- }
- }
分享到:
相关推荐
在Android开发中,上拉加载和下拉刷新是常见的组件功能,用于提升用户体验,使得用户在滚动列表到顶部时能够方便地获取更多数据,而在滚动到底部时加载更多内容。本示例“Android自定义上拉加载下拉刷新控件”提供了...
在Android应用开发中,下拉刷新(Pull-to-Refresh)是一种常见的交互设计,它允许用户通过在顶部向下拉动列表来加载更多数据或更新内容。在这个案例中,我们讨论的主题是"android仿美团下拉刷新效果",这表示我们将...
这个"Android listView下拉刷新上拉刷新带阻尼效果"的源码Demo是几年前的一个示例,旨在帮助学生理解和实现Android应用中的下拉刷新和上拉加载更多功能,同时加入了阻尼效果,提升用户体验。阻尼效果是指在用户滑动...
在Android Studio中,你可以使用第三方库如SwipeRefreshLayout来轻松实现下拉刷新。在build.gradle模块文件中添加依赖: ```groovy dependencies { implementation 'androidx.swiperefreshlayout:...
本篇文章将详细讲解如何在Android中实现ListView的下拉刷新功能,以达到类似手机微博的用户体验。 首先,我们需要了解下拉刷新的基本原理。在Android中,下拉刷新通常由一个可扩展的Header View组成,当用户下拉时...
总结,下拉刷新和上拉加载是Android应用中常见的功能,通过SwipeRefreshLayout和OnScrollListener可以方便地实现。自定义ListView则允许开发者更深入地控制列表的行为和视觉效果,以提供更加个性化的用户体验。在...
在Android应用开发中,"下拉刷新"和"上拉加载"是常见的功能,用于提供流畅的用户体验,尤其是在处理大量数据列表时。本教程将详细讲解如何在Android中实现自定义的下拉刷新和上拉加载功能。 首先,我们要了解这两个...
首先,我们要引入SwipeRefreshLayout库,它是Android SDK提供的一个下拉刷新框架。在`build.gradle`文件中添加以下依赖: ```groovy dependencies { implementation 'com.android.support:support-v4:版本号' } ``...
开发者需要确保在适配器的`OnScrollListener`中正确处理下拉刷新的状态。 通过分析和学习这个源码,开发者可以了解到如何在Android应用中实现自定义的下拉刷新效果,以及如何在`FrameLayout`基础上扩展功能。这对于...
在Android应用开发中,"下拉刷新"和"上拉加载更多"是常见的用户体验功能,尤其是在数据列表展示中,如GridView、ListView和ScrollView等。这些功能使得用户在滚动到列表顶部时可以更新内容(下拉刷新),而在滚动到...
在Android开发中,"谷歌原生下拉刷新"是指使用Google官方提供的SwipeRefreshLayout组件来实现一个常见的用户界面交互效果——下拉刷新。这个组件被广泛应用于列表或者网格视图,当用户从顶部向下滑动时,可以触发...
综上所述,Android ListView的下拉刷新和上拉加载更多功能是现代移动应用中不可或缺的组件,它们提升了用户体验,使得数据更新和加载更加便捷。开发者可以通过多种方式实现这些功能,但需要注意性能优化和用户体验的...
在Android开发中,"下拉刷新"和"上拉加载更多"是常见的用户交互功能,尤其是在使用RecyclerView等列表视图时。RecyclerView是一个高效且灵活的布局管理器,它替代了ListView,提供了更好的性能和更多的定制选项。本...
综上所述,Android中的下拉刷新、上拉加载和滑动删除功能极大地提高了用户体验。通过SwipeRefreshLayout、自定义OnScrollListener以及第三方库,我们可以轻松地在ListView中实现这些功能,使应用更加人性化和互动。...
在Android应用开发中,"下拉刷新"和"加载更多"是常见的功能,尤其是在列表或者网格视图中,用户可以通过下拉手势更新数据,而滚动到底部时则自动加载更多内容。这两个特性极大地提升了用户体验,使得用户能够方便地...
在实现自定义下拉刷新时,可以使用`OnScrollListener`监听ListView的滑动,通过计算偏移量来判断是否需要显示刷新头部。同时,需要结合`Animation`或`ObjectAnimator`来创建平滑的动画效果。 总结来说,ListView的...
在Android应用开发中,下拉刷新功能已经成为许多应用的标准特性,尤其在列表视图和滚动内容中非常常见。这个DEMO是关于如何在Android平台上实现下拉刷新效果的一个实例,对于初学者和经验丰富的开发者来说,都是一个...
在Android开发中,ListView是一种常用的组件,用于展示大量的数据列表。然而,为了提供更好的用户体验,很多应用都会在ListView顶部添加下拉刷新的...但不论使用哪个组件,下拉刷新都是提高用户体验的重要特性之一。
本资料包主要涵盖了在Android环境下实现ListView的下拉刷新、上拉加载以及列表头的横向滚动,包括带有表头和固定列的复杂布局。 首先,下拉刷新通常采用SwipeRefreshLayout组件,它提供了一个易于使用的接口,可以...