首页 > 其他 > 详细

下滑刷新-----SwipeRefreshLayout

时间:2014-04-21 18:36:54      阅读:669      评论:0      收藏:0      [点我收藏+]

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

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

bubuko.com,布布扣
<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>
bubuko.com,布布扣

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

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

bubuko.com,布布扣
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();
        
    }

}
bubuko.com,布布扣

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

bubuko.com,布布扣
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();    
    }
}
bubuko.com,布布扣

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

代码如下:

bubuko.com,布布扣
<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"/ >
bubuko.com,布布扣

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

bubuko.com,布布扣        bubuko.com,布布扣

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

下滑刷新-----SwipeRefreshLayout,布布扣,bubuko.com

下滑刷新-----SwipeRefreshLayout

原文:http://www.cnblogs.com/969059506-java/p/3677472.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!