Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the setSelection doest not work #10

Open
usherdl opened this issue Jul 7, 2014 · 6 comments
Open

the setSelection doest not work #10

usherdl opened this issue Jul 7, 2014 · 6 comments

Comments

@usherdl
Copy link

usherdl commented Jul 7, 2014

After the HorizontalListView init ,I need to locate to the middle of the list , I find the method setSelection but it does not work for me , Then I google how to use it in normal listview . I almost try every way but still now work .I am not sure it is a bug of Android-HorizontalListView .so can you help me . thank you .And forgive my poor Englist.

@guyzk
Copy link

guyzk commented Oct 7, 2014

I Have the same problem exactly!
Please address this issue...

@pietmau
Copy link

pietmau commented Oct 8, 2014

Same here

@yuxiangping
Copy link

hi, i find a way to solve setSelection.

  private int mChildWidth;

  @Override
  public void setSelection(int position) {
    if(mChildWidth == 0) {
      mChildWidth = getSelectedView().getWidth();
    }

    int positionX = position * mChildWidth;
    if (positionX <= 0) {
      positionX  = 0;
    }
    int maxWidth = mAdapter.getCount() * mChildWidth;
    if (positionX > maxWidth) {
      positionX = maxWidth;
    }

    scrollTo(positionX);
    mCurrentlySelectedAdapterIndex = position;
  }

@linjian99666
Copy link

here are some problem if you use the code above,you will get NullPointException at "getSelectedView()",you can try like this
if (mChildWidth == 0) {
View selectedView = getSelectedView();
if (selectedView != null) {
mChildWidth = selectedView.getWidth();
}
}
and then modify the method
@OverRide
public View getSelectedView() {
View kid = getChild(mCurrentlySelectedAdapterIndex);
if(kid != null){
kid.setSelected(true);
}
return kid;
}
@yuxiangping i am not sure whatever you had encountered this type of problem

@rishabhbsdei
Copy link

I am also getting the null pointer exception with the code posted by @yuxiangping

@br3ant
Copy link

br3ant commented Aug 15, 2017

Perfectly solved the same problem,thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants