Usage of ListView nested GridView and precautions

Source: Internet
Author: User

MainActivity is as follows: Copy codeThe Code is as follows: package cn. testlistviewandgridview;
Import java. util. ArrayList;
Import java. util. HashMap;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. ListView;
/**
* Demo description:
* Usage of ListView nested GridView
* That is, each Item in the ListView contains a GridView
*
* Note:
* Because ListView and GridView are slip controls.
* Therefore, you need to customize the GridView and override its onMeasure () method.
* In this method, set the height of the GridView to the size of wrap_content. Otherwise
* Only a small portion of the content can be displayed.
*
* References:
* 1 http://bbs.csdn.net/topics/380245627
* 2 http://blog.csdn.net/lsong89/article/details/8598856
* Thank you very much
*/
Public class MainActivity extends Activity {
Private ListView mListView;
Private ListViewAdapter mListViewAdapter;
Private ArrayList <HashMap <String, Object> mArrayList;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Init ();
}
Private void init (){
MListView = (ListView) findViewById (R. id. listView );
InitData ();
MListViewAdapter = new ListViewAdapter (mArrayList, MainActivity. this );
MListView. setAdapter (mListViewAdapter );
}
Private void initData (){
MArrayList = new ArrayList <HashMap <String, Object> ();
HashMap <String, Object> hashMap = null;
ArrayList <HashMap <String, Object> arrayListForEveryGridView;

For (int I = 0; I <10; I ++ ){
ArrayListForEveryGridView = new ArrayList <HashMap <String, Object> ();
For (int j = 0; j <5; j ++ ){
HashMap = new HashMap <String, Object> ();
HashMap. put ("content", "I =" + I + ", j =" + j );
ArrayListForEveryGridView. add (hashMap );
}
MArrayList. add (arrayListForEveryGridView );
}

}
}

The ListViewAdapter is as follows:Copy codeThe Code is as follows: package cn. testlistviewandgridview;
Import java. util. ArrayList;
Import java. util. HashMap;
Import android. content. Context;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. GridView;
Import android. widget. ImageView;
Public class ListViewAdapter extends BaseAdapter {
Private ArrayList <HashMap <String, Object> mList;
Private Context mContext;

Public ListViewAdapter (ArrayList <HashMap <String, Object> mList, Context mContext ){
Super ();
This. mList = mList;
This. mContext = mContext;
}
@ Override
Public int getCount (){
If (mList = null ){
Return 0;
} Else {
Return this. mList. size ();
}
}
@ Override
Public Object getItem (int position ){
If (mList = null ){
Return null;
} Else {
Return this. mList. get (position );
}
}
@ Override
Public long getItemId (int position ){
Return position;
}
@ Override
Public View getView (int position, View convertView, ViewGroup parent ){
ViewHolder holder = null;
If (convertView = null ){
Holder = new ViewHolder ();
ConvertView = LayoutInflater. from
(This. mContext). inflate (R. layout. listview_item, null, false );
Holder. imageView = (ImageView) convertView. findViewById (R. id. listview_item_imageview );
Holder. gridView = (GridView) convertView. findViewById (R. id. listview_item_gridview );
ConvertView. setTag (holder );
} Else {
Holder = (ViewHolder) convertView. getTag ();
}

If (this. mList! = Null ){
If (holder. imageView! = Null ){
Holder. imageView. setImageDrawable
(MContext. getResources (). getDrawable (R. drawable. e ));
}
If (holder. gridView! = Null ){
ArrayList <HashMap <String, Object> arrayListForEveryGridView = this. mList. get (position );
GridViewAdapter gridViewAdapter = new GridViewAdapter (mContext, arrayListForEveryGridView );
Holder. gridView. setAdapter (gridViewAdapter );
}
}
Return convertView;
}

Private class ViewHolder {
ImageView imageView;
GridView;
}
}

GridViewAdapter is as follows:Copy codeThe Code is as follows: package cn. testlistviewandgridview;
Import java. util. ArrayList;
Import java. util. HashMap;
Import android. content. Context;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. Button;
Import android. widget. Toast;
Public class GridViewAdapter extends BaseAdapter {
Private Context mContext;
Private ArrayList <HashMap <String, Object> mList;

Public GridViewAdapter (Context mContext, ArrayList <HashMap <String, Object> mList ){
Super ();
This. mContext = mContext;
This. mList = mList;
}
@ Override
Public int getCount (){
If (mList = null ){
Return 0;
} Else {
Return this. mList. size ();
}
}
@ Override
Public Object getItem (int position ){
If (mList = null ){
Return null;
} Else {
Return this. mList. get (position );
}
}
@ Override
Public long getItemId (int position ){
Return position;
}
@ Override
Public View getView (final int position, View convertView, ViewGroup parent ){
ViewHolder holder = null;
If (convertView = null ){
Holder = new ViewHolder ();
ConvertView = LayoutInflater. from
(This. mContext). inflate (R. layout. gridview_item, null, false );
Holder. button = (Button) convertView. findViewById (R. id. gridview_item_button );
ConvertView. setTag (holder );

} Else {
Holder = (ViewHolder) convertView. getTag ();
}

If (this. mList! = Null ){
HashMap <String, Object> hashMap = this. mList. get (position );
If (holder. button! = Null ){
Holder. button. setText (hashMap. get ("content"). toString ());
Holder. button. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Toast. makeText (mContext, "th" + (position + 1) + ", Toast. LENGTH_SHORT). show ();
}
});
}
}
Return convertView;
}

Private class ViewHolder {
Button button;
}
}

NoScrollGridView is as follows:Copy codeThe Code is as follows: package cn. testlistviewandgridview;
Import android. content. Context;
Import android. util. AttributeSet;
Import android. widget. GridView;
Public class NoScrollGridView extends GridView {
Public NoScrollGridView (Context context ){
Super (context );

}
Public NoScrollGridView (Context context, AttributeSet attrs ){
Super (context, attrs );
}

@ Override
Protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec ){
Int expandSpec = MeasureSpec. makeMeasureSpec (Integer. MAX_VALUE> 2, MeasureSpec. AT_MOST );
Super. onMeasure (widthMeasureSpec, expandSpec );
}
}

Main. xml is as follows:Copy codeThe Code is as follows: <RelativeLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
>
<ListView
Android: id = "@ + id/listView"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: focusable = "false"
/>
</RelativeLayout>

Listview_item.xml is as follows:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical"
>
<ImageView
Android: id = "@ + id/listview_item_imageview"
Android: layout_width = "fill_parent"
Android: layout_height = "20dip"
Android: scaleType = "fitXY"
Android: src = "@ drawable/e"
/>
<Cn. testlistviewandgridview. NoScrollGridView
Android: id = "@ + id/listview_item_gridview"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: stretchMode = "columnWidth"
Android: verticalSpacing = "5dip"
Android: horizontalSpacing = "5dip"
Android: numColumns = "2"/>
</LinearLayout>

Gridview_item.xml is as follows:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "horizontal"
Android: padding = "10dip"
>
<Button
Android: id = "@ + id/gridview_item_button"
Android: layout_width = "140dip"
Android: layout_height = "40dip"
Android: background = "@ drawable/e"
Android: textColor = "@ android: color/background_light"
Android: clickable = "true"
/>
</LinearLayout>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.