《Android开发卷——ListView嵌套GridView(基础)》

 

listview嵌套gridview,最主要应该解决的问题是listview跟GridView的滑动问题.这个利用GridView是自定义的,就是让GridView内容有多大就显示多大,然后禁用他的滑动,这样就不会跟listview的滑动冲突了.

public class MyGridView extends GridView  
{  
    public MyGridView(android.content.Context context,  
            android.util.AttributeSet attrs)  
    {  
        super(context, attrs);  
    }  
  
    /** 
     * 设置不滚动 
     */  
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)  
    {  
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,  
                MeasureSpec.AT_MOST);  
        super.onMeasure(widthMeasureSpec, expandSpec);  
  
    }  
  
} 

listview嵌套gridview,会想到这个的人,对他们怎样布局应该很清楚了.最外是listview,listview的每个item是gridview,gridview的每个item再是其他布局(这里不上代码).


主类代码,我这里的listview是给定的多少个,对listview熟悉的人应该知道怎么动态添加listview,这里为了方便就偷下懒哈.gridview也是一样,给定九个子item.

public class MainActivity extends Activity {
	
	private ListView mListView;
	private MyAdapter myAdapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mListView = (ListView)this.findViewById(R.id.listView);
		myAdapter = new MyAdapter(this);
		mListView.setAdapter(myAdapter);
	}
	
	class MyAdapter extends BaseAdapter{
		
		private Context context;
		private LayoutInflater inflater;
		
		private GridView gridView;
		private ImageView imageView;
		private int[] imgId = new int[]{R.drawable.ic_launcher
				,R.drawable.ic_launcher,R.drawable.ic_launcher,
				R.drawable.ic_launcher,R.drawable.ic_launcher,
				R.drawable.ic_launcher,R.drawable.ic_launcher,
				R.drawable.ic_launcher,R.drawable.ic_launcher};
		
		MyAdapter(Context context){
			this.context = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 20;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int arg0, View view, ViewGroup arg2) {
			// TODO Auto-generated method stub
			view = inflater.from(context).inflate(R.layout.test, null);
			gridView = (GridView)view.findViewById(R.id.gridview);
			
			List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();
			
			for(int i = 0;i<imgId.length;i++){
				Map<String,Object> map = new HashMap<String, Object>();
				map.put("img", imgId[i]);
				listItems.add(map);
			}
			
			SimpleAdapter simpleAdapter = new SimpleAdapter(context, listItems, R.layout.image, new String[]{"img"}, new int[]{R.id.img});
			
			gridView.setAdapter(simpleAdapter);
			return view;
		}
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

这只是个listview嵌套gridview的基础.大家可以在这个基础上作出很多好看的界面,或者功能来的.


源码地址:http://download.csdn.net/detail/chillax_li/7184825


尊重原创,转载请注明出处:http://blog.csdn.net/chillax_li/article/details/23568071




posted @ 2014-04-13 00:49  w_Kong  阅读(180)  评论(0编辑  收藏  举报