当我在布局中放置水平滚动视图时,Gridview Onitemclicklistener 无法正常工作
Gridview Onitemclicklistener is not working properly when i put a horizontal scrollview in layout
我正在开发一个 android 项目,我必须在其中实现一个带有上方水平滚动视图面板的 gridView。当我只在布局中放置 gridView 时 "onitemclicklistener" 按预期工作但是当我同时放置水平滚动视图和 gridview 时 "OnItemclickListener " 不工作 properly.It 在单击项目时选择不同的项目.调试后我发现它没有选择正确的位置。我的布局 xml 代码如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/picUpperLayout"
android:layout_marginBottom="20dp"
android:gravity="center_vertical">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/picNavigationButton" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Picture"
android:id="@+id/picTitle"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:layout_marginLeft="20dp" />
</LinearLayout>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/horizontalScrollView"
android:background="#000000"
android:layout_below="@+id/picUpperLayout"
android:focusable="false"
android:focusableInTouchMode="false">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Picture of\n The Week"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="@+id/pic_pictureOfTheWeek"
android:textColor="#ffffff"
android:lines="2" />
<ImageView
android:layout_width="match_parent"
android:layout_height="7dp"
android:id="@+id/pic_picoftheweekImage"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Bangladeshi\n Celebrity"
android:id="@+id/pic_b_celebrity"
android:textColor="#FFFFFFFF" />
<ImageView
android:layout_width="match_parent"
android:layout_height="7dp"
android:id="@+id/pic_b_celebrity_imageview"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Bollywood\nCelebrity"
android:id="@+id/pic_bolly_celebrity"
android:textColor="#FFFFFFFF" />
<ImageView
android:layout_width="match_parent"
android:layout_height="7dp"
android:id="@+id/pic_bolly_celebrity_imageiview"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Cool"
android:id="@+id/pic_cool"
android:textColor="#FFFFFFFF" />
<ImageView
android:layout_width="match_parent"
android:layout_height="7dp"
android:id="@+id/pic_cool_imageview"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Hollywood\nCelebrity"
android:id="@+id/pic_holly_celebrity"
android:textColor="#FFFFFFFF" />
<ImageView
android:layout_width="match_parent"
android:layout_height="7dp"
android:id="@+id/pic_holly_celebrity_imageView"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Love"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="@+id/pic_love"
android:singleLine="false"
android:textColor="#FFFFFFFF" />
<ImageView
android:layout_width="match_parent"
android:layout_height="7dp"
android:id="@+id/pic_love_imageView"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Speacial\nEvents"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="@+id/pic_speacial_events"
android:textColor="#FFFFFFFF" />
<ImageView
android:layout_width="match_parent"
android:layout_height="7dp"
android:id="@+id/pic_speacial_event_imageView"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Sticker"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="@+id/pic_sticker"
android:textColor="#FFFFFFFF" />
<ImageView
android:layout_width="match_parent"
android:layout_height="7dp"
android:id="@+id/pic_sticker_imageView"
android:layout_marginTop="5dp" />
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/horizontalScrollView">
<GridView
android:id="@+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:horizontalSpacing="2dp"
android:clickable="true"
android:layout_marginTop="20dp" />
</LinearLayout>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_centerInParent="true"
android:visibility="gone"/>
</RelativeLayout>
我的 onitemclickevent 代码是
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Get item at position
PictureGridItemModel item = (PictureGridItemModel) parent.getItemAtPosition(position);
Intent intent = new Intent(PictureGridViewActivity.this, PictureDetailsActivity.class);
ImageView imageView = (ImageView) v.findViewById(R.id.grid_item_image);
int[] screenLocation = new int[2];
imageView.getLocationOnScreen(screenLocation);
Log.d("screenLocation", String.valueOf(screenLocation));
Log.d("screenLocation",String.valueOf(imageView.getY()));
Log.d("screenPosition",String.valueOf(position));
//Pass the image title and url to DetailsActivity
intent.putExtra("left", screenLocation[0]).
putExtra("top", screenLocation[1]).
putExtra("width", imageView.getWidth()).
putExtra("height", imageView.getHeight()).
putExtra("title", item.getTitle()).
putExtra("image", item.getImage()).
putExtra("contentCode",item.getContent_code()).
putExtra("categoryCode",item.getCategoryCode()).
putExtra("contentName", item.getContent_name()).
putExtra("sContentType",item.getsContentType()).
putExtra("sPhysicalFileName",item.getsPhysicalFileName()).
putExtra("contentImg",item.getContent_img()).
putExtra("ZedID",item.getZedID());
startActivity(intent);
}
});
我不明白为什么会这样。请给我一个解决方案。提前致谢。
我还没有得到答案。但我所做的是从自定义适配器调用 ClickListener 并通过调用 ClickListener Item click 获得正确的位置并按预期正常工作。
我正在开发一个 android 项目,我必须在其中实现一个带有上方水平滚动视图面板的 gridView。当我只在布局中放置 gridView 时 "onitemclicklistener" 按预期工作但是当我同时放置水平滚动视图和 gridview 时 "OnItemclickListener " 不工作 properly.It 在单击项目时选择不同的项目.调试后我发现它没有选择正确的位置。我的布局 xml 代码如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/picUpperLayout"
android:layout_marginBottom="20dp"
android:gravity="center_vertical">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/picNavigationButton" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Picture"
android:id="@+id/picTitle"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:layout_marginLeft="20dp" />
</LinearLayout>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/horizontalScrollView"
android:background="#000000"
android:layout_below="@+id/picUpperLayout"
android:focusable="false"
android:focusableInTouchMode="false">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Picture of\n The Week"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="@+id/pic_pictureOfTheWeek"
android:textColor="#ffffff"
android:lines="2" />
<ImageView
android:layout_width="match_parent"
android:layout_height="7dp"
android:id="@+id/pic_picoftheweekImage"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Bangladeshi\n Celebrity"
android:id="@+id/pic_b_celebrity"
android:textColor="#FFFFFFFF" />
<ImageView
android:layout_width="match_parent"
android:layout_height="7dp"
android:id="@+id/pic_b_celebrity_imageview"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Bollywood\nCelebrity"
android:id="@+id/pic_bolly_celebrity"
android:textColor="#FFFFFFFF" />
<ImageView
android:layout_width="match_parent"
android:layout_height="7dp"
android:id="@+id/pic_bolly_celebrity_imageiview"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Cool"
android:id="@+id/pic_cool"
android:textColor="#FFFFFFFF" />
<ImageView
android:layout_width="match_parent"
android:layout_height="7dp"
android:id="@+id/pic_cool_imageview"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Hollywood\nCelebrity"
android:id="@+id/pic_holly_celebrity"
android:textColor="#FFFFFFFF" />
<ImageView
android:layout_width="match_parent"
android:layout_height="7dp"
android:id="@+id/pic_holly_celebrity_imageView"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Love"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="@+id/pic_love"
android:singleLine="false"
android:textColor="#FFFFFFFF" />
<ImageView
android:layout_width="match_parent"
android:layout_height="7dp"
android:id="@+id/pic_love_imageView"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Speacial\nEvents"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="@+id/pic_speacial_events"
android:textColor="#FFFFFFFF" />
<ImageView
android:layout_width="match_parent"
android:layout_height="7dp"
android:id="@+id/pic_speacial_event_imageView"
android:layout_marginTop="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Sticker"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="@+id/pic_sticker"
android:textColor="#FFFFFFFF" />
<ImageView
android:layout_width="match_parent"
android:layout_height="7dp"
android:id="@+id/pic_sticker_imageView"
android:layout_marginTop="5dp" />
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/horizontalScrollView">
<GridView
android:id="@+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:horizontalSpacing="2dp"
android:clickable="true"
android:layout_marginTop="20dp" />
</LinearLayout>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_centerInParent="true"
android:visibility="gone"/>
</RelativeLayout>
我的 onitemclickevent 代码是
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Get item at position
PictureGridItemModel item = (PictureGridItemModel) parent.getItemAtPosition(position);
Intent intent = new Intent(PictureGridViewActivity.this, PictureDetailsActivity.class);
ImageView imageView = (ImageView) v.findViewById(R.id.grid_item_image);
int[] screenLocation = new int[2];
imageView.getLocationOnScreen(screenLocation);
Log.d("screenLocation", String.valueOf(screenLocation));
Log.d("screenLocation",String.valueOf(imageView.getY()));
Log.d("screenPosition",String.valueOf(position));
//Pass the image title and url to DetailsActivity
intent.putExtra("left", screenLocation[0]).
putExtra("top", screenLocation[1]).
putExtra("width", imageView.getWidth()).
putExtra("height", imageView.getHeight()).
putExtra("title", item.getTitle()).
putExtra("image", item.getImage()).
putExtra("contentCode",item.getContent_code()).
putExtra("categoryCode",item.getCategoryCode()).
putExtra("contentName", item.getContent_name()).
putExtra("sContentType",item.getsContentType()).
putExtra("sPhysicalFileName",item.getsPhysicalFileName()).
putExtra("contentImg",item.getContent_img()).
putExtra("ZedID",item.getZedID());
startActivity(intent);
}
});
我不明白为什么会这样。请给我一个解决方案。提前致谢。
我还没有得到答案。但我所做的是从自定义适配器调用 ClickListener 并通过调用 ClickListener Item click 获得正确的位置并按预期正常工作。