如何实现Horizo​​ntalScrollView像画廊? [英] How to implement HorizontalScrollView like Gallery?

查看:155
本文介绍了如何实现Horizo​​ntalScrollView像画廊?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实施水平滚动型与画廊的一些特性,

在画廊每隔一段距离就安排对,即发。如果我​​们在屏幕上显示三幅图像滚动,单击最后一个图像将安排在中锋位置。

我将如何落实 Horizo​​ntalSCrollView 中提到?

解决方案

试试这个code:

activity_main.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =FILL_PARENT
机器人:layout_height =100dip
工具:上下文=MainActivity。>
< Horizo​​ntalScrollView
    机器人:ID =@ + ID / HSV
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentTop =真
    机器人:fillViewport =真
    机器人:measureAllChildren =假
    机器人:滚动条=无>
    <的LinearLayout
        机器人:ID =@ + ID / innerLay
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:重力=center_vertical
        机器人:方向=横向>
        <的LinearLayout
            机器人:ID =@ + ID / asthma_action_plan
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=中心
            机器人:方向=垂直>
            < RelativeLayout的
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =match_parent>
                < ImageView的
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:SRC =@可绘制/ action_plan/>
                <的TextView
                    机器人:layout_width =0.2dp
                    机器人:layout_height =FILL_PARENT
                    机器人:layout_alignParentRight =真
                    机器人:背景=@可绘制/ LN/>
            < / RelativeLayout的>
        < / LinearLayout中>
        <的LinearLayout
            机器人:ID =@ + ID / controlled_medication
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=中心
            机器人:方向=垂直>
            < RelativeLayout的
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =match_parent>
                < ImageView的
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:SRC =@可绘制/控制/>
                <的TextView
                    机器人:layout_width =0.2dp
                    机器人:layout_height =FILL_PARENT
                    机器人:layout_alignParentRight =真
                    机器人:背景=@可绘制/ LN/>
            < / RelativeLayout的>
        < / LinearLayout中>
        <的LinearLayout
            机器人:ID =@ + ID / as_needed_medication
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=中心
            机器人:方向=垂直>
            < RelativeLayout的
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =match_parent
                机器人:方向=横向>
                < ImageView的
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:SRC =@可绘制/ as_needed/>
                <的TextView
                    机器人:layout_width =0.2dp
                    机器人:layout_height =FILL_PARENT
                    机器人:layout_alignParentRight =真
                    机器人:背景=@可绘制/ LN/>
            < / RelativeLayout的>
        < / LinearLayout中>
        <的LinearLayout
            机器人:ID =@ + ID / rescue_medication
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=中心
            机器人:方向=垂直>
            < RelativeLayout的
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =match_parent>
                < ImageView的
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:SRC =@可绘制/拯救/>
                <的TextView
                    机器人:layout_width =0.2dp
                    机器人:layout_height =FILL_PARENT
                    机器人:layout_alignParentRight =真
                    机器人:背景=@可绘制/ LN/>
            < / RelativeLayout的>
        < / LinearLayout中>
        <的LinearLayout
            机器人:ID =@ + ID / your_symptoms
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=中心
            机器人:方向=垂直>
            < RelativeLayout的
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =match_parent>
                < ImageView的
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:SRC =@可绘制/症状/>
                <的TextView
                    机器人:layout_width =0.2dp
                    机器人:layout_height =FILL_PARENT
                    机器人:layout_alignParentRight =真
                    机器人:背景=@可绘制/ LN/>
            < / RelativeLayout的>
        < / LinearLayout中>
        <的LinearLayout
            机器人:ID =@ + ID / your_triggers
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=中心
            机器人:方向=垂直>
            < RelativeLayout的
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =match_parent>
                < ImageView的
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:SRC =@可绘制/触发器/>
                <的TextView
                    机器人:layout_width =0.2dp
                    机器人:layout_height =FILL_PARENT
                    机器人:layout_alignParentRight =真
                    机器人:背景=@可绘制/ LN/>
            < / RelativeLayout的>
        < / LinearLayout中>
        <的LinearLayout
            机器人:ID =@ + ID / wheeze_rate
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=中心
            机器人:方向=垂直>
            < RelativeLayout的
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =match_parent>
                < ImageView的
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:SRC =@可绘制/ wheeze_rate/>
                <的TextView
                    机器人:layout_width =0.2dp
                    机器人:layout_height =FILL_PARENT
                    机器人:layout_alignParentRight =真
                    机器人:背景=@可绘制/ LN/>
            < / RelativeLayout的>
        < / LinearLayout中>
        <的LinearLayout
            机器人:ID =@ + ID / peak_flow
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:重力=中心
            机器人:方向=垂直>
            < RelativeLayout的
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =match_parent>
                < ImageView的
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:SRC =@可绘制/ peak_flow/>
                <的TextView
                    机器人:layout_width =0.2dp
                    机器人:layout_height =FILL_PARENT
                    机器人:layout_alignParentRight =真
                    机器人:背景=@可绘制/ LN/>
            < / RelativeLayout的>
        < / LinearLayout中>
    < / LinearLayout中>
< / Horizo​​ntalScrollView>
<的TextView
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =0.2dp
    机器人:layout_alignParentRight =真
    机器人:layout_below =@ + ID / HSV
    机器人:背景=@可绘制/ LN/>
<的LinearLayout
    机器人:ID =@ + ID / $ P $光伏
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =FILL_PARENT
    机器人:layout_alignParentLeft =真
    机器人:layout_centerVertical =真
    机器人:以下属性来=5dip
    机器人:paddingRight =5dip
    机器人:descendantFocusability =blocksDescendants>
    < ImageView的
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =center_vertical
        机器人:SRC =@可绘制/ prev_arrow/>
< / LinearLayout中>
<的LinearLayout
    机器人:ID =@ + ID /下一个
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =FILL_PARENT
    机器人:layout_alignParentRight =真
    机器人:layout_centerVertical =真
    机器人:以下属性来=5dip
    机器人:paddingRight =5dip
    机器人:descendantFocusability =blocksDescendants>
    < ImageView的
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =center_vertical
        机器人:SRC =@可绘制/ next_arrow/>
< / LinearLayout中>
< / RelativeLayout的>
 

grid_item.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
< ImageView的
    机器人:ID =@ + ID / imageView1
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =100dp
    机器人:SRC =@可绘制/ ic_launcher/>
< / LinearLayout中>
 

MainActivity.java

 进口的java.util.ArrayList;

进口android.app.Activity;
进口android.graphics.Rect;
进口android.os.Bundle;
进口android.os.Handler;
进口android.view.Display;
进口android.view.GestureDetector;
进口android.view.GestureDetector.SimpleOnGestureListener;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.View.OnTouchListener;
进口android.widget.Horizo​​ntalScrollView;
进口android.widget.LinearLayout;
进口android.widget.LinearLayout.LayoutParams;

公共类MainActivity延伸活动{

的LinearLayout asthmaActionPlan,controlledMedication,asNeededMedication,
        rescueMedication,yourSymtoms,yourTriggers,wheezeRate,的Peakflow;
的LayoutParams PARAMS;
接下来的LinearLayout,preV;
INT viewWidth;
GestureDetector gestureDetector = NULL;
Horizo​​ntalScrollView horizo​​ntalScrollView;
ArrayList的<的LinearLayout>布局;
INT parentLeft,parentRight;
INT mWidth;
INT currPosition,prevPosition;
@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    preV =(的LinearLayout)findViewById(R.id. preV);
    接下来的=(的LinearLayout)findViewById(R.id.next);
    horizo​​ntalScrollView =(Horizo​​ntalScrollView)findViewById(R.id.hsv);
    gestureDetector =新GestureDetector(新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);
    显示显示= getWindowManager()getDefaultDisplay()。
    mWidth = display.getWidth(); //德precated
    viewWidth = mWidth / 3;
    布局=新的ArrayList<的LinearLayout>();
    PARAMS =新的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(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            新的处理程序()。postDelayed(新的Runnable(){
                公共无效的run(){
                    horizo​​ntalScrollView.smoothScrollTo(
                            (int)的horizo​​ntalScrollView.getScrollX()
                                    + viewWidth,
                            (int)的horizo​​ntalScrollView.getScrollY());
                }
            },100L);
        }
    });
    prev.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            新的处理程序()。postDelayed(新的Runnable(){
                公共无效的run(){
                    horizo​​ntalScrollView.smoothScrollTo(
                            (int)的horizo​​ntalScrollView.getScrollX()
                                     -  viewWidth,
                            (int)的horizo​​ntalScrollView.getScrollY());
                }
            },100L);
        }
    });
    horizo​​ntalScrollView.setOnTouchListener(新OnTouchListener(){
        @覆盖
        公共布尔onTouch(视图V,MotionEvent事件){
            如果(gestureDetector.onTouchEvent(事件)){
                返回true;
            }
            返回false;
        }
    });
}
类MyGestureDetector扩展SimpleOnGestureListener {
    @覆盖
    公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,
            浮动velocityY){
        如果(e1.getX()&其中; e2.getX()){
            currPosition = getVisibleViews(左);
        } 其他 {
            currPosition = getVisibleViews(正确);
        }

        horizo​​ntalScrollView.smoothScrollTo(layouts.get(currPosition)
                .getLeft(),0);
        返回true;
    }
}
公众诠释getVisibleViews(字符串方向){
    矩形hitRect =新的矩形();
    INT位置= 0;
    INT rightCounter = 0;
    的for(int i = 0; I< layouts.size();我++){
        如果(layouts.get(ⅰ).getLocalVisibleRect(hitRect)){
            如果(direction.equals(左)){
                位置= I;
                打破;
            }否则,如果(direction.equals(正确)){
                rightCounter ++;
                位置= I;
                如果(rightCounter == 2)
                    打破;
            }
        }
    }
    返回的位置;
}
}
 

让我知道如果任何问题 享受...

I want to implement Horizontal ScrollView with some features of Gallery,

In Gallery the scroll made at some distance it arrange in pair, i.e If we have three images shown in screen, clicking last image will arrange at center.

How would I implement HorizontalSCrollView as mentioned?

解决方案

try this code:

activity_main.xml

<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;
}
}

Let me know if any issue enjoy...

这篇关于如何实现Horizo​​ntalScrollView像画廊?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆