android - 如何在 Horizo​​ntalScrollView Android 中的特定位置上滚动

标签 android android-linearlayout horizontalscrollview

我在 Horizo​​ntalScrollView 内的 LinearLayout 中添加动态布局 View 。 布局xml如下:

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<HorizontalScrollView
    android:id="@+id/hsv_category_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:scrollbars="none">

    <LinearLayout
        android:id="@+id/ll_placeHolder"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:orientation="horizontal">

    </LinearLayout>

</HorizontalScrollView>

通过在线性布局中务实地添加 10 个 View ,如下所示

public class ScrollTest extends Activity {

  HorizontalScrollView hsv_category_list;
  LinearLayout ll_placeHolder;
  View layoutView[];
 @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scroll_test);
    hsv_category_list=
   (HorizontalScrollView)findViewById(R.id.hsv_category_list);
    ll_placeHolder = (LinearLayout) findViewById(R.id.ll_placeHolder);
    layoutView = new View[10];

    for (int i = 0; i < 10; i++) {
        layoutView[i] = 
        LayoutInflater.from(this).inflate(R.layout.category_list_item, 
        null);
        layoutView[i].setId(i);

        ll_placeHolder.addView(layoutView[i]);
    }

    }
 }

output

我想自动滚动到索引 7 上不可见的项目。 请帮我解决这个问题。谢谢

最佳答案

在为我工作之后,我做到了

 //index which your want to scroll
 int index=7;     
 layoutView[index].getParent().requestChildFocus(layoutView[index], layoutView[index]);

关于android - 如何在 Horizo​​ntalScrollView Android 中的特定位置上滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45103455/

相关文章:

android - 如何让 PixlUI for Android 自定义字体正常工作?

android - 来自 Android JNI 中任何线程的 FindClass

android - TextView 因 android 中父级的 LinearLayout wrap_content 而断线

java - 如何在 horizo​​ntalScrollView 中通过平滑滚动使 View 居中

java - 线程在进入下一个 Activity 之前保持更新一段时间,并且冲突已关闭

android - 隐藏自定义对话框时窗口泄漏

java - xml 布局的底部 View 未显示

android-layout - LinearLayout 中的重力 - android?

java - 将 ListView 放入 Horizo​​ntalScrollView 的最佳方法是什么?

android - 如何将水平 ScrollView 移动到特定位置?