android中使用开源项目做出上拉、下拉刷新功能

在github网站,下载xlistview项目,导入Eclipse转成库文件,在新建项目里,添加此库。


113223330.jpg 113225428.jpg


在布局中使用自定义xlistview控件

<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="match_parent"
    tools:context=".MainActivity" >
    <me.maxwin.view.XListView
        android:id="@+id/xListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000" >
    </me.maxwin.view.XListView>
</RelativeLayout>


代码如下:

public class MainActivity extends Activity implements IXListViewListener
{
    private XListView mListview;
    ArrayList<String> mlist = new ArrayList<String>();
    private ArrayAdapter<String> mAdapter;
    private Handler mhandler;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mhandler = new Handler();
        mListview = (XListView) findViewById(R.id.xListView);
        mListview.setPullRefreshEnable(true);//设置下拉刷新
        mListview.setXListViewListener(this);//设置监听事件,重写两个方法
        mListview.setPullLoadEnable(true);//设置上拉刷新
        mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mlist);
        for (int i = 0; i < 20; i++)
        {
            mlist.add("data" + i);
        }
        mListview.setAdapter(mAdapter);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public void onRefresh()
    {
        mhandler.postDelayed(new Runnable()
        {
              
            @Override
            public void run()
            {
                mlist.add(0, new Date().toString());
                mAdapter.notifyDataSetChanged();
                mListview.stopRefresh();//完成
                  
            }
        }, 2000);
    }
    @Override
    public void onLoadMore()
    {
        mhandler.postDelayed(new Runnable()
        {
              
            @Override
            public void run()
            {
                mlist.add(new Date().toString());
                mAdapter.notifyDataSetChanged();
                mListview.stopLoadMore();
            }
        }, 2000);
    }
}


你可能感兴趣的:(android,下拉刷新,上拉刷新)