博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
006.高级控件的使用(基础--案例)
阅读量:6158 次
发布时间:2019-06-21

本文共 6378 字,大约阅读时间需要 21 分钟。

  hot3.png

(1)ListView

实现如下:

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

activity_main.xml

(2)SpinnerView

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
adapter = new ArrayAdapter
(this,android.R.layout.simple_spinner_item,data); spinner.setAdapter(adapter); }

 

activity_main.xml

 

(3)GridView

MainActivity.java

public class MainActivity extends Activity {	private GridView gView;	private ImageView imageView;	private List
> list; private Map
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
>(); for (int i = 0; i < data.length; i++) { map = new HashMap
(); 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

testimg.xml

(4)Gallery

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

 

转载于:https://my.oschina.net/cjkall/blog/195872

你可能感兴趣的文章
原创:goldengate从11.2升级到12.1.2
查看>>
Quartz
查看>>
正则表达式的语法规则
查看>>
C#一个关于委托和事件通俗易懂的例子
查看>>
类似于SVN的文档内容差异对比工具winmerge
查看>>
Cause: java.sql.SQLException: The user specified as a definer ('root'@'%') does not exist
查看>>
quratz线程
查看>>
execnet: rapid multi-Python deployment
查看>>
windows修改3389端口
查看>>
关于JavaScript词法
查看>>
FreeSwitch中的会议功能(4)
查看>>
MySQL中创建用户分配权限(到指定数据库或者指定数据库表中)
查看>>
AutoReleasePool 和 ARC 以及Garbage Collection
查看>>
重新想象 Windows 8 Store Apps (9) - 控件之 ScrollViewer 基础
查看>>
乐在其中设计模式(C#) - 提供者模式(Provider Pattern)
查看>>
MVP Community Camp 社区大课堂
查看>>
GWT用frame调用JSP
查看>>
大型高性能ASP.NET系统架构设计
查看>>
insert select带来的问题
查看>>
EasyUI 添加tab页(iframe方式)
查看>>