m7md07sn
m7md07sn

Reputation: 3

android - Scroll and click on item inside ScrollView

I am having a trouble using Scrollview of linearlayout which contains some imageviews as i want to click on each imageview separately but the onclick method just gives me access to the linearlayout.

this is the layout for the scrollview which contains the linearlayout

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/horizontalScrollView"
            android:layout_below="@+id/seekBar"
            android:layout_alignParentLeft="true"
            android:layout_above="@+id/nextButton"
            android:layout_alignParentRight="true" >

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/eyesScrollView"></LinearLayout>
        </HorizontalScrollView>

and i add the imageviews programitically by this code

LinearLayout eyesScrollView = (LinearLayout) findViewById(R.id.eyesScrollView);
        for (int k = 0; k < 5; k++) {
            ImageView imageView2 = new ImageView(this);
            imageView2.setId(k);
            imageView2.setPadding(2, 2, 2, 2);
            imageView2.setImageBitmap(BitmapFactory.decodeResource(getResources(), eyesArrayList.get(k)));
            eyesScrollView.addView(imageView2);
        }

and this is the code which access the linearlayout onclick

@Override
    public void onClick(View view) {
        LinearLayout manyPics = (LinearLayout) view;
        manyPics.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view){
                Log.v("theID",String.valueOf(view.getId()));
            }
        });
    }

please can anyone helps me with that problem

Upvotes: 0

Views: 2841

Answers (2)

Nouman Tahir
Nouman Tahir

Reputation: 849

Although adding horizontal linear layout within horizontalScrollView let's you make a horizontally scroll-able listView, but this is really discourage and is bad in terms of performance.

The correct method for achieving horizontal ListView in to use RecyclerView and set its LinearLayoutManager in Horizontal Direction

Here is an overview for the task... When you use a RecyclerView, you need to specify a LayoutManager that is responsible for laying out each item in the view. The LinearLayoutManager allows you to specify an orientation, just like a normal LinearLayout would.

To create a horizontal list with RecyclerView, you might do something like this:

LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false);
RecyclerView myList = (RecyclerView) findViewById(R.id.my_recycler_view);
myList.setLayoutManager(layoutManager);

then for click listener, you will have to set listener on imageView within you adapter for RecyclerView.

Here you may find this link helpful...http://www.androidhive.info/2016/01/android-working-with-recycler-view/

Upvotes: 0

Ricardo
Ricardo

Reputation: 9656

You are assigning your click event to your LinearLayout therefor wherever you click inside that layout area it will trigger the onClick event from the LinearLayout and not the imageView itself. To accomplish what you want, in your for loop and before adding the view, you need to assign the onClick event to the ImageView once you create it.

Code example:

LinearLayout eyesScrollView = (LinearLayout) findViewById(R.id.eyesScrollView);
    for (int k = 0; k < 5; k++) {
        ImageView imageView2 = new ImageView(this);
        imageView2.setId(k);
        imageView2.setPadding(2, 2, 2, 2);
        imageView2.setImageBitmap(BitmapFactory.decodeResource(getResources(), eyesArrayList.get(k)));
        imageView2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view){
            doMyImageViewLogic(view);
        }
    });
        eyesScrollView.addView(imageView2);
    }

Finally do your logic

public void doMyImageViewLogic(View view){
    if(view.getId() == someId)
        doStuff();
    else if(view.getId() == otherId)
        doOtherStuff();
    .
    .
    .
}

And so on

Upvotes: 1

Related Questions