項目的目錄和架構 java
自定義的ListView的目錄結構以下,其必需要設置爲Library纔可被其它項目使用,使用者只須要將此工程導入到eclipse中便可使用,具體結構如圖所示:android
例子的目錄結構以下,其必需要將自定義的ListView做爲Library引用進來,如圖所示:架構
使用方法app
接下來咱們講一下例子中相關的知識點。eclipse
首先看一下佈局文件中的內容 refreshmore.xml中的內容。ide
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.xiaocui.refreshlistview.PullToRefreshListView xmlns:mylistview="http://schemas.android.com/apk/res/com.xiaocui.pulltorefresh.sample" android:id="@+id/pull_to_refreshmore_listview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/white" android:cacheColorHint="@android:color/transparent" mylistview:mode="both" /> </LinearLayout>
注意:佈局
1. post
xmlns:mylistview="http://schemas.android.com/apk/res/com.xiaocui.pulltorefresh.sample"
mylistview是自定義的一個命名空間,經過mylistview能夠設置對應的屬性。在這裏設置的mode="both"
ui
2. 後面的 com.xiaocui.pulltorefresh.sample是應用的包名。this
接下來,咱們再看一下java代碼中的寫法:
package com.xiaocui.pulltorefresh.sample; import java.util.ArrayList; import com.xiaocui.pulltorefresh.sample.R; import com.xiaocui.refreshlistview.PullToRefreshBase.OnRefreshListener; import com.xiaocui.refreshlistview.PullToRefreshListView; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import android.widget.TextView; public class RefreshListViewActivity extends Activity { private PullToRefreshListView listView; private ListView listShow; private ListViewSampleAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.refreshmore); listView = (PullToRefreshListView) findViewById(R.id.pull_to_refreshmore_listview); listShow = listView.getRefreshableView(); listView.setOnRefreshListener(refreshListener); adapter = new ListViewSampleAdapter(RefreshListViewActivity.this); listShow.setAdapter(adapter); //這裏是模擬數據,在實際項目中,能夠根據業務需求展現不一樣的數據。 adapter.loadData(); } /** * 上拉刷新,下拉獲取更多 */ OnRefreshListener refreshListener = new OnRefreshListener() { // 獲取更多操做 @Override public void onMore() { moreList(); } @Override public void onRefresh() { refreshList(); } }; private void refreshList() { //爲了便於演示,這裏延長了1s的等待時間。 listView.postDelayed(new Runnable() { @Override public void run() { //完成刷新的功能,將頭部分隱藏。 listView.onRefreshComplete(); } }, 1000); } private void moreList() { //爲了便於演示,這裏延長了1sd的等待時間。 listView.postDelayed(new Runnable() { @Override public void run() { //完成加載更多的功能,將頭部分隱藏。 listView.onRefreshComplete(); } }, 1000); } class ListViewSampleAdapter extends android.widget.BaseAdapter { private ArrayList<String> items = new ArrayList<String>(); LayoutInflater inflater; public ListViewSampleAdapter(Context context){ inflater = LayoutInflater.from(context); } public class ViewHolder { public String id; public TextView name; } /** * Loads the data. */ public void loadData() { items = new ArrayList<String>(); items.add("Ajax Amsterdam"); items.add("Barcelona"); items.add("Manchester United"); items.add("Chelsea"); items.add("Real Madrid"); items.add("Bayern Munchen"); items.add("Internazionale"); items.add("Valencia"); items.add("Arsenal"); items.add("AS Roma"); items.add("Tottenham Hotspur"); items.add("PSV"); items.add("Olympique Lyon"); items.add("AC Milan"); items.add("Dortmund"); items.add("Schalke 04"); items.add("Twente"); items.add("Porto"); items.add("Juventus"); // MANDATORY: Notify that the data has changed notifyDataSetChanged(); } @Override public int getCount() { return items.size(); } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { View rowView = convertView; String record = (String) getItem(position); ViewHolder viewHolder = new ViewHolder(); if (convertView == null) { rowView = inflater.inflate(R.layout.list_item, null); viewHolder.name = (TextView) rowView.findViewById(R.id.textView1); rowView.setTag(viewHolder); } final ViewHolder holder = (ViewHolder) rowView.getTag(); holder.name.setText(record); return rowView; } } }
至此本部分就能夠運行了。