(android地图开发) 高德地图添加抽屉栏效果

java哥 阅读:597 2021-04-01 10:20:04 评论:0

截图效果:

Android 抽屉相关介绍:

 SlidingDrawer(滑动式抽屉)隐藏屏外的内容,并允许用户拖拽一个handle以显示隐藏的内容。它由两个子视图组成:一个是用户拖拽的handle(柄),另一个是随着拖动变化的content(内容)。SlidingDrawer应当作为内部布局的覆盖来使用,也就是说SlidingDrawer内部应该使用FrameLayout或RelativeLayout布局。SlidingDrawer的大小决定了其内容显示时所占空间的大小,所以它的尺寸一般定义为match_parent。在XML布局中SlidingDrawer必须指定handle和content的id. 
 
其中: 
 
android:allowSingleTap     指示是否可通过单击handle打开或关闭(如果是false,刚用户必须通过拖动,滑动或者使用轨迹球,来打开/关闭抽屉。)默认的是true。 
 
android:animateOnClick    指示当用户点击handle的时候,抽屉是否以动画的形式打开或关闭。默认的是true。 
 
android:bottomOffset      Handle距离SlidingDrawer底部的额外距离 
 
android:content              标识SlidingDrawer的内容 
 
android:handle               标识SlidingDrawer的han 
 


源代码:

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <SlidingDrawer 
        android:id="@+id/slidingDrawer1" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:content="@+id/content" 
        android:handle="@+id/handle" > 
         
        <Button 
            android:id="@+id/handle" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:background="@drawable/sliding_handle_sel" 
            /> 
		 
          
        <LinearLayout 
            android:id="@+id/content" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"          
            android:background="#ffffff" 
            android:orientation="vertical" 
            > 
         
             
            <GridView              
                android:id="@+id/grid" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:horizontalSpacing="8dp" 
                android:numColumns="5" 
                android:verticalSpacing="6dp"               
                ></GridView>   
                 
        
                 <GridView              
                 android:id="@+id/grids" 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:horizontalSpacing="8dp" 
                android:numColumns="4" 
                android:verticalSpacing="12dp"   
                android:layout_marginTop="10dp"             
                ></GridView>   
             
            
        </LinearLayout> 
         
    </SlidingDrawer> 
 
</LinearLayout>


组件和适配器代码:

package com.rf.adapter; 
 
 
 
import com.rf.component.ViewHolder; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
 
 
public class ImageAdapter extends BaseAdapter { 
	// 定义相关参数(Context:上下文,Integer[]:图片资源) 
	private Context context; 
	private int[] imageIds; 
	private String[] contents; 
 
	// 相关的构造函数 
	public ImageAdapter(Context context, int[] imageIds,String[] contents) { 
		super(); 
		this.context = context; 
		this.imageIds = imageIds; 
		this.contents=contents; 
	} 
 
 
 
	@Override 
	public int getCount() { 
		// TODO Auto-generated method stub 
		return imageIds.length; 
	} 
 
	@Override 
	public Object getItem(int arg0) { 
		// TODO Auto-generated method stub 
		return arg0; 
	} 
 
	@Override 
	public long getItemId(int position) { 
		// TODO Auto-generated method stub 
		return position; 
	} 
    //核心代码 
	@Override 
	public View getView(int position, View convertView, ViewGroup parent) { 
		// TODO Auto-generated method stub 
		ViewHolder holder; 
		if (convertView == null) { 
			LayoutInflater inflater = LayoutInflater.from(context); 
			convertView = inflater.inflate(com.rf.mapabc.R.layout.adapter, null); 
			holder = new ViewHolder(convertView); 
			// 设置标识 
			convertView.setTag(holder); 
		} else { 
			holder = (ViewHolder) convertView.getTag(); 
		} 
		holder.getImageView().setBackgroundResource(imageIds[position]); 
		holder.getTextview().setText(contents[position]); 
 
		return convertView; 
	} 
 
}


 

package com.rf.adapter; 
 
import com.rf.component.TextHolder; 
import com.rf.component.ViewHolder; 
 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
 
public class MenuAdapter extends BaseAdapter { 
	//相关参数 
	private Context context; 
	private String[] contexts; 
	//相关构造函数 
	public MenuAdapter(Context context,String[] contexts) { 
		super(); 
		this.context = context; 
		this.contexts=contexts; 
	} 
 
	@Override 
	public int getCount() { 
		// TODO Auto-generated method stub 
		return contexts.length; 
	} 
	@Override 
	public Object getItem(int position) { 
		// TODO Auto-generated method stub 
		return position; 
	} 
 
	@Override 
	public long getItemId(int position) { 
		// TODO Auto-generated method stub 
		return position; 
	} 
 
	@Override 
	public View getView(int position, View convertView, ViewGroup parent) { 
		// TODO Auto-generated method stub 
		TextHolder holder; 
		if (convertView == null) { 
			LayoutInflater inflater = LayoutInflater.from(context); 
			convertView = inflater.inflate(com.rf.mapabc.R.layout.menu, null); 
			holder = new TextHolder(convertView); 
			// 设置标识 
			convertView.setTag(holder); 
		} else { 
			holder = (TextHolder) convertView.getTag(); 
		} 
		holder.getTextview().setText(contexts[position]); 
		return convertView; 
	} 
 
}


 

package com.rf.component; 
 
import com.rf.mapabc.R; 
 
import android.view.View; 
import android.widget.TextView; 
 
public class TextHolder { 
	private View baseview; 
	private TextView textview;	 
	 
	//相关构造函数 
	public TextHolder(View baseview) { 
		super(); 
		this.baseview = baseview; 
	} 
	//获取相关组件 
	public TextView getTextview() { 
		textview=(TextView) baseview.findViewById(R.id.text); 
		return textview; 
	} 
 
 
	 
 
}


 

package com.rf.component; 
 
import com.rf.mapabc.R; 
import android.view.View; 
import android.widget.ImageView; 
import android.widget.TextView; 
 
public class ViewHolder { 
	private View baseView; 
 
	private ImageView imageView; 
	private TextView textview; 
 
	// 相关的构造函数 
	public ViewHolder(View baseView) { 
		super(); 
		this.baseView = baseView; 
	} 
 
	// 相关组件实例化方法 
 
	public ImageView getImageView() { 
		imageView = (ImageView) baseView.findViewById(R.id.imageView); 
		return imageView; 
	} 
 
	public TextView getTextview() { 
		textview = (TextView) baseView.findViewById(R.id.textView); 
		return textview; 
	} 
 
}


核心代码:

package com.rf.mapabc; 
 
import com.rf.adapter.ImageAdapter; 
import com.rf.adapter.MenuAdapter; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.GridView; 
import android.widget.SlidingDrawer; 
import android.widget.SlidingDrawer.OnDrawerOpenListener; 
 
public class Test extends Activity { 
 
	@Override 
	public void onCreate(Bundle savedInstanceState) { 
		// TODO Auto-generated method stub 
		super.onCreate(savedInstanceState); 
		setContentView(R.layout.test); 
		//抽屉组件类 
		SlidingDrawer slidingDrawer1=(SlidingDrawer) findViewById(R.id.slidingDrawer1); 
		//相关数据信息 
		 int[] images=new int[]{ 
					R.drawable.pg1, 
					R.drawable.pg2, 
					R.drawable.pg3, 
					R.drawable.pg4, 
					R.drawable.pg5, 
					R.drawable.pg6, 
					R.drawable.pg7, 
					R.drawable.pg8, 
					R.drawable.pg9, 
					R.drawable.pg10 
			}; 
		 String[] contents=new String[]{ 
				 "团购","美食","酒店","公交站","银行","电影院","KTV","丽人","超市","景点" 
				  
		 }; 
		  
		 String[] texts=new String[]{ 
			"中餐","小吃快餐","火锅","自助餐","外卖","湘菜","西餐","KFC","星级酒店","连锁酒店","今夜特价","招待所","电影院","KTV","网吧","酒吧","咖啡厅","商场","景点","洗浴","公交站","加油站","停车场","火车站"  
		 }; 
			//GridView 组件 
			final GridView gridview=(GridView) findViewById(R.id.grid); 
			final GridView girviews=(GridView) findViewById(R.id.grids); 
			//资源文件绑定 
			final ImageAdapter adapter=new ImageAdapter(this,images,contents); 
			final MenuAdapter adapters=new MenuAdapter(this,texts); 
			//抽屉类(打开事件) 
			slidingDrawer1.setOnDrawerOpenListener(new OnDrawerOpenListener(){ 
			@Override 
			public void onDrawerOpened() { 
				 
					gridview.setAdapter(adapter); 
					girviews.setAdapter(adapters); 
					//编写想的监听事件 
				 
			} 
			 
		}); 
		 
		 
		 
	}


 

标签:Android
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号