码迷,mamicode.com
首页 > 移动开发 > 详细

Android 刷新下拉控制 SwipeRefreshLayout

时间:2015-09-17 17:12:16      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

        上个月,google它宣布了自己的下拉刷新控制------SwipeRefreshLayout,控制封装在android-support-v4.jar包裹,依靠听力OnRefreshListener实施刷新操作。

件使用方法十分简单,setEnable()方法控制刷新是否可用,setRefreshing()方法控制刷新进度条动画是否可用。因为是要实现下拉刷新,官方文档明白指出该控件所含有的一个直接子View必须是可以滚动的,如ListView、ScrollView等等。

         要使用该控件,须要如今布局文件定义SwipeRefreshLayout控件,布局文件例如以下:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</android.support.v4.widget.SwipeRefreshLayout>

   listView 的item布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:textSize="20sp"
        android:text="TextView" />

</RelativeLayout>

    主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<String> 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<String>();
	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下载地址:点击打开链接


版权声明:本文博主原创文章,博客,未经同意不得转载。

Android 刷新下拉控制 SwipeRefreshLayout

标签:

原文地址:http://www.cnblogs.com/hrhguanli/p/4816697.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!