简体   繁体   English

我有两个水平滚动视图。 如何一次显示一个,并在单击按钮时在它们之间切换?

[英]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:在两个Horizo​​ntalScrollView中添加ID,例如: android:id="@+id/hScrollView1"android:id="@+id/hScrollView2"

Step 2: Put both HorizontalScrollView in a parent LinearLayout like below: 第2步:将两个Horizo​​ntalScrollView放在父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.

相关问题 如何在可见的两个视图之间切换? - How do I switch between two views being visible? 如何在水平滚动视图中更改图像按钮的宽度? - How can I change the width of an image button in a horizontal scroll view? 如果单击FloatingActionButton,如何显示其他视图? - How I can show other Views if I click on the FloatingActionButton? 我有两个输入文本框和一个按钮,我想单击按钮而不是将一个文本框包含的文本切换到另一文本框 - I have two input text box and one button, I want to click on button than switch one text box contain to other text box 我有三个开关按钮,如果一个开关按钮处于活动状态,则其他两个开关按钮应保持不活动或禁用 - I have three switch button, if one switch button is active then other two switch button should remain inactive or disable 如何在按钮单击时在同一个按钮的两种背景颜色之间切换 - how to switch between two background color of the same button on button click 在viewpager中查看所有视图后,如何显示按钮? - How can I show a button when all views are viewed in viewpager? 如何在点击按钮时切换播放器? - How do I switch players on button click? 我可以为一个按钮设置两种不同的 onclick 方法吗 - can I have two different onclick methods for one button 点屏时怎么同时出现多个imageview? (圈出一张图片) - How can I have more than one imageview on the screen at the same time when I click the screen? (Circle one imageview)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM