如何实现像Gallery一样的Horizo​​ntalScrollView?

2023-11-24

I want to implement Horizontal ScrollView with some features of Gallery, enter image description here

在图库中,滚动条在一定距离处成对排列,即如果屏幕上显示三个图像,单击最后一个图像将排列在中心。

我将如何实施HorizontalSCrollView如上所述?


试试这个代码:

活动主文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="100dip"
tools:context=".MainActivity" >
<HorizontalScrollView
    android:id="@+id/hsv"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:fillViewport="true"
    android:measureAllChildren="false"
    android:scrollbars="none" >
    <LinearLayout
        android:id="@+id/innerLay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal" >
        <LinearLayout
            android:id="@+id/asthma_action_plan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="match_parent" >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/action_plan" />
                <TextView
                    android:layout_width="0.2dp"
                    android:layout_height="fill_parent"
                    android:layout_alignParentRight="true"
                    android:background="@drawable/ln" />
            </RelativeLayout>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/controlled_medication"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="match_parent" >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/controlled" />
                <TextView
                    android:layout_width="0.2dp"
                    android:layout_height="fill_parent"
                    android:layout_alignParentRight="true"
                    android:background="@drawable/ln" />
            </RelativeLayout>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/as_needed_medication"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal" >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/as_needed" />
                <TextView
                    android:layout_width="0.2dp"
                    android:layout_height="fill_parent"
                    android:layout_alignParentRight="true"
                    android:background="@drawable/ln" />
            </RelativeLayout>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/rescue_medication"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="match_parent" >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/rescue" />
                <TextView
                    android:layout_width="0.2dp"
                    android:layout_height="fill_parent"
                    android:layout_alignParentRight="true"
                    android:background="@drawable/ln" />
            </RelativeLayout>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/your_symptoms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="match_parent" >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/symptoms" />
                <TextView
                    android:layout_width="0.2dp"
                    android:layout_height="fill_parent"
                    android:layout_alignParentRight="true"
                    android:background="@drawable/ln" />
            </RelativeLayout>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/your_triggers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="match_parent" >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/triggers" />
                <TextView
                    android:layout_width="0.2dp"
                    android:layout_height="fill_parent"
                    android:layout_alignParentRight="true"
                    android:background="@drawable/ln" />
            </RelativeLayout>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/wheeze_rate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="match_parent" >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/wheeze_rate" />
                <TextView
                    android:layout_width="0.2dp"
                    android:layout_height="fill_parent"
                    android:layout_alignParentRight="true"
                    android:background="@drawable/ln" />
            </RelativeLayout>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/peak_flow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical" >
            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="match_parent" >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/peak_flow" />
                <TextView
                    android:layout_width="0.2dp"
                    android:layout_height="fill_parent"
                    android:layout_alignParentRight="true"
                    android:background="@drawable/ln" />
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>
</HorizontalScrollView>
<TextView
    android:layout_width="fill_parent"
    android:layout_height="0.2dp"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/hsv"
    android:background="@drawable/ln" />
<LinearLayout
    android:id="@+id/prev"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:paddingLeft="5dip"
    android:paddingRight="5dip"
    android:descendantFocusability="blocksDescendants" >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:src="@drawable/prev_arrow" />
</LinearLayout>
<LinearLayout
    android:id="@+id/next"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:paddingLeft="5dip"
    android:paddingRight="5dip"
    android:descendantFocusability="blocksDescendants" >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:src="@drawable/next_arrow" />
</LinearLayout>
</RelativeLayout>

grid_item.xml

<?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" >
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:src="@drawable/ic_launcher" />
</LinearLayout>

MainActivity.java

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity {

LinearLayout asthmaActionPlan, controlledMedication, asNeededMedication,
        rescueMedication, yourSymtoms, yourTriggers, wheezeRate, peakFlow;
LayoutParams params;
LinearLayout next, prev;
int viewWidth;
GestureDetector gestureDetector = null;
HorizontalScrollView horizontalScrollView;
ArrayList<LinearLayout> layouts;
int parentLeft, parentRight;
int mWidth;
int currPosition, prevPosition;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    prev = (LinearLayout) findViewById(R.id.prev);
    next = (LinearLayout) findViewById(R.id.next);
    horizontalScrollView = (HorizontalScrollView) findViewById(R.id.hsv);
    gestureDetector = new GestureDetector(new MyGestureDetector());
    asthmaActionPlan = (LinearLayout) findViewById(R.id.asthma_action_plan);
    controlledMedication = (LinearLayout) findViewById(R.id.controlled_medication);
    asNeededMedication = (LinearLayout) findViewById(R.id.as_needed_medication);
    rescueMedication = (LinearLayout) findViewById(R.id.rescue_medication);
    yourSymtoms = (LinearLayout) findViewById(R.id.your_symptoms);
    yourTriggers = (LinearLayout) findViewById(R.id.your_triggers);
    wheezeRate = (LinearLayout) findViewById(R.id.wheeze_rate);
    peakFlow = (LinearLayout) findViewById(R.id.peak_flow);

    Display display = getWindowManager().getDefaultDisplay();
    mWidth = display.getWidth(); // deprecated
    viewWidth = mWidth / 3;
    layouts = new ArrayList<LinearLayout>();
    params = new LayoutParams(viewWidth, LayoutParams.WRAP_CONTENT);

    asthmaActionPlan.setLayoutParams(params);
    controlledMedication.setLayoutParams(params);
    asNeededMedication.setLayoutParams(params);
    rescueMedication.setLayoutParams(params);
    yourSymtoms.setLayoutParams(params);
    yourTriggers.setLayoutParams(params);
    wheezeRate.setLayoutParams(params);
    peakFlow.setLayoutParams(params);

    layouts.add(asthmaActionPlan);
    layouts.add(controlledMedication);
    layouts.add(asNeededMedication);
    layouts.add(rescueMedication);
    layouts.add(yourSymtoms);
    layouts.add(yourTriggers);
    layouts.add(wheezeRate);
    layouts.add(peakFlow);

    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    horizontalScrollView.smoothScrollTo(
                            (int) horizontalScrollView.getScrollX()
                                    + viewWidth,
                            (int) horizontalScrollView.getScrollY());
                }
            }, 100L);
        }
    });

    prev.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new Handler().postDelayed(new Runnable() {
                public void run() {
                    horizontalScrollView.smoothScrollTo(
                            (int) horizontalScrollView.getScrollX()
                                    - viewWidth,
                            (int) horizontalScrollView.getScrollY());
                }
            }, 100L);
        }
    });

    horizontalScrollView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    });
}

class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        if (e1.getX() < e2.getX()) {
            currPosition = getVisibleViews("left");
        } else {
            currPosition = getVisibleViews("right");
        }

        horizontalScrollView.smoothScrollTo(layouts.get(currPosition)
                .getLeft(), 0);
        return true;
    }
}

public int getVisibleViews(String direction) {
    Rect hitRect = new Rect();
    int position = 0;
    int rightCounter = 0;
    for (int i = 0; i < layouts.size(); i++) {
        if (layouts.get(i).getLocalVisibleRect(hitRect)) {
            if (direction.equals("left")) {
                position = i;
                break;
            } else if (direction.equals("right")) {
                rightCounter++;
                position = i;
                if (rightCounter == 2)
                    break;
            }
        }
    }
    return position;
}
}

如有任何问题请告诉我 享受...

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何实现像Gallery一样的Horizo​​ntalScrollView? 的相关文章

随机推荐

  • 对称整数到整数加密

    我需要一些关于如何将一个 int 加密为另一个 int 的实际示例 并且需要一个密钥来解密该值 就像是 encrypt 1 secret key 67123571122 decrypt 67123571122 secret key 1 这家
  • 如何在 Asp.Net MVC 中动态插入部分视图

    我正在将 Webforms 站点迁移到 MVC 在我的网络表单网站中 我的页面利用了用户控件的各种组合 然后是 html 块 然后是标签 文本框等 我不想对每个页面进行硬连线 因此我将从 CMS 驱动每个页面的输出 该 CMS 指定将控件插
  • bash Heredoc 可以将其结果直接放入变量中吗?

    我有一些这样的代码 CMD cat lt
  • 我的领域路径定义的 #if TARGET_OS_SIMULATOR 代码有什么问题?

    我有这个代码 if TARGET OS SIMULATOR let device false let RealmDB try Realm path Users Admin Desktop realm Realm realm else let
  • 检索 CSS 是否需要“getPropertyValue”方法?

    你能告诉我为什么我们需要使用getPropertyValue方法 如果我们只能使用getComputedStyle one 例如 据我了解 这将起作用 var s getComputedStyle element null opacity
  • 错误! “sudo”不是 Play 的有效属性

    我有一个 ansible 播放文件 它必须执行两个任务 第一个任务是在本地计算机上获取磁盘使用情况 另一个任务是获取远程计算机的磁盘使用情况并在远程计算机中安装 apache2 当我尝试运行该文件时出现错误 错误 sudo 不是 Play
  • 带有 json 正文的 Swagger POST

    我正在尝试使用 swagger 编写服务器响应的静态 json 文件 我被帖子正文困住了 不知道如何描述它 它看起来与 Grooveshark api 非常相似 其中有一个页面和不同的帖子参数 因此 给出grooveshark的例子 htt
  • 对 CollectionViewSource 感到困惑(SelectedItem 无法在组合中工作)

    我有一堆组合 它们都共享相同的可用选项 这些选择在我的 ViewModel 公开的集合中提供 一切都很好 花花公子 我现在想要对这些选择进行排序 所以我决定公开一个ICollectionView来自我的 ViewModel 而不是我平常的R
  • 将 plupload 与 MVC3 结合使用

    因此 我在 MVC3 中使用 flash 运行时实现了 plupload 它工作完美 因为它使用更正操作上传并运行全部内容 但是 我真的很希望能够控制响应 并在 plupload 中处理它 但我似乎无法得到任何响应 我尝试过覆盖 fileU
  • ambari hadoop 安装期间权限被拒绝(publickey、gssapi-keyex、gssapi-with-mic、密码)

    我正在尝试使用 ambari 部署 hadoop 集群 但是当我选择具有 FQDN 的主机名并继续配置时 我收到 ssh 的权限被拒绝错误 脚步 1 使用 ssh keygen 作为 root 生成 rsa 密钥 更改了 ssh 700 和
  • 我什么时候会使用 AppDomain?

    我对反射相当陌生 我想知道我会使用 第二个 AppDomain 做什么 在商业应用中会有什么实际应用 有很多用途 辅助 AppDomain 可以提供一定程度的隔离 类似于操作系统提供的进程隔离 我使用它的一个实际用途是动态加载 插件 DLL
  • 如何在android中将url加载到webview时显示进度?

    我正在将 url 加载到 webview 中 WebView webview WebView findViewById R id webview webview loadUrl url 加载网址需要一些时间 在此期间显示空白屏幕 我想在加载
  • 递归查询挑战 - 简单的父/子示例

    注意 在 postgresql 上的 RhodiumToad 的帮助下 我找到了一个解决方案 我将其作为答案发布 如果有人可以对此进行改进 请加入 我还没能适应之前的递归查询解决方案到以下有向无环图 其中包括多个 根 无祖先 节点 我正在尝
  • System.Diagnostics.Process.Start 针对不同域的进程

    我们有一个场景 我们需要用户能够启动 SQLServer 并使用与当前登录的域不同的域进行身份验证 因此 为了澄清其设置方式 用户到达办公室并登录公司域 为简单起见 我们将其称为 LOCALDOMAIN 他们希望连接到不同域上的远程数据库
  • TWTweetComposeViewController 中的错误?

    我正在使用 TWTweetComposeViewController 如果可用 从我的 iOS 应用程序内部发送推文 我用样板文本预先填充视图控制器 然后让用户自行修改和发送 它在大多数情况下都非常有效 蒸馏下来 它看起来像这样 与body
  • 测试|无法在resetFakeAsyncZone处读取未定义的属性“assertPresent”

    我的 karma v1 4 有问题 测试框架 我所有的单元测试现在都因错误而失败Cannot read property assertPresent of undefined at resetFakeAsyncZone 我已经搜索过解决方案
  • 让 eclipse 调试我的 android 项目

    我正在使用 Eclipse 编写 Android 应用程序 当我单击左侧树视图中的项目 然后单击 调试 时 IDE 会构建一个 APK 并按预期安装在模拟器 设备中 但是 如果我正在编辑文件并且忘记在尝试调试之前首先单击该项目 那么 IDE
  • 如何在网格视图的行命令中找到该控件?

    我怎么能够在 row 命令中找到控件网格视图 实际上 GridViewCommandEventArgs 中没有 Row 因此您需要从命令源命名容器中获取行 GridViewRow row GridViewRow Control e Comm
  • .NET 中的 Java 小程序相当于什么?

    NET 中的 Java 小程序相当于什么 是银光吗 Java applet 还在广泛使用吗 1997 年 当 Java 1 0 发布时 Java applet 是 新的热门事物 几年后 它们变得越来越不受欢迎 主要是因为在计算机上安装Jav
  • 如何实现像Gallery一样的Horizo​​ntalScrollView?

    I want to implement Horizontal ScrollView with some features of Gallery 在图库中 滚动条在一定距离处成对排列 即如果屏幕上显示三个图像 单击最后一个图像将排列在中心 我