1首先,主页面的布局
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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:background="#FFFFFF" tools:context="cn.jalasmart.com.demo18.MainActivity"> <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/swipe_refresh_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.v4.widget.SwipeRefreshLayout> </FrameLayout>
2以及tem的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="25dp" android:gravity="center" android:text="item" android:textSize="20sp" /> </LinearLayout>3 定义SwipeRefreshLayout刷新旋转时的颜色数组
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name="gplus_color_1">#3e802f</color> <color name="gplus_color_2">#f4b400</color> <color name="gplus_color_3">#427fed</color> <color name="gplus_color_4">#b23424</color> <integer-array name="gplus_colors"> <item>@color/colorAccent</item> <item>@color/gplus_color_2</item> <item>@color/gplus_color_3</item> <item>@color/gplus_color_4</item> </integer-array> </resources>接下来 就是demo的代码
package cn.jalasmart.com.demo18; import android.app.Activity; import android.os.Handler; import android.os.Message; import android.os.SystemClock; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; import java.util.zip.Inflater; public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener { private SwipeRefreshLayout mSwipeRefreshLayout; private boolean isRefresh; private ListView listView; private List<String> lists; private MyAdapter myAdapter; private LayoutInflater inflater; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initView(); } private void initView() { inflater = LayoutInflater.from(this); listView = (ListView) findViewById(R.id.listview); lists = new ArrayList<>(); for (int i = 0; i < 5; i++) { lists.add("item"+i); } myAdapter = new MyAdapter(); listView.setAdapter(myAdapter); mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout); mSwipeRefreshLayout.setOnRefreshListener(this); mSwipeRefreshLayout.setColorSchemeColors(getResources().getIntArray(R.array.gplus_colors)); isRefresh = false; } @Override public void onRefresh() { //当然一般是在刷新时 进行网络请求。下面操作 本应该在网络请求的回调接口中执行(执行时注意所在线程应该在mainThread) if (isRefresh) { //为了看出定义的颜色数组的效果,发送一个延时三秒的空消息 new Handler(){ @Override public void handleMessage(Message msg) {
mSwipeRefreshLayout.setRefreshing(false);//设置成false才会消失 lists.add("刷新item"); myAdapter.notifyDataSetChanged(); Toast.makeText(MainActivity.this,"刷新成功",Toast.LENGTH_SHORT).show(); } }.sendEmptyMessageDelayed(0,3000); } else { mSwipeRefreshLayout.setRefreshing(false); Toast.makeText(this,"刷新失败",Toast.LENGTH_SHORT).show(); isRefresh = true; } } //在项目中,adapter要进行抽取 class MyAdapter extends BaseAdapter{ @Override public int getCount() { return lists.size(); } @Override public String getItem(int position) { return lists.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(R.layout.item,null); } TextView text = (TextView) convertView.findViewById(R.id.text); text.setText(lists.get(position)); return convertView; } } }