您的位置:首页 > 编程语言 > Go语言

使用Google API 下拉刷新或者增加数据 SwipeRefreshLayout

2016-03-15 18:01 441 查看
贴出布局代码:

<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);
}
}


至此就可以完成了 一个简单的刷新!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: