Android入门开发之高级界面组件

转载请注明出处:http://blog.csdn.net/qq_32451699/article/details/52422390

一、ListView

实例1.ArrayAdapter做适配器

avtivity_list_test1.xml文件:


    
    
ListTest1Activity.java文件:

package com.example.control;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListTestActivity extends Activity {
	private ListView listview;
	private List list;
	private ArrayAdapter aa;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_test);
		//实例化列表视图
		 listview = (ListView) findViewById(R.id.listView1);
		 //listview使用的MVC开发模型,其中listview是视图,Adapter是模型,在java源文件中进行控制
		 //list为listview提供显示的数据
		 list=new ArrayList();
		 list.add("刘德华");
		 list.add("刘德华");
		 list.add("刘德华");
		 list.add("刘德华");
		 list.add("刘德华");
		 //设置listview的适配器,使用android系统提供的布局文件android.R.layout.simple_list_item_1
		 aa=new ArrayAdapter(this, android.R.layout.simple_list_item_1,list);
	         listview.setAdapter(aa);
	         //为listview设置列表点击监听器用于监听用户点击Listview中每一项的点击事件
	         listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView arg0, View arg1, int arg2,
					long arg3) {
					Toast.makeText(ListTestActivity.this,"别点我",Toast.LENGTH_SHORT).show();
			} 
		 });
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.list_test, menu);
		return true;
	}
}
实例2.SimpleAdapter做适配器

activity_list_test2.xml文件:


    
    
    
还需要提供一个user.xml布局用于listview每一行里面的1布局效果:



		
    		
    		
    
    
    	
    	
    
ListTest2Activity.java文件:

package com.example.control;

import java.util.ArrayList;
import java.util.HashMap;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class ListTest2Activity extends Activity {

	private ListView listview2 = null;

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_test2);
		//实例化listview2
		listview2 = (ListView) findViewById(R.id.listView2);
		//ArrayList里面放的是HashMap,HashMap里面放着三个key-value为布局文件中的三个组件提供数据
		ArrayList> list = new ArrayList>();
		@SuppressWarnings("rawtypes")
		HashMap[]  map = new HashMap[10];
		//向HashMap中添加key-value键值对
		for(int i=0;i<10;i++)
		{
			map[i]=new HashMap();
			map[i].put("image", R.drawable.liudehua);
			map[i].put("name", "刘德华");
			map[i].put("phone", "1234567");
			list.add(map[i]);
		}
		//SimpleAdapter需要提供五个参数
		//第一个参数是上下文对象,就在当前activity中,因此使用this
		//第二个参数是list列表用提供listview中将显示那些数据
		//第三个参数是布局文件,决定listview每一行的布局样式
		//第四个参数是需要显示的value1所对应的key值,是一个字符串数组
		//第五个参数是每一项布局中用那些控件来显示数据,注意此处一顺序与第四个参数中的顺序保持一致
	   SimpleAdapter listadapter = new SimpleAdapter(this, list,
				R.layout.user, new String[] { "image", "name", "phone" },
				new int[] { R.id.img1, R.id.textView3, R.id.textView4 });
	   //为listview设置适配器,其实就是为view设置model
		listview2.setAdapter(listadapter);
		//为listview注册点击项监听器
		listview2.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView arg0, View arg1, int arg2,
					long arg3) {
		        	 Toast.makeText(ListTest2Activity.this,"你点击了列表"+arg2,Toast.LENGTH_LONG).show();
			}
		});
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.list_test2, menu);
		return true;
	}
}
       运行效果:

                             Android入门开发之高级界面组件_第1张图片                                   Android入门开发之高级界面组件_第2张图片

二、ExpandableListView

  activity_main.xml文件:


    
    
    
    
        
      
    
     
group.xml文件为每一父列表项提供布局:



    

item.xml文件为每一子列表项提供布局:



        

MainActivity.java文件:

package com.example.expandablelistviewtest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.LinearLayout;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    private ExpandableListView elvFirst;
    private ExpandableListView elvSecond;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	    //初始化第一个可扩展列表框
		initFirstExpandableListView();
		//初始化第二个可扩展列表框
		initSecondExpandableListView();
	}
	
	private void initFirstExpandableListView(){
		elvFirst = (ExpandableListView)findViewById(R.id.elvFirst);
		//list1为组列表框的每一个列表项提供数据
		List> list1 = new ArrayList>() ;
		Map group1 = new HashMap();
		Map group2 = new HashMap();
		group1.put("group", "列表1");
		group2.put("group", "列表2");
		list1.add(group1);
		list1.add(group2);
		//child1为第一个可扩展列表项下的子列表框提供数据
		List> child1 = new ArrayList>() ;
		Map childData1 = new HashMap();
		Map childData2 = new HashMap();
		childData1.put("child", "子列表1");
		childData2.put("child", "子列表2");
		child1.add(childData1);
		child1.add(childData2);
		//child2为第二个可扩展列表项下的子列表框提供数据
		List> child2 = new ArrayList>() ;
		Map child2Data1 = new HashMap();
		Map child2Data2 = new HashMap();
		child2Data1.put("child", "子列表3");
		child2Data2.put("child", "子列表4");
		child2.add(child2Data1);
		child2.add(child2Data2);
		//把为每一个可扩展项的子项提供数据给放到一个list中
		List>> childs = new ArrayList>>();
		childs.add(child1);
		childs.add(child2);
		//初始化数据适配器
		//SimpleExpandableListAdapter的构造器有九个参数
		//arg1: 上下文对象
		//arg2: 为可扩展项提供数据
		//arg3: 可扩展项的布局文件
		//arg4: 可扩展项的数据来源
		//arg5: 数据显示在布局文件中的什么地方
		//arg6: 为可扩展项的每一子项提供数据
		//arg7: 每一子项的布局文件
		//arg8: 每一子项的数据来源
		//arg9: 数据显示在布局文件中的什么地方
		SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(this, list1, R.layout.group, 
			new String[]{"group"}, new int[]{R.id.groupTo}, childs, R.layout.item, new String[]{"child"}, new int[]{R.id.childTo});
		//为可扩展列表框设置适配器
		elvFirst.setAdapter(sela);
		//为列表框二设置子项单击监听器
		elvFirst.setOnChildClickListener(new OnChildClickListener() {
			@SuppressWarnings("rawtypes")
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
                Toast.makeText(MainActivity.this, "你点击了列表框1:"+((HashMap)parent.getExpandableListAdapter().getChild(groupPosition, childPosition)).get("child"), Toast.LENGTH_LONG).show();
				return true;
			}
		});
	}
	
	private void  initSecondExpandableListView(){
	       elvSecond = (ExpandableListView) findViewById(R.id.elvSecond);
	       //BaseExpandableListAdapter抽象类
	       //需要重写一部分方法,从而达到自定义数据适配器
	       ExpandableListAdapter ela = new BaseExpandableListAdapter() {
	    	   //可扩展列表项需要显示的数据
	    	   String[] group = new String[]{"列表1", "列表2" };
	    	   //每一子项需要显示的数据
	    	   String[][] childList = new String[][]{
	    	   {"子列表1","子列表2"}, {"子列表3","子列表4"}};
			@Override
			public boolean isChildSelectable(int groupPosition, int childPosition) {
				return true;
			}	
			@Override
			public boolean hasStableIds() {
				return true;
			}		
			@Override
			public View getGroupView(int groupPosition, boolean isExpanded,
					View convertView, ViewGroup parent) {
				LinearLayout ll = new LinearLayout(MainActivity.this);
				ll.setOrientation(0);
				TextView textview = getTextView();
				//设置字体颜色
				textview.setTextColor(android.graphics.Color.rgb(244, 33, 33));
				//设置文本框对象需要显示的具体内容
				textview.setText(getGroup(groupPosition).toString());    
				//设置字体大小
		        textview.setTextSize(20);
				ll.addView(textview);
				return ll;
			}		
			@Override
			public long getGroupId(int groupPosition) {
				return groupPosition;
			}	
			@Override
			public int getGroupCount() {
				return group.length;
			}	
			@Override
			public Object getGroup(int groupPosition) {
				return group[groupPosition];
			}	
			@Override
			public int getChildrenCount(int groupPosition) {

				return childList[groupPosition].length;
			}
			//getTextView 将返回一个文本框对象用于显示
			private TextView getTextView(){
				@SuppressWarnings("deprecation")
				AbsListView.LayoutParams lp = new  AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,64);
				TextView textview = new TextView(MainActivity.this);
				textview.setLayoutParams(lp);
				textview.setTextSize(15);
				return textview;
			}
			@Override
			public View getChildView(int groupPosition, int childPosition,
					boolean isLastChild, View convertView, ViewGroup parent) {
				TextView textview = getTextView();
				//设置字体大小
				textview.setTextColor(android.graphics.Color.rgb(150, 150, 33));
				//设置文本框对象需要显示的具体内容
				textview.setText(getChild(groupPosition,childPosition).toString());
				return textview;
			}		
			@Override
			public long getChildId(int groupPosition, int childPosition) {
				return childPosition;
			}	
			@Override
			public Object getChild(int groupPosition, int childPosition) {
				return childList[groupPosition][childPosition];
			}
		};
		//为列表框设置适配器
		elvSecond.setAdapter(ela);
		//为列表框二设置子项单击监听器
		elvSecond.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
                   Toast.makeText(MainActivity.this, "你点击了列表框2:"+parent.getExpandableListAdapter().getChild(groupPosition, childPosition),
                		   Toast.LENGTH_LONG).show();
				return true;
			}
		});
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}

     运行效果:

                      Android入门开发之高级界面组件_第3张图片                   Android入门开发之高级界面组件_第4张图片

三、GridView

activity_main.xml布局文件:


    
    
picturegriditem.xml文件为每个项提供布局:



    
    
MainActivity.java文件:

package com.example.gridviewtest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends Activity {
	private GridView gv;
	private int[] images = new int[]{
	       R.drawable.st0,
	       R.drawable.st1,
	       R.drawable.st2,
	       R.drawable.st3,
	       R.drawable.st4,
	       R.drawable.st5,
	       R.drawable.st6,
	       R.drawable.st7,
	       R.drawable.st8,
	       R.drawable.st9,
	};
	String[] movieName = new String[]{
			"谎言西西里",
			"情况不妙",
			"冰川时代",
			"危城",
			"封神传奇",
			"盗墓笔记",
			"使徒行者",
			"超越星辰",
			"宝贝当家",
			"爱宠大机密",	
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initGridView();
	}
	@SuppressWarnings("unchecked")
	private void  initGridView(){
		     gv = (GridView)findViewById(R.id.gridview);
		     List> list =new ArrayList>();
		     @SuppressWarnings("rawtypes")
			HashMap[] map = new HashMap[10];
		     for(int i = 0 ; i<10 ; i++){
		    	 map[i] = new HashMap();
		    	 map[i].put("image", images[i]);
		    	 map[i].put("name", movieName[i]);
		    	 list.add(map[i]);
		     }
		     SimpleAdapter sa = new SimpleAdapter(this, list, R.layout.picturegriditem, new String[]{"image","name"}, 
		    		 new int[]{R.id.imageView1,R.id.ItemText});
		     //设置适配器
		     gv.setAdapter(sa);
		     //注册监听器
		     gv.setOnItemClickListener(new OnItemClickListener() {
				@SuppressWarnings("rawtypes")
				@Override
				public void onItemClick(AdapterView arg0, View arg1,int arg2, long arg3) {
					   Toast.makeText(MainActivity.this,"你点击了:"+ ((HashMap)arg0.getItemAtPosition(arg2)).get("name").toString(), Toast.LENGTH_LONG).show();
				}
			});
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
           运行效果:

                                 Android入门开发之高级界面组件_第5张图片

你可能感兴趣的:(Android)