下拉刷新

主要是参考  http://blog.csdn.net/lmj623565791/article/details/24521483


关于这个下拉刷新还是比较简单的,就看在实际中怎么用啦 。


下拉刷新使用的时Google原生态的库中的,也不知道为啥总是喜欢这种官方的。

引用的是  android.support.v4.widget.SwipeRefreshLayout;这个类。

总体功能就是下滑一下 数字增加1


1、布局文件

<?xml version="1.0" encoding="utf-8"?>
<!--<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hejingzhou.swiperefreshdemo.MainActivity">-->

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:addStatesFromChildren="false"
    android:background="#990033"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:background="#990033"
                android:text="rrrrrrrrrrrrrrr"
                android:textSize="40dp" />
        </RelativeLayout>
    </android.support.v4.widget.SwipeRefreshLayout>


<!--</RelativeLayout>-->

主要就是将所需要的内容用      <android.support.v4.widget.SwipeRefreshLayout>滑动区域控件< android.support.v4.widget.SwipeRefreshLayout/>  这个布局包裹起来就行了。


SwipeRefreshLayout常用方法:



        1、setOnRefreshListener(OnRefreshListener listener)  设置下拉监听,当用户下拉的时候会去执行回调
        2、setColorSchemeColors(int... colors) 设置 进度条的颜色变化,最多可以设置4种颜色
        3、setProgressViewOffset(boolean scale, int start, int end) 调整进度条距离屏幕顶部的距离
        4、setRefreshing(boolean refreshing) 设置SwipeRefreshLayout当前是否处于刷新状态,一般是在请求数据的时候设置为true,在数据被加载到View中后,设置为false。

2、MainActivity

package com.example.hejingzhou.swiperefreshdemo;


import android.app.Service;
import android.os.Message;
import android.os.Vibrator;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
    private String TAG = getClass().getSimpleName();
    private static final int REFERSH_COMPLETE = 0x00;//刷新标志
    private SwipeRefreshLayout refreshLayout;
    private TextView textView;
    private int i = 0;
    private android.os.Handler handler = new android.os.Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case REFERSH_COMPLETE:
                    Log.i(TAG, "我想吃麻辣烫");
                    i++;
                    textView.setText("" + i);
                    refreshLayout.setRefreshing(false);//通知小部件,它刷新状态已更改 改成true后刷新箭头刷新后不再回去
                    break;
            }
        }
    };


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

    /**
     * 初始化 加载时进行刷新一下数据
     */
    private void initView() {
        textView = (TextView) findViewById(R.id.textView);
        textView.setText("0");
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setColorSchemeResources(R.color.colorPrimary);//colorAccent圆箭头
        refreshLayout.setRefreshing(true);//true加载View时刷新
        refreshLayout.setProgressViewOffset(true, 20, 30);
        handler.sendEmptyMessageDelayed(REFERSH_COMPLETE, 500);//首次加载一次线程
        refreshLayout.setSize(SwipeRefreshLayout.LARGE);//设置下拉动画的大小就两个值
    }

    @Override
    public void onRefresh() {
        shock(100);
        handler.sendEmptyMessageDelayed(REFERSH_COMPLETE, 2000);
    }

    /**
     * 下拉震动
     * 需要添加权限  <uses-permission android:name="android.permission.VIBRATE" />
     *
     * @param time
     */
    private void shock(int time) {
        //Toast.makeText(getApplicationContext(),"正在刷新数据...",Toast.LENGTH_SHORT).show();
        Vibrator vib = (Vibrator) getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
        vib.vibrate(time);
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值