Android控制項(一)下拉重新整理:SwipeRefreshLayout

來源:互聯網
上載者:User

標籤:android   http   color   io   os   ar   java   檔案   sp   

需要注意的是SwipeRefreshLayout下面只可以有一個直接子節點。

布局檔案如下。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:ignore="MergeRootFrame" >    <android.support.v4.widget.SwipeRefreshLayout        android:id="@+id/swipe_container"        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></FrameLayout>

主程式如下:

package com.francis.swiperefreshlayouttest;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.view.Menu;import android.view.MenuItem;import android.support.v4.widget.SwipeRefreshLayout;import android.widget.ArrayAdapter;import android.widget.ListView;// 布局檔案下,<android.support.v4.widget.SwipeRefreshLayout> 只能有一個直接的子類//// 主要方法//        setOnRefreshListener(OnRefreshListener): 為布局添加一個Listener//        setRefreshing(boolean): 顯示或隱藏重新整理進度條//        isRefreshing(): 檢查是否處於重新整理狀態//        setColorSchemeResource(): 設定進度條的顏色主題,最多能設定四種public class MyActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener{    private SwipeRefreshLayout mSwipeLayout;    private ListView mListView;    private ArrayAdapter<String> mArrayAdapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_my);        String[] strings = new String[] {"a","b","c","d"};        mArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strings);        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);        mSwipeLayout.setOnRefreshListener(this);        //載入顏色是迴圈播放的,只要沒有完成重新整理就會一直迴圈,color1>color2>color3>color4        mSwipeLayout.setColorSchemeResources(                android.R.color.holo_blue_bright,                android.R.color.holo_green_light,                android.R.color.holo_orange_light,                android.R.color.holo_red_light);        mListView = (ListView)findViewById(R.id.list);        mListView.setAdapter(mArrayAdapter);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.my, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_grid_view) {            startActivity(new Intent(MyActivity.this,GridViewTest.class));        }        return super.onOptionsItemSelected(item);    }    @Override    public void onRefresh() {        new Handler().postDelayed(new Runnable() {            @Override            public void run() {                // 停止重新整理                mSwipeLayout.setRefreshing(false);            }        }, 3000);    }}


Android控制項(一)下拉重新整理:SwipeRefreshLayout

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.