Android 向滚动视图动态添加内容
我的活动中有以下布局Android 向滚动视图动态添加内容,android,Android,我的活动中有以下布局 <HorizontalScrollView android:id="@+id/horizontalScrollView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/topHeading" android:layout_centerHor
<HorizontalScrollView
android:id="@+id/horizontalScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/topHeading"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal|center_vertical">
<LinearLayout
android:id="@+id/scrollViewlinearlayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal">
<!--
Content will be dynamically added to this portion.
-->
</LinearLayout>
</HorizontalScrollView>
在我的活动代码中,我将视图充气并添加到上面的布局中
LinearLayout scrollViewLinearLayout=(LinearLayout)root.findViewById(R.id.scrollViewlinearlayout);
for(int i=0;i<10;i++){
View view =inflater.inflate(R.layout.lesson_row,null,false);
Button b1=(Button) view.findViewById(R.id.button);
b1.setOnClickListener(this);
scrollViewLinearLayout.addView(view);
}
现在这个很好用。我的scrollview布局中有10个布局。现在,R.layout.lesson_行包含我要单击的按钮。我可以很容易地添加一个点击监听器到按钮,但10个布局中的哪个按钮被点击了 将ID或标记与正在创建的按钮实例相关联,以便区分它们
或者,将按钮实例放在HashMap或其他Java数据结构中,这样您就可以在运行时进行查找。或者只使用ListView……这是一种水平布局。使用LinearLayoutManager的新RecyclerView(方向:水平)如何