Android学习笔记⑧——UI组件的学习AdapterView相关2

前面都是用ListView控件来配合Adapter做的一些实例,这次我们来见识一下GridView与Adapter之间的爱恨情仇。。。。

GridView是用于在界面上按行、列分布的方式来显示多个的组件,与ListView相似,但是比ListView高级,因为他可以一下子有多个列,而ListView只有一列。

GridView设置一个简单的图片浏览器

大概功能就是 点击一下图片,然后下方显示放大的图。。。

主布局代码:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6 
 7     <GridView
 8         android:id="@+id/gridview"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content"
11         android:gravity="center"
12         android:horizontalSpacing="1pt"
13         android:numColumns="4"
14         android:verticalSpacing="1pt">
15     </GridView>
16 
17     <ImageView
18         android:id="@+id/imageshow"
19         android:layout_width="250dp"
20         android:layout_height="250dp"
21         android:layout_gravity="center_horizontal"
22         />
23 </LinearLayout>

 用与ImageView布局的 代码:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="wrap_content"
 4     android:layout_height="wrap_content">
 5 
 6     <ImageView
 7         android:id="@+id/image"
 8         android:layout_width="70dp"
 9         android:layout_height="70dp"
10         android:layout_margin="5dp"
11         android:scaleType="fitXY"
12         />
13 </LinearLayout>

 

java代码:

 1 package com.doliao.helloworld;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.widget.AdapterView;
 7 import android.widget.GridView;
 8 import android.widget.ImageView;
 9 import android.widget.SimpleAdapter;
10 
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 
16 /**
17  * Created by Administrator on 2016/10/12 0012.
18  */
19 
20 public class GridViewActivity extends Activity {
21 
22     GridView gridView;
23     ImageView imageView;
24     int[] imageids = new int[]{R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6
25             , R.drawable.pic7, R.drawable.pic8, R.drawable.pic9, R.drawable.pic10, R.drawable.pic11, R.drawable.pic12};
26 
27     @Override
28     protected void onCreate(Bundle savedInstanceState) {
29         super.onCreate(savedInstanceState);
30         setContentView(R.layout.activity_gridview);
31 
32         gridView = (GridView) findViewById(R.id.gridview);
33         imageView = (ImageView) findViewById(R.id.imageshow);
34 
35         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
36         for (int imageid : imageids) {
37             Map<String, Object> map = new HashMap<String, Object>();
38             map.put("imageid", imageid);
39             list.add(map);
40         }
41         SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.all_images,
42                 new String[]{"imageid"}, new int[]{R.id.image});
43         gridView.setAdapter(simpleAdapter);
44 
45         gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
46             @Override
47             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
48                 imageView.setImageResource(imageids[position]);
49             }
50         });
51     }
52 }

 

运行的显示截图如下:

 

 ExpandableListView (可展开的列表组件)

 ExpandableListView是List的子类,在基本的List上面进行了扩展,他把列表分为几组,每组里面又可以包含多个列表现。简单的来说就相当于QQ列表一样,有分组,分组里面又有好多的好友列表。这么高级的组件本身也是傲娇的,他有自己御用的Adapter——ExpandableListAdapter。EXpandableAdapter与Adapter相似,一般实现的方法有三种:

1、扩展BaseExpandableLIstAdapter实现的ExpandableListAdapter;

2、使用SimpleExpandableListAdapter将两个list集合包装成ExpandableListAdapter

3、使用SimpleCursorTreeAdapter将Cursor中的数据包装成SimpleCursorTreeAdapter。

布局代码:activity_expandablelistview.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 
 6     <ExpandableListView
 7         android:id="@+id/expand"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content">
10 
11     </ExpandableListView>
12 </LinearLayout>

 Java代码: ExpandableListViewActivity.java

  1 package com.doliao.helloworld;
  2 
  3 import android.app.Activity;
  4 import android.os.Bundle;
  5 import android.view.Gravity;
  6 import android.view.View;
  7 import android.view.ViewGroup;
  8 import android.widget.AbsListView;
  9 import android.widget.BaseExpandableListAdapter;
 10 import android.widget.ExpandableListAdapter;
 11 import android.widget.ExpandableListView;
 12 import android.widget.LinearLayout;
 13 import android.widget.TextView;
 14 
 15 /**
 16  * Created by Administrator on 2016/10/13 0013.
 17  */
 18 
 19 
 20 public class ExpandableListViewActivity extends Activity {
 21 
 22     //定义列表组
 23     String[] listType = new String[]{"名著列表", "小说列表", "动漫列表"};
 24     //每个列表组下面的数据
 25     String[][] lists = new String[][]{
 26             {"西游记", "水浒传", "红楼梦"},
 27             {"择天记", "星辰变", "间客"},
 28             {"海贼王", "火影忍者", "死神"}
 29     };
 30 
 31 
 32     @Override
 33     protected void onCreate(Bundle savedInstanceState) {
 34         super.onCreate(savedInstanceState);
 35         setContentView(R.layout.activity_expandablelistview);
 36 
 37 
 38         ExpandableListAdapter expandableListAdapter = new BaseExpandableListAdapter() {
 39 
 40             @Override
 41             public int getGroupCount() {
 42                 return lists.length;
 43             }
 44 
 45             @Override
 46             public int getChildrenCount(int groupPosition) {
 47                 return lists[groupPosition].length;
 48             }
 49 
 50             @Override
 51             public Object getGroup(int groupPosition) {
 52                 return listType[groupPosition];
 53             }
 54 
 55             @Override
 56             public Object getChild(int groupPosition, int childPosition) {
 57                 return lists[groupPosition][childPosition];
 58             }
 59 
 60             @Override
 61             public long getGroupId(int groupPosition) {
 62                 return groupPosition;
 63             }
 64 
 65             @Override
 66             public long getChildId(int groupPosition, int childPosition) {
 67                 return childPosition;
 68             }
 69 
 70             @Override
 71             public boolean hasStableIds() {
 72                 return true;
 73             }
 74 
 75             @Override
 76             public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
 77 
 78                 //View view = View.inflate(ExpandableListViewActivity.this, R.layout.autotext, null);
 79                 //TextView textView = (TextView) findViewById(R.layout.autotext);
 80 
 81                 /*View view = View.inflate(ExpandableListViewActivity.this, R.layout.linear_text, null);
 82                 TextView textView = (TextView) findViewById(R.id.text1);
 83                 textView.setText(getGroup(groupPosition).toString());
 84                 view.setLayoutParams();*/
 85 
 86                 LinearLayout ll = new LinearLayout(ExpandableListViewActivity.this);
 87                 TextView textView = getTextView();
 88                 textView.setText(getGroup(groupPosition).toString());
 89                 ll.addView(textView);
 90                 return ll;
 91 
 92             }
 93 
 94             @Override
 95             public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
 96                 //TextView textView = (TextView) findViewById(R.id.autotext);
 97                 //textView.setText(getChild(groupPosition, childPosition).toString());
 98                 TextView textView = getTextView();
 99                 textView.setText(getChild(groupPosition, childPosition).toString());
100                 return textView;
101             }
102 
103             @Override
104             public boolean isChildSelectable(int groupPosition, int childPosition) {
105                 return true;
106             }
107         };
108 
109 
110         ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expand);
111         expandableListView.setAdapter(expandableListAdapter);
112     }
113 
114     private TextView getTextView() {
115         AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64);
116         TextView textView = new TextView(ExpandableListViewActivity.this);
117         textView.setLayoutParams(lp);
118         textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
119         textView.setPadding(36, 0, 0, 0);
120         textView.setTextSize(20);
121         return textView;
122     }
123 }

 

运行截图:(没有改变颜色。。。)

做这个实例的时候,书上的例子是上面的代码,我一开始是想不用书上的例子,我的想法是他既然是 布局、组件都是新建,那么我应该可以事先设置好他的xml文件,等到时候用的时候,我在调用过来,但是我折腾了好久都没成功,但是我学到了怎么获取 事先布局好的Layout,但是我就不知道如何想Layout中添加组件如下:

View view = View.inflate(ExpandableListViewActivity.this, R.layout.linear_text, null);
TextView textView = (TextView) findViewById(R.id.text1);

布局是事先设置好的,textview独立xml文件或者放在Layout xml的文件中都是行不通的,可能是我想的有问题吧,有可能根本就没这个方法。

最后我放弃了,还是按照书上的例子做了下去,好气啊。先做个标记,容我去打怪升级,等我等级高了再来报仇!!!!!!!!!!!

 

最近工作上面的事情比较忙,所以没有多少时间学习,但是。。。。。。。。还是要坚持主动的学习下去!!!!

如果有错,请大牛们指正。 再次先谢谢了 !

posted @ 2016-10-17 20:13  大园子  阅读(337)  评论(0编辑  收藏  举报