2014-2-12AdapterView及其子類2

1.網格視圖(GridView)

GridView用於在界面上按行、列分佈的方式來顯示多個組件。

       GridView和ListView有共同的父類:AbsListView,所以它們有很高的相似性。ListView相當於一種特殊的GridView。

       GridView也需要Adapter來提供顯示的數據。

實例:帶預覽的圖片瀏覽器

界面佈局文件代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    >
    <!-- 定義一個GridView組件 -->
    <GridView
        android:id="@+id/grid1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:horizontalSpacing="1pt"
        android:verticalSpacing="1pt"
        android:numColumns="4"
        android:gravity="center"
        ></GridView>
    <!-- 定義一個ImageView組件 -->
    <ImageView
        android:id="@+id/image1"
        android:layout_width="240dp"
        android:layout_height="240dp"
        android:layout_gravity="center_horizontal"
        />
</LinearLayout>

PS:以上代碼簡單定義了一個GridView、一個ImageView。屬性androidnumColumns=4

意味着網格包含4列,行數由Adapter決定。

Activity代碼清單:

public class GridViewT extends Activity {
 
    GridView grid1;
    ImageView image1;
    int[] imageIds = new int[]
            {
            R.drawable.p00, R.drawable.p01, R.drawable.p02,
            R.drawable.p03, R.drawable.p04, R.drawable.p05,
            R.drawable.p06, R.drawable.p07, R.drawable.p08,
            R.drawable.p09, R.drawable.p10, R.drawable.p11
            };
    @Override
    protected voidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_view_t);
        //創建一個List對象,List對象的元素是Map
        List<Map<String, Object>>listItems =
                new ArrayList<Map<String,Object>>();
        for(int i = 0; i< imageIds.length; i++)
        {
            Map<String, Object> listItem =new HashMap<String, Object>();
            listItem.put("image", imageIds[i]);
            listItems.add(listItem);
        }
        //獲取顯示圖片的ImageView
        image1 = (ImageView) findViewById(R.id.image1);
        image1.setImageResource(imageIds[0]);
        //創建SimpleAdapter
        SimpleAdapter simpleAdapter = new SimpleAdapter(this,
                listItems,
                //使用cell.xml文件作爲界面佈局
                R.layout.cell, new String[] {"image"},
                new int[]{R.id.image});
        grid1 = (GridView) findViewById(R.id.grid1);
        grid1.setAdapter(simpleAdapter);
        //添加列表項被選中的監聽器
        grid1.setOnItemSelectedListener(new OnItemSelectedListener()
        {
 
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                // 顯示當前被選中的圖片
                image1.setImageResource(imageIds[position + 1]);
            }
 
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
               
            }          
        });
        //添加列表項被單擊的監聽器
        grid1.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                //顯示被單擊的圖片
                image1.setImageResource(imageIds[position]);
            }
           
        });
    }
}


Ps:在layout目錄下定義一個cell.xml界面佈局,包含一個簡單ImageView組件。

顯示效果:

2.可展開的列表組件(ExpandableListView)

ExpandableListViewListView的子類,它把應用中的列表項分成幾組,每組裏又可包含多個列表項。列表項由ExpandableListAdapter提供。

       實例:

界面佈局文件:在LinearLayout內定義一個ExpandableListView

Activity代碼清單:

public class ExpandableLiestViewT extends Activity {
 
    @Override
    protected voidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expandable_liest_view_t);
        //創建一個BaseExpandableListAdapter對象
        ExpandableListAdapter adapter = new BaseExpandableListAdapter()
        {
            int[] logos = new int[]
                    {
                    R.drawable.p,
                    R.drawable.z,
                    R.drawable.t
                    };
            private String[] armTypes = new String[]
                    {"神族兵種", "蟲族兵種", "人族兵種"};
            private String[][] arms = new String[][]
                    {
                    {"狂戰士", "龍騎士", "黑暗聖堂", "閃電兵"},
                    {"天狗", "刺蛇", "飛龍", "自爆精靈"},
                    {"機槍兵", "護士mm", "幽靈"},
                    };
            //獲取指定組件位置、指定子列表處的子列表數據
            @Override
            public Object getChild(int groundPosition, int childPosition)
            {
                return arms[groundPosition][childPosition];
            }
            @Override
            public long getChildId(int groupPosion, int childPosion)
            {
                return childPosion;
            }
            @Override
            public int getChildrenCount(int groupPosition){
                return arms[groupPosition].length;
            }
            private TextView getTextView()
            {
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, 64);
                TextView textView = new TextView(ExpandableLiestViewT.this);
                textView.setLayoutParams(lp);
                textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                textView.setPadding(36, 0, 0,0);
                textView.setTextSize(20);
                return textView;
            }
            //該方法決定每個子選項的外觀
            @Override
            public View getChildView(int groupPosition, int childPosition,
                    boolean isLastChild, View convertView, ViewGroupparent) {
                TextView textView =getTextView();
                textView.setText(getChild(groupPosition,childPosition).toString());
                return textView;
            }
            //獲取指定組位置處的組數據
            @Override
            public Object getGroup(int groupPosition) {
                return armTypes[groupPosition];
            }
            @Override
            public int getGroupCount() {
                return armTypes.length;
            }
            @Override
            public long getGroupId(int groupPosition) {
                return groupPosition;
            }
            //該方法決定每個組選項的外觀
            @Override
            public View getGroupView(int groupPosition, boolean isExpanded,
                    View convertView, ViewGroupparent) {
                LinearLayout ll = new LinearLayout(ExpandableLiestViewT.this);
                ll.setOrientation(0);
                ImageView logo = new ImageView(ExpandableLiestViewT.this);
                logo.setImageResource(logos[groupPosition]);
                ll.addView(logo);
                TextView textView =getTextView();
                textView.setText(getGroup(groupPosition).toString());
                ll.addView(textView);
                return ll;
            }
            @Override
            public boolean hasStableIds() {
                return true;
            }
            @Override
            public boolean isChildSelectable(int groupPosition,
                    int childPosition) {
                return true;
            }
        };
        ExpandableListView expandableListView =(ExpandableListView) findViewById
                (R.id.list);
        expandableListView.setAdapter(adapter);
    }
}

PS:上面代碼通過擴展BaseExpandableListAdapter來實現ExpandableListAdapter, 擴展BaseExpandableListAdapter時關鍵有如下四個方法:

l      getGroupCount():該方法返回包含的組列表項的數量

l       getGroupView():該方法返回的View對象將作爲組列表項

l      getChildrenCount():該方法返回特定組所包含的子列表項的數量

l       getChildView():該方法返回的View對象將作爲特定組、特定位置的子列表項

由於在界面佈局文件的ExpandableListView中添加了android:childIndicator="@drawable/ic_launcher"       所以在子列表項左邊會有圖標。

顯示效果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章