android Spinner的简单demo(用到BaseAdapter)

package com.example.spinnerdemo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * Spinner 是 ViewGroup的间接子类 可以做容器
 * 
 */
public class MainActivity extends Activity {

	private int[] mImages = { R.drawable.bomb5, R.drawable.bomb6,
			R.drawable.bomb7, R.drawable.bomb8, R.drawable.bomb9 };

	private String[] mText = { "疯狂的 Android 讲义", "疯狂的 Java 讲义",
			"疯狂的 Python 讲义", "疯狂的 XML 讲义", "疯狂的 HTML 讲义", };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

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

		BaseAdapter baseAdapter = new BaseAdapter() {

			@Override
			public int getCount() {
				return 5;
			}

			@Override
			public Object getItem(int arg0) {
				return null;
			}

			@Override
			public long getItemId(int arg0) {
				return 0;
			}

			@Override
			public View getView(int pos, View arg1, ViewGroup arg2) {

				LinearLayout liner = new LinearLayout(MainActivity.this);
				liner.setOrientation(LinearLayout.HORIZONTAL);

				ImageView image = new ImageView(MainActivity.this);
				image.setImageResource(mImages[pos]);

				TextView textView = new TextView(MainActivity.this);
				textView.setText(mText[pos]);
				textView.setTextColor(Color.BLACK);
				
				liner.addView(image);
				liner.addView(textView);

				return liner;
			}

		};

		spinner.setAdapter(baseAdapter);
	}
}


查了下getView:

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

Get a View that displays the data at the specified position in the data set. You can either create a View manually or inflate it from an XML layout file. When the View is inflated, the parent View (GridView, ListView...) will apply default layout parameters unless you use LayoutInflater.inflate(int, android.view.ViewGroup, boolean) to specify a root view and to prevent attachment to the root. 


参数:
position - The position of the item within the adapter's data set of the item whose view we want.
convertView - The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view.
parent - The parent that this view will eventually be attached to 
返回:
A View corresponding to the data at the specified position.


实现效果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值