美文网首页程序员@IT·互联网
Android简单的下拉刷新(谷歌官方)

Android简单的下拉刷新(谷歌官方)

作者: 热爱生活的男孩子 | 来源:发表于2017-05-30 15:38 被阅读0次

使用的是[Android(http://lib.csdn.net/base/android).support.v4.widget.SwipeRefreshLayout控件

布局文件:

 <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        />
<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe_ly"
        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" >
        </ListView>
    </android.support.v4.widget.SwipeRefreshLayout>

代码如下:

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    private static final int REFRESH_COMPLETE = 0X110;
    private SwipeRefreshLayout mSwipeLayout;
    private ListView mListView;
    private ArrayAdapter<String> mAdapter;
    private List<String> mDatas = new ArrayList<String>(Arrays.asList("A", "A", "A", "A", "A"));

    private Handler mHandler = new Handler()
    {
        public void handleMessage(android.os.Message msg)
        {
            switch (msg.what)
            {
                case REFRESH_COMPLETE:
                    mDatas.addAll(Arrays.asList("C", "C", "C"));
                    mAdapter.notifyDataSetChanged();
                    mSwipeLayout.setRefreshing(false);
                    break;

            }
        };
    };
    private Toolbar toolbar;


    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //继承AppCompatActivity必须用这个隐藏标题
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        //toolbar
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("下拉刷新");//标题
        toolbar.setTitleTextColor(Color.WHITE);


        mListView = (ListView) findViewById(R.id.listview);
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_ly);
        //设置监听
        mSwipeLayout.setOnRefreshListener(this);

        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDatas);
        mListView.setAdapter(mAdapter);

    }
    public void onRefresh()
    {

        mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000);

    }
}
效果图是我自己操作不当,上传不上来,想看的朋友可以用发财得小手写一下代码,抱歉!

相关文章

网友评论

    本文标题:Android简单的下拉刷新(谷歌官方)

    本文链接:https://www.haomeiwen.com/subject/hrjrfxtx.html