Search code examples
androidandroid-layoutgridviewhorizontal-scrollingandroid-scrollview

Gridview Onitemclicklistener is not working properly when i put a horizontal scrollview in layout


I am developing an android project where i have to implement A gridView with a upper horizontal scroll view panel. When i put only gridView in the layout "onitemclicklistener" is working as expected but when i put both horizontal scroll view and gridview then "OnItemclickListener " is not working properly.It is selecting different item while clicking on a item. After debugging i found it is not selecting correct position. My layout xml code like below

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

and my onitemclickevent code is

     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);
            }
        });

I can't understand why this is happening. please provide me a solution. Thanks in advance.


Solution

  • I Haven't got the answer. But which I did is call the ClickListener from the custom adapter and By calling ClickListener Item click is getting the correct position and worked properly as expected.