android listveiw 嵌套Girdview



需要自定义一个GridView


package com.news;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;

public class MainlistView extends Activity{

	private ListView listview;
	private List<String> userlist = new ArrayList<String>();
	private List<String> userlist2 = new ArrayList<String>();
	MAdapter madapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_list);
		initView();
		initData();
		
	}
	private  void initView(){
		listview = (ListView)findViewById(R.id.listview);
	}
	private void initData(){
		for(int i = 0;i<10;i++){
			String s = "刘华兵"+i;
			userlist.add(s);
		}
		for(int i = 0;i<10;i++){
			String s = "   海尔兄弟"+i;
			userlist2.add(s);
		}
		madapter= new MAdapter(this);
		listview.setAdapter(madapter);
	}
	class MAdapter extends BaseAdapter{
		private Context context;
		public MAdapter(Context context){
			this.context = context;
		}
		public int getCount() {
			return userlist.size();
		}
		public Object getItem(int position) {
			return userlist.get(position);
		}
		public long getItemId(int position) {
			return position;
		}
		public View getView(int position, View convertView, ViewGroup parent) {
			ItemView itemView = null;
			 if (convertView == null) {  
				convertView = LayoutInflater.from(context).inflate(R.layout.list_item, null);
				itemView = new ItemView();
				itemView.itemText = (TextView)convertView.findViewById(R.id.itemText);
				itemView.grideview = (MyGridView)convertView.findViewById(R.id.grideview);
				
				
				convertView.setTag(itemView);
			}else{
	            itemView = (ItemView)convertView.getTag();
	        }
			 
			 String s = userlist.get(position);
			 itemView.itemText.setText(s);
			 NAdapter n = new NAdapter(MainlistView.this);
			 itemView.grideview.setAdapter(n);
				
			return convertView;
		}
		
		class ItemView{
			public TextView itemText;
			public MyGridView grideview;
		}
	}
	
	class NAdapter extends BaseAdapter{
		private Context context;
		
		public NAdapter(Context context){
			this.context = context;
		}
		public int getCount() {
			return userlist2.size();
		}
		public Object getItem(int position) {
			return userlist2.get(position);
		}
		public long getItemId(int position) {
			return position;
		}
		
		public View getView(int position, View convertView, ViewGroup parent) {
			ItemView itemView = null;
			 if (convertView == null) {  
				convertView = LayoutInflater.from(context).inflate(R.layout.list_view_itemimg, null);
				itemView = new ItemView();
				itemView.items = (TextView)convertView.findViewById(R.id.items);
				convertView.setTag(itemView);
			}else{
	            itemView = (ItemView)convertView.getTag();
	        }
			 
			 String s = userlist2.get(position);
			 itemView.items.setText(s);
			return convertView;
		}
		
		class ItemView{
			public TextView items;
			
		}
	}
	
}


MyGridView


package com.news;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;

public class MyGridView extends GridView {     
    public MyGridView(Context context, AttributeSet attrs) {     
        super(context, attrs);     
    }     
    
    public MyGridView(Context context) {     
        super(context);     
    }     
    
    public MyGridView(Context context, AttributeSet attrs, int defStyle) {     
        super(context, attrs, defStyle);     
    }     
    
    @Override     
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {     
    
        int expandSpec = MeasureSpec.makeMeasureSpec(     
                Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);     
        super.onMeasure(widthMeasureSpec, expandSpec);     
    }     

}
两个xml代码如下:


list_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   
    >

       
         <TextView   
               android:layout_width="fill_parent"   
               android:layout_height="wrap_content"   
               android:text="TextView01"   
               android:id="@+id/itemText"
               android:textColor="#000000"
               >  
         </TextView> 
         
         <com.news.MyGridView 
             android:id="@+id/grideview"
             android:layout_marginTop="14dip"
             android:layout_below="@+id/itemText"
             android:layout_height="fill_parent"
             android:layout_width="fill_parent"
             android:numColumns="2"
			    android:verticalSpacing="10dp"
			    android:horizontalSpacing="10dp"
			    android:columnWidth="90dp"
			    android:stretchMode="columnWidth"
			    android:gravity="center"
             />
       
</RelativeLayout>
list_view_itemimg.xml

<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"
     >

    <TextView 
        android:id="@+id/items"
        android:text=""
       android:layout_height="wrap_content"
       android:layout_width="fill_parent"
        
        
        />

</RelativeLayout>

你可能感兴趣的:(android listveiw 嵌套Girdview)