SwipeRefreshLayout的使用方法

来源:互联网 发布:英雄联盟单机版mac 编辑:程序博客网 时间:2024/06/05 17:54

SwipeRefreshLayout其实是一个viewgroup,它其中只能含有一个子view.用法很简单,



activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:layout_width="match_parent"              android:layout_height="match_parent">    <android.support.v4.widget.SwipeRefreshLayout        xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"        android:id="@+id/swipeLayout" >        <ListView            android:id="@+id/listview"            android:layout_width="match_parent"            android:layout_height="wrap_content"/>    </android.support.v4.widget.SwipeRefreshLayout></LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="wrap_content"    >    <TextView        android:id="@+id/item_tv"        android:layout_width="match_parent"        android:layout_height="60dp"        android:text="TO BE BETTER!"        android:textSize="18dp"        android:gravity="center"        android:background="#cde78a"        /></RelativeLayout>

MainActivity:
public class MainActivity extends AppCompatActivity  {    ListView listview;    List<String> list;    SwipeRefreshLayout srfl;    MyAdapter adapter;    Handler h;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_main);        h=new Handler(){            @Override            public void handleMessage(Message message) {                adapter.notifyDataSetChanged();                srfl.setRefreshing(false);            }        };                        srfl = (SwipeRefreshLayout) findViewById(R.id.swipeLayout);
//设置swiperefreshlayout        refresh();                listview = (ListView) findViewById(R.id.listview);        //初始化listview数据
list=new ArrayList<String>();        for (int i=0;i<30;i++) {            list.add("BETTER "+i);        }        adapter = new MyAdapter(list);        listview.setAdapter(adapter);            }            //listView的adapter    class MyAdapter extends BaseAdapter{        List<String> list;        public MyAdapter(List<String> list) {            this.list=list;        }        @Override        public int getCount() {            return list.size();        }        @Override        public Object getItem(int position) {            return list.get(position);        }        @Override        public long getItemId(int position) {            return position;        }        @Override        public View getView(int position, View convertView, ViewGroup parent) {            Holder h;            if (convertView == null) {                convertView = MainActivity.this.getLayoutInflater().inflate(R.layout.item, null);                h = new Holder();                h.tv = (TextView) convertView.findViewById(R.id.item_tv);                convertView.setTag(h);            } else {                h=(Holder)convertView.getTag();            }            if (position % 6 == 0) {                h.tv.setBackgroundColor(Color.parseColor("#78aced"));            } else {                h.tv.setBackgroundColor(Color.parseColor("#cde78a"));            }            h.tv.setText(list.get(position));            return convertView;        }        class Holder{            public TextView tv;        }    }        public void refresh(){//        设置小球弹回后的位置//        srfl.setProgressViewEndTarget(true, 180);//        设置小球开始和结束的位置//        srfl.setProgressViewOffset(true,400,500);//        设置圆球的大小  只有两种//        srfl.setSize(SwipeRefreshLayout.LARGE);//        小圆球的背景色和渐变border//        srfl.setProgressBackgroundColorSchemeColor(Color.parseColor("#ffffff"));//        设置旋转箭头的颜色 方法1//        srfl.setColorSchemeResources(R.color.swipe1,//                R.color.swipe2,//                R.color.swipe3,//                R.color.swipe4);        //设置旋转箭头的颜色  方法2        srfl.setColorSchemeColors(                R.color.swipe2,                R.color.swipe3,                R.color.swipe4);//      设置手势滑动的距离和小圆球下降的距离比例,数值越大,手指就需要向下滑动越长得距离唤出小球//      srfl.setDistanceToTriggerSync(300);
//设置监听的刷新事件,需要开一个线程,不然会导致UI界面小球的旋转卡住        srfl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {            @Override            public void onRefresh() {                new Thread(new Runnable() {                    @Override                    public void run() {                                      for (int i=0;i<5;i++) {                            list.add(0,"NEW "+i);                        }                        try {                            Thread.sleep(3000);                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                                               h.sendMessage(new Message());                        onRefreshing=true;                    }                }).start();            }        });    }}
主要用法如上面代码的注释。如有不对的地方,感谢指出。

0 0
原创粉丝点击