Google官方SwipeRefreshLayout重新整理控制項使用例子

來源:互聯網
上載者:User

以前我們做下拉重新整理時沒有官方的,都是第三方自訂的控制項,第三方雖然好用,但是時不時會蹦出個問題又很難解決,如今我們可以使用官方的進行資料重新整理,布局只有一個listView,item布局只是一個TextView就不給出了,下面上效果圖

如上圖所示,這種官方的控制項還有一個下拉的圓圈的樣式,就不給出了,下面介紹這個控制項的使用,

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
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>

主布局就是一個重新整理控制項包裹一個listview

下面上代碼


public class MainActivity extends Activity implements SwipeRefreshLayout.OnRefreshListener {
 
   private SwipeRefreshLayout swipeLayout;
   private ListView listView;
   private ListViewAdapter adapter;
   private List<Entity> list;
 
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
      swipeLayout.setOnRefreshListener(this);
      swipeLayout.setColorScheme(R.color.materialBlue,R.color.materialGreen,
            R.color.materialRed, R.color.materialYellow);
      list = new ArrayList<Entity>();
      for (int i=0;i<4;i++){
         list.add(new Entity("重新整理"));
      }
      listView = (ListView) findViewById(R.id.list);
   }
   public void onRefresh() {
      new Handler().postDelayed(new Runnable() {
         public void run() {
            swipeLayout.setRefreshing(false);
            list.add(new Entity("我們"));
            adapter = new ListViewAdapter(MainActivity.this, list);
            listView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
         }
      }, 100000);
   }
}

接下來是adapter代碼


public class ListViewAdapter extends BaseAdapter {
 
    private Context context;
    private List<Entity> list;
 
    public ListViewAdapter(Context context, List<Entity> list) {
        this.context = context;
        this.list = list;
    }
 
    public int getCount() {
        return list.size();
    }
 
    public Object getItem(int arg0) {
        return null;
    }
 
    public long getItemId(int arg0) {
        return 0;
    }
 
    public View getView(int position, View convertView, ViewGroup arg2) {
        HolderView holderView = null;
        if (convertView == null) {
            holderView = new HolderView();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);
            holderView.text = (TextView) convertView.findViewById(R.id.text);
 
        } else {
            convertView.getTag();
        }
        Entity entity=list.get(position);
        holderView.text.setText(entity.getText()+"");
        return convertView;
    }
 
    class HolderView {
        TextView text;
    }
 
}

以上就是全部代碼了,個人感覺挺好用的,最起碼穩定些,

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.