[英]I have two horizontal scroll views. How can I show one at a time and switch between them on button click?
On my android app I have two horizontal scroll views that act as docks. 在我的android应用中,我有两个水平滚动视图充当停靠栏。 How can I make it so that if button 1 is clicked dock 1 displays and if button 2 is clicked dock 2 displays?
我如何才能做到,如果单击了按钮1,则显示停靠点1,如果单击了按钮2,则显示停靠点2? Since they're docks I want them to both display in the same position.
由于它们是基座,我希望它们都显示在同一位置。
here is my code: 这是我的代码:
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5" />
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5" />
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dockButton1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dockButton2" />
Button code: 按钮代码:
dock1button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
dock2button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
Step 1: add id in both of the HorizontalScrollView for example: android:id="@+id/hScrollView1"
and android:id="@+id/hScrollView2"
步骤1:在两个HorizontalScrollView中添加ID,例如:
android:id="@+id/hScrollView1"
和android:id="@+id/hScrollView2"
Step 2: Put both HorizontalScrollView in a parent LinearLayout like below: 第2步:将两个HorizontalScrollView放在父LinearLayout中,如下所示:
<LinearLayout>
<HorizontalScrollView
android:id="@+id/hScrollView1">
.
.
</HorizontalScrollView>
<HorizontalScrollView
android:id="@+id/hScrollView2">
.
.
</HorizontalScrollView>
</LinearLayout>
Step 3: In your button click just toggle the visibility: in onClick just use.. 第3步:在您的按钮中单击只需切换可见性:在onClick中只需使用。
hScrlView1.setVisibility(View.GONE);
hScrlView2.setVisibility(View.VISIBLE);
and 和
hScrlView1.setVisibility(View.VISIBLE);
hScrlView2.setVisibility(View.GONE);
where hScrlView1 and hScrlView2 are the ScrollView you want to show. 其中hScrlView1和hScrlView2是要显示的ScrollView。
put the two horizontal listviews in a relative layout and set the visibility of one of them to invisible. 将两个水平listview放在相对布局中,并将其中一个的可见性设置为invisible。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5" />
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:visibility="invisible" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dockButton1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dockButton2" />
then change the visibility in buttons' codes: 然后更改按钮代码中的可见性:
Button code: 按钮代码:
dock1button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
horizontalList2.setVisibilty(View.INVISIBLE);
horizontalList1.setVisibilty(View.VISIBLE);
}
});
dock2button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
horizontalList1.setVisibilty(View.INVISIBLE);
horizontalList2.setVisibilty(View.VISIBLE);
}
});
You'd be better off by just using FrameLayout
containing both scrollviews. 只使用同时包含两个滚动视图的
FrameLayout
会更好。 It would be more performant than using LinearLayout
or even worse RelativeLayout
. 它比使用
LinearLayout
或更糟糕的RelativeLayout
具有更高的性能。
Just do something like this (schematic code, some xml attrs omitted) 只需执行以下操作(示意图代码,省略一些xml attrs)
<FrameLayout>
<HorizontalScrollView
android:id="@+id/view1"/>
<HorizontalScrollView
android:id="@+id/view2"
android:visibility="gone"
/>
</FrameLayout>
And then in your onClick handler(s) do something like 然后在您的onClick处理程序中执行以下操作
view1.setVisibility(View.VISIBLE)
view2.setVisibility(View.GONE)
or vice versa (depending on button pressed) 反之亦然(取决于按下的按钮)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.