簡單實現標題欄懸浮狀態,使用PullToRefreshListView上拉載入下拉重新整理
title_xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="評論"
android:textSize="30sp"
android:id="@+id/pinglun1"
android:background="@android:color/background_dark"
android:textColor="@android:color/holo_blue_light"/>
</LinearLayout>
head_xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/af"
android:id="@+id/iv"/>
</LinearLayout>
main_xml:
<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"
>
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pull_list_view" >
</com.handmark.pulltorefresh.library.PullToRefreshListView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="評論"
android:textSize="30sp"
android:id="@+id/pinglun1"
android:visibility="gone"
android:background="@android:color/background_dark"
android:textColor="@android:color/holo_blue_light"/>
</RelativeLayout>
介面卡
public class MyAdapter extends BaseAdapter{
private Context context;
private ArrayList<String> stringList;
public MyAdapter(Context context,ArrayList<String> stringList) {
super();
this.context = context;
this.stringList = stringList;
}
@Override
public int getCount() {
return stringList.size();
}
@Override
public Object getItem(int position) {
return stringList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(context);
tv.setText(stringList.get(position));
tv.setTextSize(100);
return tv;
}
}
MainActivity
public class MainActivity extends Activity {
private PullToRefreshListView pull;
private View headView;
private View headTab;
private TextView pinglun1;
private View headTabTitle;
private ArrayList<Integer> mList;
private ArrayList<String> stringList;
private MyAdapter adapter;
private int mItemCount = 9;
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0:
stringList.clear();
initAdapterData();
adapter.notifyDataSetChanged();
pull.onRefreshComplete();
break;
case 1:
mItemCount=mItemCount+10;
initAdapterData();
adapter.notifyDataSetChanged();
pull.onRefreshComplete();
break;
default:
break;
}
};
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pull = (PullToRefreshListView) findViewById(R.id.pull_list_view);
stringList = new ArrayList<String>();
pinglun1 = (TextView) findViewById(R.id.pinglun1);
// listview的頭部
initDetailHead();
// listview的title
initTab();
// 新增listview
initListView();
// 初始化介面卡的引數
initAdapterData();
adapter = new MyAdapter(this, stringList);
pull.setAdapter(adapter);
}
// 初始化介面卡的引數
private void initAdapterData() {
for (int i = 0; i < mItemCount; i++) {
stringList.add(i + "");
}
}
//非同步載入
// private class GetDataTask extends AsyncTask<Void, Void, String> {
//
// @Override
// protected String doInBackground(Void... params) {
// try {
// Thread.sleep(2000);
// } catch (InterruptedException e) {
// }
// return ""+ (mItemCount++);
// }
//
// @Override
// protected void onPostExecute(String result) {
// stringList.add(result);
// adapter.notifyDataSetChanged();
// // Call onRefreshComplete when the list has been refreshed.
// pull.onRefreshComplete();
//
// }
// }
// 新增listview
private void initListView() {
ListView listView = pull.getRefreshableView();
listView.addHeaderView(headView);
listView.addHeaderView(headTabTitle);
// 兩端重新整理的方法
pull.setMode(Mode.BOTH);
//實現下拉重新整理,上拉載入
initRefresh();
// 滾動監聽
pull.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
pinglun1.setVisibility(firstVisibleItem >= 2 ? View.VISIBLE
: View.GONE);
}
});
// //上拉載入
// pull.setOnRefreshListener(new OnRefreshListener<ListView>() {
//
// @Override
// public void onRefresh(PullToRefreshBase<ListView> refreshView) {
// // TODO Auto-generated method stub
// stringList.clear();
// initAdapterData();
// adapter.notifyDataSetChanged();
// }
// });
//
// //滑動到底部最後一個item監聽
// pull.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
//
// @Override
// public void onLastItemVisible() {
// initAdapterData();
// adapter.notifyDataSetChanged();
// }
// });
}
private void initRefresh() {
new Thread(){
public void run() {
pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
// 下拉重新整理
@Override
public void onPullDownToRefresh(PullToRefreshBase refreshView) {
// TODO Auto-generated method stub
//非同步載入
//new GetDataTask().execute();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}
// 上拉載入
@Override
public void onPullUpToRefresh(PullToRefreshBase refreshView) {
// TODO Auto-generated method stub
//非同步載入
//new GetDataTask().execute();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.sendEmptyMessage(1);
}
});
};
}.start();
}
// listview的title
private void initTab() {
headTabTitle = View.inflate(this, R.layout.title, null);
}
// listview的頭部
public void initDetailHead() {
headView = View.inflate(this, R.layout.head, null);
ImageView iv = (ImageView) headView.findViewById(R.id.iv);
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "點選了圖片", 0).show();
}
});
}
}