006.高级控件的使用(基础--案例)

(1)ListView

实现如下:

006.高级控件的使用(基础--案例)

MainActivity.java

public class MainActivity extends Activity {

	private ListView lv;



	@Override

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		lv = (ListView) findViewById(R.id.listview);

		String[] title = this.getResources().getStringArray(R.array.title);

		String[] content = this.getResources().getStringArray(R.array.content);

		int[] image = { R.drawable.ic_launcher, R.drawable.ic_launcher,

				R.drawable.ic_launcher };

		MyAdapter adapter = new MyAdapter(this, image, title, content);

		lv.setAdapter(adapter);



	}



}

MyAdapter.java

package com.example.listview_adapter;



import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;



public class MyAdapter extends BaseAdapter {

	private Context context;

	private int[] image;

	private String[] title;

	private String[] content;



	public MyAdapter(Context context, int[] image, String[] title,

			String[] content) {

		// TODO Auto-generated constructor stub

		this.context = context;

		this.image = image;

		this.title = title;

		this.content = content;

	}



	@Override

	public int getCount() {

		// TODO Auto-generated method stub

		return title.length;

	}



	@Override

	public Object getItem(int position) {

		// TODO Auto-generated method stub

		return position;

	}



	@Override

	public long getItemId(int position) {

		// TODO Auto-generated method stub

		return position;

	}



	@Override

	public View getView(int position, View arg1, ViewGroup arg2) {

		// TODO Auto-generated method stub

		ViewHolder holder;

		holder = new ViewHolder();

		if (arg1 == null) {

			LayoutInflater inflater = (LayoutInflater) context

					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);



			arg1 = inflater.inflate(R.layout.listadapter_item, null);

			holder.imagView = (ImageView) arg1.findViewById(R.id.img1);

			holder.tv_title = (TextView) arg1

					.findViewById(R.id.listadapter_item_tv_title);

			holder.tv_content = (TextView) arg1

					.findViewById(R.id.listadapter_item_tv_content);

			arg1.setTag(holder);

		} else {

			holder = (ViewHolder) arg1.getTag();

		}



		holder.imagView.setImageResource(image[position]);

		holder.tv_title.setText(title[position]);

		holder.tv_content.setText(content[position]);

		return arg1;

	}



	private static class ViewHolder {

		private ImageView imagView;

		private TextView tv_title;

		private TextView tv_content;



	}



}

listadapter_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="horizontal" >



    <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" >



        <ImageView

            android:id="@+id/img1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:contentDescription="@string/des" />

    </LinearLayout>



    <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="vertical">



        <TextView

            android:id="@+id/listadapter_item_tv_title"

            android:layout_width="wrap_content"

            android:layout_height="0dip"

            android:layout_weight="1" />



        <TextView

            android:id="@+id/listadapter_item_tv_content"

            android:layout_width="wrap_content"

            android:layout_height="0dip"

            android:layout_weight="1" />

    </LinearLayout>



    <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="right"

        android:gravity="right" >



        <CheckBox

            android:layout_width="wrap_content"

            android:layout_height="wrap_content" />

    </LinearLayout>



</LinearLayout>

activity_main.xml

    <ListView

        android:id="@+id/listview"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" >

    </ListView>


(2)SpinnerView

006.高级控件的使用(基础--案例)

MainActivity.java

public class MainActivity extends Activity {

  private Spinner spinner;

	@Override

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		spinner = (Spinner) findViewById(R.id.spinner1);

		String[] data= this.getResources().getStringArray(R.array.data);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,data);

		spinner.setAdapter(adapter);

		

	}


 

activity_main.xml

 <Spinner

        android:id="@+id/spinner1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />

 

(3)GridView

006.高级控件的使用(基础--案例)

MainActivity.java

public class MainActivity extends Activity {

	private GridView gView;

	private ImageView imageView;

	private List<Map<String, Object>> list;

	private Map<String, Object> map;



	@Override

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		gView = (GridView) findViewById(R.id.gv);

		imageView = (ImageView) findViewById(R.id.iv2);

		final int[] data = { R.drawable.ic_launcher, R.drawable.ic_launcher,

				R.drawable.ic_launcher, R.drawable.ic_launcher,

				R.drawable.ic_launcher, R.drawable.ic_launcher,

				R.drawable.ic_launcher, R.drawable.ic_launcher,

				R.drawable.ic_launcher, R.drawable.ic_launcher };

		list = new ArrayList<Map<String, Object>>();



		for (int i = 0; i < data.length; i++) {

			map = new HashMap<String, Object>();

			map.put("img", data[i]);

			list.add(map);

		}



		SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,

				R.layout.lay_textview, new String[] { "img" },

				new int[] { R.id.iv });

		gView.setAdapter(simpleAdapter);

        gView.setOnItemClickListener(new OnItemClickListener() {



			@Override

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,

					long arg3) {

			//	Toast.makeText(MainActivity.this,"这是第"+arg2+"张图",Toast.LENGTH_SHORT).show();

			imageView.setImageResource(data[arg2]);

			}

});

	}



}

activity_main.java

<LinearLayout 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"

    android:orientation="vertical"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity" >



    <GridView

        android:id="@+id/gv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:numColumns="3" >

    </GridView>



    <ImageView

        android:id="@+id/iv2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />



</LinearLayout>


testimg.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/iv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content" />



</LinearLayout>


(4)Gallery

006.高级控件的使用(基础--案例)



MainActvity.java

public class MainActivity extends Activity {

	private Gallery gallery;

	private MyAdapter myAdapter;



	@Override

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);

		gallery = (Gallery) findViewById(R.id.gallery);

		final int[] data = { R.drawable.ic_launcher, R.drawable.ic_launcher,

				R.drawable.ic_launcher, R.drawable.ic_launcher,

				R.drawable.ic_launcher, R.drawable.ic_launcher,

				R.drawable.ic_launcher, R.drawable.ic_launcher,

				R.drawable.ic_launcher, R.drawable.ic_launcher };

		 myAdapter = new MyAdapter(this,data);

		 gallery.setAdapter(myAdapter);

		



	}


MyAdapter.java

public class MyAdapter extends BaseAdapter {

	private Context context;

	private int[] data;

	public MyAdapter(Context context, int[] data) {

	this.context = context;

	this.data = data;

	}



	@Override

	public int getCount() {

		// TODO Auto-generated method stub

		return data.length;

	}



	@Override

	public Object getItem(int arg0) {

		// TODO Auto-generated method stub

		return arg0;

	}



	@Override

	public long getItemId(int arg0) {

		// TODO Auto-generated method stub

		return arg0;

	}



	@Override

	public View getView(int arg0, View arg1, ViewGroup arg2) {

		// TODO Auto-generated method stub

		LinearLayout linearLayout = new LinearLayout(context);

        linearLayout.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

		ImageView imageView = new ImageView(context);

		imageView.setImageResource(data[arg0]);

		imageView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));

		linearLayout.addView(imageView);

        return linearLayout;

	}



	



}

activity_main.java

    <Gallery

        android:id="@+id/gallery"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />



 

 

你可能感兴趣的:(基础)