swipRefresh在fragment中使用

来源:互联网 发布:sql三大部分 编辑:程序博客网 时间:2024/06/09 20:19

首先在xml布局中声明swiperefresh控件,直接上代码:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/swipeRefresh"    android:orientation="vertical"    tools:context="com.example.administrator.ordersystem.fragment.StayOperaterFragment">    <ScrollView        android:layout_width="match_parent"        android:layout_height="match_parent">    <LinearLayout        android:orientation="vertical"        android:layout_width="match_parent"        android:layout_height="match_parent">        <android.support.v7.widget.Toolbar            android:background="#309cfc"            android:layout_width="match_parent"            android:layout_height="wrap_content">            <TextView                android:id="@+id/title_number"                android:textSize="20dp"                android:textColor="#fff"                android:gravity="center"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:text="待操作"/>        </android.support.v7.widget.Toolbar>        <ListView            android:id="@+id/operator_listview"            android:layout_width="match_parent"            android:layout_height="match_parent">        </ListView>    </LinearLayout>    </ScrollView></android.support.v4.widget.SwipeRefreshLayout>

然后在fragment代码中:

public class StayOperaterFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener{    private ListView listView;    private List<ListInfoBean> list;    private String  strNO,strName;    private  ListInfoAdapter adapter;    private TextView title_number;    private SwipeRefreshLayout swipeRefresh;    @Override    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.fragment_stay_operater, container, false);        listView = (ListView) view.findViewById(R.id.operator_listview);        title_number = (TextView) view.findViewById(R.id.title_number);        swipeRefresh = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefresh);               init();        //设置刷新出现的位置        //        swipeRefresh.setProgressViewEndTarget(false, 200);//绑定监听       swipeRefresh.setOnRefreshListener(this);//设置颜色 swipeRefresh.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light);        swipeRefresh.setDistanceToTriggerSync(50);            @Override    public void onRefresh() {                  list.clear();                     init()new Handler().postDelayed(new Runnable() {                    @Override                    public void run() {                        swipeRefresh.setRefreshing(false);                    }                },4000);            }
基本就是这样的,就可以实现下拉刷新了,刷新数据可以在onRerfresh方法中

1 0
原创粉丝点击