第3編ListView編に深く入り込む

5093 ワード

テーマに直行~!
図のように構成されています.
第三篇 深入ListView篇
main.xmlコード:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<TextView  

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:text="@string/hello"

    />

</LinearLayout>


showcontent.xmlコード:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

	android:orientation="vertical" android:layout_width="fill_parent"

	android:layout_height="fill_parent">

	<LinearLayout android:layout_width="fill_parent"

		android:layout_height="wrap_content" android:id="@+id/linearLayout1"

		android:gravity="center">

		<ImageView android:layout_width="100dip"

			android:layout_height="100dip" android:src="@drawable/icon"

			android:id="@+id/iv"></ImageView>

		<TextView android:text="     " android:id="@+id/tv"

			android:layout_width="wrap_content" android:layout_height="wrap_content"

			></TextView>

	</LinearLayout>



</LinearLayout>


Control_ListView_LoadingActivity.javaコード:
public class Control_ListView_LoadingActivity extends ListActivity implements

		OnScrollListener {

	private LayoutParams layoutParams1 = new LinearLayout.LayoutParams(

			LinearLayout.LayoutParams.WRAP_CONTENT,

			LinearLayout.LayoutParams.WRAP_CONTENT);

	private LayoutParams layoutParams2 = new LinearLayout.LayoutParams(

			LinearLayout.LayoutParams.FILL_PARENT,

			LinearLayout.LayoutParams.FILL_PARENT);

	private ProgressBar pb;

	private TextView tv;

	private int lastItem = 0;

	mylistener adapter;



	/** Called when the activity is first created. */

	@Override

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		// setContentView(R.layout.main);

		Layout_initialize();

	}



	public void Layout_initialize() {

		LinearLayout ll1 = new LinearLayout(

				Control_ListView_LoadingActivity.this);

		pb = new ProgressBar(Control_ListView_LoadingActivity.this);

		tv = new TextView(Control_ListView_LoadingActivity.this);

		pb.setPadding(0, 0, 15, 0);

		ll1.addView(pb, layoutParams1);

		tv.setText("    ...");

		tv.setGravity(Gravity.CENTER_HORIZONTAL);

		ll1.addView(tv, layoutParams2);

		ll1.setGravity(Gravity.CENTER);

		LinearLayout loadingLayout = new LinearLayout(this);

		loadingLayout.addView(ll1, layoutParams1);

		loadingLayout.setGravity(Gravity.CENTER);

		ListView listView = getListView();

		listView.addFooterView(loadingLayout);

		registerForContextMenu(listView);

		adapter = new mylistener(Control_ListView_LoadingActivity.this);

		setListAdapter(adapter);

		listView.setOnScrollListener(Control_ListView_LoadingActivity.this);



	}



	Handler hd = new Handler() {



		@Override

		public void handleMessage(Message msg) {

			// TODO Auto-generated method stub

			// super.handleMessage(msg);

			if (msg.what == 123) {



				adapter.notifyDataSetChanged();

				// setListAdapter(adapter);

			}

		}



	};



	public void onScroll(AbsListView view, int firstVisibleItem,

			int visibleItemCount, int totalItemCount) {

		// TODO Auto-generated method stub

		lastItem = firstVisibleItem + visibleItemCount - 1;



	}



	public void onScrollStateChanged(AbsListView view, int scrollState) {

		// TODO Auto-generated method stub

		

	      

		//         ,      onscroll     ,    

        if(scrollState==OnScrollListener.SCROLL_STATE_IDLE)

        {

        	Toast.makeText(Control_ListView_LoadingActivity.this, "    ", Toast.LENGTH_LONG).show();

        }

	

		if (lastItem == adapter.count) {

			adapter.count += 5;

			Message msg = hd.obtainMessage(123);

			hd.sendMessage(msg);



		}

	}



	class mylistener extends BaseAdapter {

		Context ct;

		int count = 5;



		public mylistener(Context ct) {

			this.ct = ct;

		}



		public int getCount() {

			// TODO Auto-generated method stub

			return count;

		}



		public Object getItem(int position) {

			// TODO Auto-generated method stub

			return position;

		}



		public long getItemId(int position) {

			// TODO Auto-generated method stub

			return position;

		}



		public View getView(int position, View convertView, ViewGroup parent) {

			// TODO Auto-generated method stub

			// return null;

			LayoutInflater lif = getLayoutInflater();

			convertView = lif.inflate(R.layout.showcontent, null);

			return convertView;

		}



	}

}