zoukankan      html  css  js  c++  java
  • 使用Google API 下拉刷新或者增加数据 SwipeRefreshLayout

    贴出布局代码:

    <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/id_swipe_ly"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:clickable="true">
                <TextView
                    android:id="@+id/id_listview"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="按钮"/>
            </LinearLayout>
        </android.support.v4.widget.SwipeRefreshLayout>

    根据我的不完全实验 SwipeRefreshLayout 内可以使用所有控件,但是前提是必须在不能点击的控件上 写上这个 android:clickable=“true”  ,不然会有些问题。虽然数据依然能加载出来但是加载图标就会闪现(可以随意尝试)

    使用这个 SwipeRefreshLayout 必须实现这个接口:

    public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener  
    然后会自动生成它的一个方法:
    public void onRefresh() {

    }
    可以在这里面写你的一系列操作,但是不建议写耗时的操作,可以写一个Handler 来接收信息 并加载信息。

    全部代码:
    布局文件
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/id_swipe_ly"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:clickable="true">
                <TextView
                    android:id="@+id/id_listview"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="按钮"/>
            </LinearLayout>
        </android.support.v4.widget.SwipeRefreshLayout>
    </RelativeLayout>

    MainAcitivity

    public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {
    
        private static final int REFRESH_COMPLETE = 0X110;
        private SwipeRefreshLayout mSwipeLayout;
        private TextView mListView;
    
        private Handler mHandler = new Handler() {
            public void handleMessage(android.os.Message msg) {
                switch (msg.what) {
                    case REFRESH_COMPLETE:
    
                        mListView.setText("猪猪侠");
                        mSwipeLayout.setRefreshing(false);
                        break;
    
                }
            }
    
            ;
        };
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mListView = (TextView) findViewById(R.id.id_listview);
            mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
    
            mSwipeLayout.setOnRefreshListener(this);
            mSwipeLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                    android.R.color.holo_orange_light, android.R.color.holo_red_light);
        }
    
        public void onRefresh() {
    
            mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);
        }
    }
    至此就可以完成了 一个简单的刷新!


  • 相关阅读:
    PHP基础学习笔记(一)
    安装wampserver之后,浏览器中输入localhost页面显示IIS7解决办法
    HTML5常识总结(一)
    AngularJs中的服务
    AngularJs中的directives(指令part1)
    Happy Number——LeetCode
    Binary Tree Zigzag Level Order Traversal——LeetCode
    Construct Binary Tree from Preorder and Inorder Traversal——LeetCode
    Construct Binary Tree from Inorder and Postorder Traversal——LeetCode
    Convert Sorted Array to Binary Search Tree——LeetCode
  • 原文地址:https://www.cnblogs.com/android-host/p/5280518.html
Copyright © 2011-2022 走看看