学习地址:麦子学院 Android 高级UI 课程
1布局文件中使用android.support.v4.widger.SwipeRefreshLayout
2.布局文件中的SwipeRefreshLayout只能包含一个child 这个Child必须是可滚动的view,如ListView,ScrollView
3.实例化SwipeRefreshLayout的Activity,要实现OnRefreshListener借口 ,实现onRefresh()方法,为SwipeRefreshLayout设置RefreshListener
4.调用SwipeRefreshLayout的setRefreshing(true)方法,以显示刷新过程动画
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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="horizontal">
<TextView
android:id="@+id/generate_random"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pull_to_random"
android:textSize="15sp" />
<TextView
android:id="@+id/random"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="15sp" />
</LinearLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private SwipeRefreshLayout mSwipeRefreshLayout;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
mSwipeRefreshLayout.setOnRefreshListener(this);
mTextView = (TextView) findViewById(R.id.random);
}
@Override
public void onRefresh() {
mSwipeRefreshLayout.setRefreshing(true);
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
mSwipeRefreshLayout.setRefreshing(false);//停止动画
int i = (int) (Math.random() * 100 + 1); // 产生一个1~100的整数
mTextView.setText(String.valueOf(i));
}
},3000);
}
}