下滑刷新-----SwipeRefreshLayout

android最近推出的一个更新,竟然包含了下滑刷新,效果还不错,分享一下,探讨一下。

建立项目时要把最小支持的android系统设置为3.0,如果要兼容2.2可以向下兼容。

首先是一个fragment.xml的布局:

<android.support.v4.widget.SwipeRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/swipeRefreshLayout"
    >
    <ScrollView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="下滑刷新"/>
    </ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>

在这个文件中一定要注意:android.support.v4.widget.SwipeRefreshLayout必须是全名,而且这中间必须是一个可滑动组件ScrollView 或listView(不然还怎么下滑刷新)

然后准备一下fragment,并且设置OnRefreshListener监听:代码如下

public class PlaceholderFragment extends Fragment implements OnRefreshListener{
    
    private SwipeRefreshLayout srl;
    @SuppressLint("InlinedApi")
    /*准备fragment,
     * 他生命周期中有许多方法,但是我们可以不复写,但是要想显示他
     * 我们就必须写这个方法
     * */
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState){
        
        View view = inflater.inflate(R.layout.fragment_main, null);
        //可以在这中间设置一些组件的效果
        srl = (SwipeRefreshLayout)view.findViewById(R.id.swipeRefreshLayout);
        //这个方法有四个参数,是四种颜色,就是下滑时闪闪的颜色
        srl.setColorScheme(android.R.color.holo_blue_bright,
        android.R.color.holo_green_light,
        android.R.color.holo_red_light,
        android.R.color.holo_orange_light);
        //为SwipeRefreshLayout加上监听
        srl.setOnRefreshListener(this);
        return view;
        
    }
    @Override
    //实现接口要写的方法
    public void onRefresh() {
        //为模拟下滑刷新而定义的线程
         new Thread(){

             @Override
             public void run() {
                 try {
                     sleep(3000);
                     srl.setRefreshing(false);
                 } catch (InterruptedException e) {
                     e.printStackTrace();
                 }    
             }
         }.start();
        
    }

}

然后fragment准备好了,我们就把他加入MainActivity吧,代码如下:

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*使用Fragment时,可以通过用户交互来执行一些动作,比如增加、移除、替换等。
         * 所有这些改变构成一个集合,这个集合被叫做一个transaction。
         * 可以调用FragmentTransaction中的方法来处理这个transaction,
         * 并且可以将transaction存进由activity管理的back stack中,
         * 这样用户就可以进行fragment变化的回退操作。
         * */
        //获得fragment的管理器
        FragmentManager mFragmentManager = getFragmentManager();
        /*得到变换的一个引用,调用了增加方法,将 PlaceholderFragment
              加入了R.id.container代表的布局组件中*/
        FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.add(R.id.container,new PlaceholderFragment());
        //这个变换,无疑需要提交
        mFragmentTransaction.commit();    
    }
}

这下就把它加入了MainActivity,但是我们发现那个R.id.container不知道代表什么,其实他是activityb_main.xml中的布局

代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.swipereflesh.MainActivity"
    tools:ignore="MergeRootFrame"/ >

到这下滑刷新就完成了,哈哈、、、、、赶快看一下效果吧

        

注意:想使用这个组件,你的支持库必须是19.1。

还是愉快的一天吗?好忧伤、、还有好多不会,离技术转化为金钱还有好远------------一个渣渣的梦

原文:BlueLuffy   博客园

原文地址:https://www.cnblogs.com/969059506-java/p/3677472.html