ListView item项布局listview_item.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:id="@+id/imageView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/pretty_girl"
- />
- <TextView
- android:id="@+id/textview"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
清单文件也贴出来吧,其实就是自动生成的,什么都不用改:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.focustech.android"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="14" />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:label="@string/app_name"
- android:name="com.focustech.android.MainActivity" >
- <intent-filter >
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
此Demo的Activity:
- package com.focustech.android;
- import android.app.Activity;
- import android.database.DataSetObserver;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ListAdapter;
- import android.widget.TextView;
- import com.focustech.android.CustomListView.OnRefreshListener;
- import com.focustech.android.R;
- public class MainActivity extends Activity {
- private ListAdapter adapter;
- int size = 10;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- final CustomListView list = (CustomListView) findViewById(R.id.list);
- adapter = new ListAdapter() {
- public void unregisterDataSetObserver(DataSetObserver arg0) {
- }
- public void registerDataSetObserver(DataSetObserver arg0) {
- }
- public boolean isEmpty() {
- return false;
- }
- public boolean hasStableIds() {
- return false;
- }
- public int getViewTypeCount() {
- return 1;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- convertView = LayoutInflater.from(getApplicationContext())
- .inflate(R.layout.listview_item, null);
- TextView mTextView = (TextView) convertView
- .findViewById(R.id.textview);
- mTextView.setText("pretty girl" + position);
- mTextView.setTextColor(Color.RED);
- return convertView;
- }
- public int getItemViewType(int arg0) {
- return arg0;
- }
- public long getItemId(int arg0) {
- return arg0;
- }
- public Object getItem(int arg0) {
- return null;
- }
- public int getCount() {
- return size;
- }
- @Override
- public boolean isEnabled(int position) {
- return false;
- }
- @Override
- public boolean areAllItemsEnabled() {
- return false;
- }
- };
- list.setAdapter(adapter);
- // 在list上注册自定义的监听器
- list.setonRefreshListener(new OnRefreshListener() {
- public void onRefresh() {
- new Thread(new Runnable() {
- public void run() {
- try {
- Thread.sleep(1000);
- } catch (Exception e) {
- }
- runOnUiThread(new Runnable() {
- public void run() {
- list.onRefreshComplete();
- }
- });
- size++;
- }
- }).start();
- }
- });
- }
- }
