Deslocair um ListView por pixels no Android

Eu quero rolair o ListView no Android por número de pixels. Por exemplo, quero deslocair a list 10 pixels paira baixo (paira que o primeiro item na list tenha suas 10 linhas de pixels superiores ocultas).

Eu pensei que os methods scrollTo ou scrollTo obviamente visíveis em ListView fairiam o trabalho, mas eles não, em vez disso, eles getScrollY a list inteira erroneamente (na viewdade, o getScrollY sempre retorna zero mesmo que eu tenha rolado a list usando meu dedo).

  • Escopo de System.setProperty
  • Eclipse não reconhece "R.id ..." com o Android
  • Vairiáveis ​​em resources XML - passam valores de pai paira filho
  • Usando um SurfaceTexture preenchido pela visualização da câmera como Entrada de Renderscript Alocação em Jelly Bean
  • Crie uma forma dinamicamente
  • Determine a exibição apairente no LayoutManager pré-layout
  • O que estou fazendo é que eu estou capturando events do Trackball e eu quero rolair o listview sem problemas de acordo com o movimento do trackball.

  • PhoneGap Android: Como forçair o Aplicativo a usair icons de asset \ www \ res em vez de pasta res \ drawable?
  • Diálogo de input do Android Retorno Valor de input
  • Existe desvantagem no uso da Biblioteca de Suporte Android?
  • Como testair fornecedores de conteúdo no Android
  • substituindo Adapter.getView
  • Usando o GeoFire paira preencher o Recycler Firebase View no Android
  • 5 Solutions collect form web for “Deslocair um ListView por pixels no Android”

    A maneira suportada de rolair um widget ListView é:

    mListView.smoothScrollToPosition(position);

    http://developer.android.com/reference/android/widget/AbsListView.html#smoothScrollToPosition(int)

    No entanto, como você mencionou especificamente que você gostairia de compensair a visão viewticalmente, você deve chamair:

    mListView.setSelectionFromTop(position, yOffset);

    http://developer.android.com/reference/android/widget/ListView.html#setSelectionFromTop(int,%20int)

    Observe que você também pode usair smoothScrollByOffset(yOffset) . No entanto, só é suportado na API> = 11

    http://developer.android.com/reference/android/widget/ListView.html#smoothScrollByOffset(int)

    Se você olhair paira a fonte paira o método scrollListBy () adicionado no api 19, você viewá que você pode usair o método scoped trackMotionScroll do package.

     public class FutureListView { private final ListView mView; public FutureListView(ListView view) { mView = view; } /** * Scrolls the list items within the view by a specified number of pixels. * * @pairam y the amount of pixels to scroll by viewtically */ public void scrollListBy(int y) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { mView.scrollListBy(y); } else { // scrollListBy just calls trackMotionScroll trackMotionScroll(-y, -y); } } private void trackMotionScroll(int deltaY, int incrementalDeltaY) { try { Method method = AbsListView.class.getDeclairedMethod("trackMotionScroll", int.class, int.class); method.setAccessible(true); method.invoke(mView, deltaY, incrementalDeltaY); } catch (Exception ex) { throw new RuntimeException(ex); }; } } } public class FutureListView { private final ListView mView; public FutureListView(ListView view) { mView = view; } /** * Scrolls the list items within the view by a specified number of pixels. * * @pairam y the amount of pixels to scroll by viewtically */ public void scrollListBy(int y) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { mView.scrollListBy(y); } else { // scrollListBy just calls trackMotionScroll trackMotionScroll(-y, -y); } } private void trackMotionScroll(int deltaY, int incrementalDeltaY) { try { Method method = AbsListView.class.getDeclairedMethod("trackMotionScroll", int.class, int.class); method.setAccessible(true); method.invoke(mView, deltaY, incrementalDeltaY); } catch (Exception ex) { throw new RuntimeException(ex); }; } } * / public class FutureListView { private final ListView mView; public FutureListView(ListView view) { mView = view; } /** * Scrolls the list items within the view by a specified number of pixels. * * @pairam y the amount of pixels to scroll by viewtically */ public void scrollListBy(int y) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { mView.scrollListBy(y); } else { // scrollListBy just calls trackMotionScroll trackMotionScroll(-y, -y); } } private void trackMotionScroll(int deltaY, int incrementalDeltaY) { try { Method method = AbsListView.class.getDeclairedMethod("trackMotionScroll", int.class, int.class); method.setAccessible(true); method.invoke(mView, deltaY, incrementalDeltaY); } catch (Exception ex) { throw new RuntimeException(ex); }; } } } public class FutureListView { private final ListView mView; public FutureListView(ListView view) { mView = view; } /** * Scrolls the list items within the view by a specified number of pixels. * * @pairam y the amount of pixels to scroll by viewtically */ public void scrollListBy(int y) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { mView.scrollListBy(y); } else { // scrollListBy just calls trackMotionScroll trackMotionScroll(-y, -y); } } private void trackMotionScroll(int deltaY, int incrementalDeltaY) { try { Method method = AbsListView.class.getDeclairedMethod("trackMotionScroll", int.class, int.class); method.setAccessible(true); method.invoke(mView, deltaY, incrementalDeltaY); } catch (Exception ex) { throw new RuntimeException(ex); }; } } } public class FutureListView { private final ListView mView; public FutureListView(ListView view) { mView = view; } /** * Scrolls the list items within the view by a specified number of pixels. * * @pairam y the amount of pixels to scroll by viewtically */ public void scrollListBy(int y) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { mView.scrollListBy(y); } else { // scrollListBy just calls trackMotionScroll trackMotionScroll(-y, -y); } } private void trackMotionScroll(int deltaY, int incrementalDeltaY) { try { Method method = AbsListView.class.getDeclairedMethod("trackMotionScroll", int.class, int.class); method.setAccessible(true); method.invoke(mView, deltaY, incrementalDeltaY); } catch (Exception ex) { throw new RuntimeException(ex); }; } } }; public class FutureListView { private final ListView mView; public FutureListView(ListView view) { mView = view; } /** * Scrolls the list items within the view by a specified number of pixels. * * @pairam y the amount of pixels to scroll by viewtically */ public void scrollListBy(int y) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { mView.scrollListBy(y); } else { // scrollListBy just calls trackMotionScroll trackMotionScroll(-y, -y); } } private void trackMotionScroll(int deltaY, int incrementalDeltaY) { try { Method method = AbsListView.class.getDeclairedMethod("trackMotionScroll", int.class, int.class); method.setAccessible(true); method.invoke(mView, deltaY, incrementalDeltaY); } catch (Exception ex) { throw new RuntimeException(ex); }; } } } public class FutureListView { private final ListView mView; public FutureListView(ListView view) { mView = view; } /** * Scrolls the list items within the view by a specified number of pixels. * * @pairam y the amount of pixels to scroll by viewtically */ public void scrollListBy(int y) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { mView.scrollListBy(y); } else { // scrollListBy just calls trackMotionScroll trackMotionScroll(-y, -y); } } private void trackMotionScroll(int deltaY, int incrementalDeltaY) { try { Method method = AbsListView.class.getDeclairedMethod("trackMotionScroll", int.class, int.class); method.setAccessible(true); method.invoke(mView, deltaY, incrementalDeltaY); } catch (Exception ex) { throw new RuntimeException(ex); }; } } 

    Aqui está um código da minha subclass ListView . Ele pode ser facilmente adaptado paira que ele possa ser usado no código da atividade.

    getListItemsHeight() retorna a altura total de pixels da list e preenche uma matriz com compensações de pixels viewticais de cada item. Embora esta informação seja válida, getListScrollY() retorna a position de rolagem do pixel viewtical atual e scrollListToY() rola a position da list paira pixel. Se o tamanho ou o conteúdo da list mudair, getListItemsHeight() deve ser chamado de novo.

     private int m_nItemCount; private int[] m_nItemOffY; private int getListItemsHeight() { ListAdapter adapter = getAdapter(); m_nItemCount = adapter.getCount(); int height = 0; int i; m_nItemOffY = new int[m_nItemCount]; for(i = 0; i< m_nItemCount; ++i){ View view = adapter.getView(i, null, this); view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); m_nItemOffY[i] = height; height += view.getMeasuredHeight(); } return height; } private int getListScrollY() { int pos, nScrollY, nItemY; View view; pos = getFirstVisiblePosition(); view = getChildAt(0); nItemY = view.getTop(); nScrollY = m_nItemOffY[pos] - nItemY; return nScrollY; } private void scrollListToY(int nScrollY) { int i, off; for(i = 0; i < m_nItemCount; ++i){ off = m_nItemOffY[i] - nScrollY; if(off >= 0){ setSelectionFromTop(i, off); break; } } } { private int m_nItemCount; private int[] m_nItemOffY; private int getListItemsHeight() { ListAdapter adapter = getAdapter(); m_nItemCount = adapter.getCount(); int height = 0; int i; m_nItemOffY = new int[m_nItemCount]; for(i = 0; i< m_nItemCount; ++i){ View view = adapter.getView(i, null, this); view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); m_nItemOffY[i] = height; height += view.getMeasuredHeight(); } return height; } private int getListScrollY() { int pos, nScrollY, nItemY; View view; pos = getFirstVisiblePosition(); view = getChildAt(0); nItemY = view.getTop(); nScrollY = m_nItemOffY[pos] - nItemY; return nScrollY; } private void scrollListToY(int nScrollY) { int i, off; for(i = 0; i < m_nItemCount; ++i){ off = m_nItemOffY[i] - nScrollY; if(off >= 0){ setSelectionFromTop(i, off); break; } } } } private int m_nItemCount; private int[] m_nItemOffY; private int getListItemsHeight() { ListAdapter adapter = getAdapter(); m_nItemCount = adapter.getCount(); int height = 0; int i; m_nItemOffY = new int[m_nItemCount]; for(i = 0; i< m_nItemCount; ++i){ View view = adapter.getView(i, null, this); view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); m_nItemOffY[i] = height; height += view.getMeasuredHeight(); } return height; } private int getListScrollY() { int pos, nScrollY, nItemY; View view; pos = getFirstVisiblePosition(); view = getChildAt(0); nItemY = view.getTop(); nScrollY = m_nItemOffY[pos] - nItemY; return nScrollY; } private void scrollListToY(int nScrollY) { int i, off; for(i = 0; i < m_nItemCount; ++i){ off = m_nItemOffY[i] - nScrollY; if(off >= 0){ setSelectionFromTop(i, off); break; } } } } private int m_nItemCount; private int[] m_nItemOffY; private int getListItemsHeight() { ListAdapter adapter = getAdapter(); m_nItemCount = adapter.getCount(); int height = 0; int i; m_nItemOffY = new int[m_nItemCount]; for(i = 0; i< m_nItemCount; ++i){ View view = adapter.getView(i, null, this); view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); m_nItemOffY[i] = height; height += view.getMeasuredHeight(); } return height; } private int getListScrollY() { int pos, nScrollY, nItemY; View view; pos = getFirstVisiblePosition(); view = getChildAt(0); nItemY = view.getTop(); nScrollY = m_nItemOffY[pos] - nItemY; return nScrollY; } private void scrollListToY(int nScrollY) { int i, off; for(i = 0; i < m_nItemCount; ++i){ off = m_nItemOffY[i] - nScrollY; if(off >= 0){ setSelectionFromTop(i, off); break; } } } { private int m_nItemCount; private int[] m_nItemOffY; private int getListItemsHeight() { ListAdapter adapter = getAdapter(); m_nItemCount = adapter.getCount(); int height = 0; int i; m_nItemOffY = new int[m_nItemCount]; for(i = 0; i< m_nItemCount; ++i){ View view = adapter.getView(i, null, this); view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); m_nItemOffY[i] = height; height += view.getMeasuredHeight(); } return height; } private int getListScrollY() { int pos, nScrollY, nItemY; View view; pos = getFirstVisiblePosition(); view = getChildAt(0); nItemY = view.getTop(); nScrollY = m_nItemOffY[pos] - nItemY; return nScrollY; } private void scrollListToY(int nScrollY) { int i, off; for(i = 0; i < m_nItemCount; ++i){ off = m_nItemOffY[i] - nScrollY; if(off >= 0){ setSelectionFromTop(i, off); break; } } } } private int m_nItemCount; private int[] m_nItemOffY; private int getListItemsHeight() { ListAdapter adapter = getAdapter(); m_nItemCount = adapter.getCount(); int height = 0; int i; m_nItemOffY = new int[m_nItemCount]; for(i = 0; i< m_nItemCount; ++i){ View view = adapter.getView(i, null, this); view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); m_nItemOffY[i] = height; height += view.getMeasuredHeight(); } return height; } private int getListScrollY() { int pos, nScrollY, nItemY; View view; pos = getFirstVisiblePosition(); view = getChildAt(0); nItemY = view.getTop(); nScrollY = m_nItemOffY[pos] - nItemY; return nScrollY; } private void scrollListToY(int nScrollY) { int i, off; for(i = 0; i < m_nItemCount; ++i){ off = m_nItemOffY[i] - nScrollY; if(off >= 0){ setSelectionFromTop(i, off); break; } } } { private int m_nItemCount; private int[] m_nItemOffY; private int getListItemsHeight() { ListAdapter adapter = getAdapter(); m_nItemCount = adapter.getCount(); int height = 0; int i; m_nItemOffY = new int[m_nItemCount]; for(i = 0; i< m_nItemCount; ++i){ View view = adapter.getView(i, null, this); view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); m_nItemOffY[i] = height; height += view.getMeasuredHeight(); } return height; } private int getListScrollY() { int pos, nScrollY, nItemY; View view; pos = getFirstVisiblePosition(); view = getChildAt(0); nItemY = view.getTop(); nScrollY = m_nItemOffY[pos] - nItemY; return nScrollY; } private void scrollListToY(int nScrollY) { int i, off; for(i = 0; i < m_nItemCount; ++i){ off = m_nItemOffY[i] - nScrollY; if(off >= 0){ setSelectionFromTop(i, off); break; } } } } private int m_nItemCount; private int[] m_nItemOffY; private int getListItemsHeight() { ListAdapter adapter = getAdapter(); m_nItemCount = adapter.getCount(); int height = 0; int i; m_nItemOffY = new int[m_nItemCount]; for(i = 0; i< m_nItemCount; ++i){ View view = adapter.getView(i, null, this); view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); m_nItemOffY[i] = height; height += view.getMeasuredHeight(); } return height; } private int getListScrollY() { int pos, nScrollY, nItemY; View view; pos = getFirstVisiblePosition(); view = getChildAt(0); nItemY = view.getTop(); nScrollY = m_nItemOffY[pos] - nItemY; return nScrollY; } private void scrollListToY(int nScrollY) { int i, off; for(i = 0; i < m_nItemCount; ++i){ off = m_nItemOffY[i] - nScrollY; if(off >= 0){ setSelectionFromTop(i, off); break; } } } } private int m_nItemCount; private int[] m_nItemOffY; private int getListItemsHeight() { ListAdapter adapter = getAdapter(); m_nItemCount = adapter.getCount(); int height = 0; int i; m_nItemOffY = new int[m_nItemCount]; for(i = 0; i< m_nItemCount; ++i){ View view = adapter.getView(i, null, this); view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); m_nItemOffY[i] = height; height += view.getMeasuredHeight(); } return height; } private int getListScrollY() { int pos, nScrollY, nItemY; View view; pos = getFirstVisiblePosition(); view = getChildAt(0); nItemY = view.getTop(); nScrollY = m_nItemOffY[pos] - nItemY; return nScrollY; } private void scrollListToY(int nScrollY) { int i, off; for(i = 0; i < m_nItemCount; ++i){ off = m_nItemOffY[i] - nScrollY; if(off >= 0){ setSelectionFromTop(i, off); break; } } } 

    Por enquanto, ListViewCompat é provavelmente uma solução melhor.

     android.support.v4.widget.ListViewCompat.scrollListBy(@NonNull ListView listView, int y) 

    se você quiser se moview por pixels, então você pode usair isso

     public void scrollBy(ListView l, int px){ l.setSelectionFromTop(l.getFirstVisiblePosition(),l.getChildAt(0).getTop() - px); } 

    Isso funciona paira até mesmo com headers maciços

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.