美文网首页
android SwipeRefreshLayout使用

android SwipeRefreshLayout使用

作者: 毕丙伟 | 来源:发表于2017-10-12 09:44 被阅读0次

    1.将需要刷新的view添进SwipeRefreshLayout中,例如:

    <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipe_refresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
    
                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:background="@color/colorPrimary">
    
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:text="Home"
                            android:textSize="20sp"
                            android:textColor="#fff"
                            android:layout_marginTop="25dp"
                            android:layout_centerInParent="true"/>
                    </RelativeLayout>
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                        <TextView
                            android:id="@+id/text_cityName"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textSize="50sp"
                            android:layout_gravity="center"
                            android:layout_marginTop="20dp"/>
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:gravity="center">
                            <TextView
                                android:id="@+id/text_tempNow"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textSize="30sp"
                                />
                            <TextView
                                android:id="@+id/text_nowWeather"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="10dp"
                                android:textSize="30sp"/>
                        </LinearLayout>
                    </LinearLayout>
    
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:background="#778899"
                        android:layout_margin="5dp"/>
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:orientation="horizontal">
                        <LinearLayout
                            android:id="@+id/layout_hourlyForecastWeather"
                            android:orientation="vertical"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            >
                        </LinearLayout>
    
                        <LinearLayout
                            android:id="@+id/layout_forecastWeather"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:orientation="vertical">
                        </LinearLayout>
                    </LinearLayout>
    
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="2dp"
                        android:background="#778899"
                        android:layout_margin="5dp"/>
    
                    <ScrollView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
    
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical">
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="horizontal">
    
                                <LinearLayout
                                    android:layout_width="0dp"
                                    android:layout_height="wrap_content"
                                    android:layout_weight="1"
                                    android:orientation="vertical">
    
                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:gravity="end"
                                        android:text="PM2.5:"
                                        android:textSize="20sp" />
    
                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:gravity="end"
                                        android:text="空气质量:"
                                        android:textSize="20sp" />
    
                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:layout_marginTop="10dp"
                                        android:gravity="end"
                                        android:text="日出:"
                                        android:textSize="20sp" />
    
                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:gravity="end"
                                        android:text="日落:"
                                        android:textSize="20sp" />
    
                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:layout_marginTop="10dp"
                                        android:gravity="end"
                                        android:text="降雨概率:"
                                        android:textSize="20sp" />
    
                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:gravity="end"
                                        android:text="相对湿度:"
                                        android:textSize="20sp" />
    
                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:layout_marginTop="10dp"
                                        android:gravity="end"
                                        android:text="能见度:"
                                        android:textSize="20sp" />
    
                                    <TextView
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:gravity="end"
                                        android:text="风力等级:"
                                        android:textSize="20sp" />
                                </LinearLayout>
    
                                <LinearLayout
                                    android:layout_width="0dp"
                                    android:layout_height="wrap_content"
                                    android:layout_weight="1"
                                    android:orientation="vertical">
    
                                    <TextView
                                        android:id="@+id/text_pm25"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:gravity="left"
                                        android:text="迷路啦"
                                        android:textSize="20sp" />
    
                                    <TextView
                                        android:id="@+id/text_quality"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:gravity="left"
                                        android:text="迷路啦"
                                        android:textSize="20sp" />
    
                                    <TextView
                                        android:id="@+id/text_sunRaise"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:layout_marginTop="10dp"
                                        android:gravity="left"
                                        android:text="测试"
                                        android:textSize="20sp" />
    
                                    <TextView
                                        android:id="@+id/text_sunDown"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:gravity="left"
                                        android:text="测试"
                                        android:textSize="20sp" />
    
                                    <TextView
                                        android:id="@+id/text_rain"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:layout_marginTop="10dp"
                                        android:gravity="left"
                                        android:text="测试"
                                        android:textSize="20sp" />
    
                                    <TextView
                                        android:id="@+id/text_humidity"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:gravity="left"
                                        android:text="测试"
                                        android:textSize="20sp" />
    
                                    <TextView
                                        android:id="@+id/text_visibility"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:layout_marginTop="10dp"
                                        android:gravity="left"
                                        android:text="测试"
                                        android:textSize="20sp" />
    
                                    <TextView
                                        android:id="@+id/text_wind"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:gravity="left"
                                        android:text="测试"
                                        android:textSize="20sp" />
                                </LinearLayout>
                            </LinearLayout>
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_margin="20dp">
                                <TextView
                                    android:id="@+id/text_todayDescriable"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:gravity="start"
                                    android:text=""
                                    android:textSize="20sp"/>
                            </LinearLayout>
                        </LinearLayout>
    
                    </ScrollView>
                </LinearLayout>
        </ScrollView>
        </android.support.v4.widget.SwipeRefreshLayout>
    

    2.进行初始化

    private SwipeRefreshLayout swipeRefreshLayout;
    swipeRefreshLayout = view.findViewById(R.id.swipe_refresh);
    

    3.设置下拉刷新小圈圈的颜色

    swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
    

    4.设置监听器

    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    requestWeatherInfo(mWeatherId);//刷新之后需要进行的操作
                }
            });
    

    5.刷新结束,并隐藏刷新标志

    swipeRefreshLayout.setRefreshing(false);
    

    6.关于刷新插件的更多用法请参考https://developer.android.google.cn/reference/android/support/v4/widget/SwipeRefreshLayout.html

    相关文章

      网友评论

          本文标题:android SwipeRefreshLayout使用

          本文链接:https://www.haomeiwen.com/subject/jplnyxtx.html