Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 下拉刷新控件 SwipeRefreshLayout

Android 下拉刷新控件 SwipeRefreshLayout

編輯:關於Android編程

上個月,google發布了自己的下拉刷新控件------SwipeRefreshLayout,該控件封裝在android-support-v4.jar包中,依靠監聽OnRefreshListener實現刷新操作。該控件用法十分簡單,setEnable()方法控制刷新是否可用,setRefreshing()方法控制刷新進度條動畫是否可用,由於是要實現下拉刷新,官方文檔明確指出該控件所含有的一個直接子View必須是能夠滾動的,如ListView、ScrollView等等。

要使用該控件,需要現在布局文件定義SwipeRefreshLayout控件,布局文件如下:


    
    


listView 的item布局文件:




    



主activity代碼如下:

package com.example.android_test_swiperefreshlayout;

import java.util.ArrayList;

import android.os.Bundle;
import android.os.Handler;
import android.R.integer;
import android.app.Activity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnRefreshListener
{
    private SwipeRefreshLayout swipeLayout;
    private ListView listView;
    private ArrayList list;
    private MyAdapter adapter;
    private int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	initData();
	swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe);
	swipeLayout.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);
	
	listView = (ListView) findViewById(R.id.list);
	adapter = new MyAdapter();
	listView.setAdapter(adapter);
	swipeLayout.setOnRefreshListener(this);
    }

    public void initData()
    {
	list = new ArrayList();
	for (int i = 0; i < 24; i++)
	{
	    list.add("北京" + i);
	}
	count = 24;
    }

    class MyAdapter extends BaseAdapter
    {

	@Override
	public int getCount()
	{
	    // TODO Auto-generated method stub
	    return list.size();
	}

	@Override
	public Object getItem(int position)
	{
	    // TODO Auto-generated method stub
	    return list.get(position);
	}

	@Override
	public long getItemId(int position)
	{
	    // TODO Auto-generated method stub
	    return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
	    // TODO Auto-generated method stub
	    if (convertView == null)
	    {
		convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.item,
			null);
	    }
	    TextView textView = (TextView) convertView.findViewById(R.id.textView1);
	    textView.setText(list.get(position));
	    return convertView;
	}

    }

    @Override
    public void onRefresh()
    {
	// TODO Auto-generated method stub
	new Handler().postDelayed(new Runnable()
	{
	    
	    @Override
	    public void run()
	    {
		// TODO Auto-generated method stub
		swipeLayout.setRefreshing(true);
		list.add("新增北京"+count);
		count++;
		adapter.notifyDataSetChanged();
		
	    }
	}, 3000);
    }

}

Demo下載地址:點擊打開鏈接


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved