Search code examples
androidlistviewscrollhorizontallist

How to get horizental Listview's position to variable in android?


I am developing an Application.

My layout is in my Previous Question.

For that I am using horizontal List view. In that I want to Get the position of the images while Scroll is changed.

I had tried for this:

hlv.setOnScrollStateChangedListener(new OnScrollStateChangedListener() {

            @Override
            public void onScrollStateChanged(ScrollState scrollState) {
                // TODO Auto-generated method stub

                for(int i=0;i<albumList.size();i++)
                {
                    if(hlv.callOnClick())
                    {
                        int id=i;
                        Toast.makeText(getApplicationContext(), ""+id, 1000).show();
                    }
                }
            }
        });

But didnt successed. I didnt got the position of element in the Variable.


Solution

  • Did you tried the setOnItemClickListener for listview?. Even for a custom HorizontalListView it should be implemented as it is based on a normal ListView. Try it:

    hlv.setOnItemClickListener(
         new OnItemClickListener()
         {
          public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
               Toast.makeText(this, "POSITION: "+position, Toast.LENGTH_SHORT).show();
           }
         }
      );
    

    Hope it helps!