首页 > 其他分享 >让GridView拥有Gallery的拖动功能

让GridView拥有Gallery的拖动功能

时间:2023-04-07 10:09:00浏览次数:51  
标签:GridView layout 拖动 item int gridView Gallery simple


利用Gallery的拖动功能,能很容易的将在一行上显示不下的内容显示出来。
这种特性可以用在菜单上(如果菜单足够多,以至一行显示不下)。
但是Gallery有个不爽的地方,就是被点击的那个item会一直出现在中间,有时候我们并不需要这样的“智能”!怎么办呢?
那就利用GridView,将GridView放在HorizontalScrollView中,如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
  	android:orientation="vertical"
  	android:layout_width="fill_parent"
  	android:layout_height="fill_parent">

   	<RelativeLayout android:background="#030e13"
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
	    >
	    <ImageView android:id="@+id/webnav_left" 
	        android:layout_width="8dip"
	        android:layout_height="wrap_content"
	        android:layout_centerVertical="true"
	        android:src="@drawable/news_left"
			/>
	    <ImageView android:id="@+id/webnav_right" 
	        android:layout_width="8dip"
	        android:layout_height="wrap_content"
    		android:layout_alignParentRight="true"
	        android:layout_centerVertical="true"
	        android:src="@drawable/news_right"
			/>
		<HorizontalScrollView android:layout_width="fill_parent"
	        android:layout_height="wrap_content"
       		android:layout_toLeftOf="@id/webnav_right"
       		android:layout_toRightOf="@id/webnav_left"
	        android:scrollbars="none">
	        <LinearLayout android:layout_width="fill_parent"
	            android:layout_height="wrap_content">
	            <LinearLayout android:id="@+id/layout_webnav" 
	            	android:layout_width="800dip"
	                android:layout_height="wrap_content"
	                android:orientation="horizontal"> 
				  	<GridView android:id="@+id/gallery_webnav" 
						android:layout_width="fill_parent"
						android:layout_height="fill_parent"
						android:background="#030e13"
				        android:gravity="center" 
				        android:numColumns="auto_fit"
				        android:listSelector="#00000000">
				    </GridView> 
	    		</LinearLayout>
	        </LinearLayout>
	    </HorizontalScrollView>
  	</RelativeLayout>

</LinearLayout>



注意上面id为layout_webnav的LinearLayout,这里的layout_width是需要计算的!见下面代码。


public class Test_2_Activity  extends Activity{

	private final int per=3;//每行显示的个数
	private GridView gridView;
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.test2);
	        DisplayMetrics dm = new DisplayMetrics();
	        dm = getApplicationContext().getResources().getDisplayMetrics();
	        int menuWidth = dm.widthPixels-16;
	        
	        gridView= (GridView) findViewById(R.id.gallery_webnav); 
	        int itemWidth = menuWidth/per;
	        gridView.setColumnWidth(itemWidth);
	        
	        ArrayList<Map<String,String>> data=new ArrayList<Map<String,String>>();
	        Map<String,String> map;
	        for(int i=0;i<5;i++){
	        	map=new HashMap<String,String>();
	        	map.put("simple_item_1", "name"+i);
	        	map.put("simple_item_2", "age"+i);
	        	map.put("simple_item_3", "class"+i);
	        	data.add(map);
	        }
	        int resource=R.layout.row_test2;
	        String[] from={"simple_item_1","simple_item_2","simple_item_3"};
	        int[] to={R.id.simple_item_1,R.id.simple_item_2,R.id.simple_item_3};
	        SimpleAdapter adapter=new SimpleAdapter(this, data, resource, from, to);
	        
	        gridView.setAdapter(adapter);
	        
	      //让GridView一行显示,这里的layout_width是需要计算的
	        LinearLayout layout = (LinearLayout) findViewById(R.id.layout_webnav); 
	        layout.setLayoutParams(new LayoutParams(itemWidth*data.size(), LayoutParams.WRAP_CONTENT));
	        
	        gridView.setSelection(0);
                gridView.setOnItemClickListener(listener);
	 }
}



注意上面的itemWidth*data.size(),这里才是LinearLayout的实际宽度!



R.layout.row_test2布局如下:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  	<TextView android:id="@+id/simple_item_1"
	    android:layout_width="fill_parent" 
	    android:layout_height="fill_parent" 
	    android:gravity="center"
	    />
    <RelativeLayout android:background="#030e13"
    	android:layout_width="fill_parent"
    	android:layout_height="fill_parent"
	    >
	    <TextView android:id="@+id/simple_item_2"
	    android:layout_width="wrap_content" 
	    android:layout_height="wrap_content" 
	    android:layout_alignParentLeft="true"
	    android:paddingLeft="10dp"
	    />
	    <TextView android:id="@+id/simple_item_3"
	    android:layout_width="wrap_content" 
	    android:layout_height="wrap_content" 
	    android:layout_alignParentRight="true"
	    android:paddingRight="10dp"
	    />
	</RelativeLayout>
</LinearLayout>



运行后显示的效果:




看不出什么,呵呵,拖动一下看看。




  • 让GridView拥有Gallery的拖动功能_Test

  • 大小: 5.8 KB
  • 让GridView拥有Gallery的拖动功能_xml_02

  • 大小: 8.5 KB
  • 查看图片附件

标签:GridView,layout,拖动,item,int,gridView,Gallery,simple
From: https://blog.51cto.com/u_5454003/6174576

相关文章

  • ImageSwitcher&Gallery练习
    先看图再说:布局如下:<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout......
  • android 拖拽图片&拖动浮动按钮到处跑
    来自老外:importandroid.app.Activity;importandroid.content.Context;importandroid.graphics.Canvas;importandroid.os.Bundle;importandroid.view.MotionEvent;importandroid.widget.AbsoluteLayout;importandroid.widget.Button;public......
  • 拖动一个控件在另一个控件(layout)上,并固定位置在几个位置显示
    实现效果:鼠标拖动btnSSS,SSS在水平的layout上移动。当鼠标抬起响应UP事件。SSS会自动移动到距离其最近的Btn上,与其重合。即SSS如图只存在五个固定的显示位置。SSS响应setOnTouchListener事件。在MotionEvent.ACTION_UP事件中,调用TranslateAnimation动画效果,将其从UP事件位置......
  • 上拉下拉刷新,支持ListView,GridView,ScrollView
    开源项目PullToRefresh详解(一)——PullToRefreshListView类似Lollipop滚动溢出效果的下拉刷新布局:JellyRefreshLayouthttp://www.open-open.com/lib/view/open1437223823115.html网上很多这样的效果都是继承某个具体的AdapterView或ScrollView,这样很不通......
  • Gallery默认第一项居左解决方案
    网上找的,暂时用不到没测试过。Android中的Gallery控件默认会将第一项居中显示,在某些场景会影响用户体验,分享一下居左的解决方案:/***Alignthefirstgalleryitemtotheleft.**@paramparentViewTheviewcontainingthegallerywidget(weass......
  • 带header的GridView(HeaderGridView)
    扣自goolgeandroid.photos/**Copyright(C)2013TheAndroidOpenSourceProject**LicensedundertheApacheLicense,Version2.0(the"License");*youmaynotusethisfileexceptincompliancewiththeLicense.*Yo......
  • Android开发-Android常用组件-SeekBar拖动条
    4.9 SeekBar拖动条android:max滑动条的最大值android:progress滑动条的当前值android:secondaryProgress二级滑动条的进度android:thumb滑块的drawable 接着要说下SeekBar的事件了,SeekBar.OnSeekBarChangeListener我们只需重写三个对应......
  • DEVEXPRESS GRIDVIEW 代码添加按钮
    1.1创建一个RepositoryItemButtonEdit方法privateRepositoryItemButtonEditCreateRepositoryItemButtonEdit(Dictionary<object,string>dicButtons){RepositoryItemButtonEditrepositoryBtn=newRepositoryItemButtonEdit();re......
  • Winform/Csharp中使用定时器+分页算法实现DataGridView自动分页显示(模拟大屏切换效果
    场景Winform中DataGridView设置前景色、单元格背景色、标题栏样式、禁止改变高宽、不显示空白行、清除选中样式、填充数据源、设置标题、设置单列宽度:https://blog.csdn.......
  • div 实现类似textarea的拖动大小的效果
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width......