与Android的HoritzontalScrollView创建画廊 [英] Create Gallery with HoritzontalScrollView Android

查看:146
本文介绍了与Android的HoritzontalScrollView创建画廊的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做ImageViews,但与HortizontalScrollView一个画廊,因为画廊是pcated德$ P $,它不能很好地工作。但我不找像幻灯片库中的图像的形式。我已经这样做了code:
- xml文件:

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=@色/的backgroundColor
机器人:ID =@ + ID /一般
工具:上下文=com.mappleapps.tm2ibz.Activity.PoblacionsActivity>< Horizo​​ntalScrollView
    机器人:layout_width =match_parent
    机器人:layout_height =198dp
    机器人:ID =@ + ID / layGalMap>
    <的LinearLayout
        机器人:ID =@ + ID / mygallery
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=横向
        />
< / Horizo​​ntalScrollView>

活动文件:

  hsv.setOnTouchListener(新View.OnTouchListener(){
        @覆盖
        公共布尔onTouch(视图V,MotionEvent事件){
            如果(event.getAction()== MotionEvent.ACTION_UP)
            {
                INT的i1 = 0;
                INT I2 = imageView.getWidth();
                INT I3 = I2 * 2;
                INT I4 = I2 * 3;
                INT I5 = I2 * 4;                浮currentPosition = hsv.getScrollX();
                浮pagesCount = hsv.getChildCount();
                浮pageLengthInPx = imageView.getMeasuredWidth()/ pagesCount;
                当前是浮动= currentPosition / pageLengthInPx;                布尔isBehindHalfScreen = currentPage-(INT)当前页> 0.5;                如果(isBehindHalfScreen){
                    如果(续== 1){
                        hsv.scrollTo(I2,0);
                        续= 2;
                        康特= 1;
                    }否则如果(续== 2){
                        hsv.scrollTo(I3,0);
                        续= 3;
                        康特= 2;
                    }否则如果(续== 3){
                        hsv.scrollTo(I4,0);
                        续= 4;
                        康特= 3;
                    }否则如果(续== 4){
                        hsv.scrollTo(I5,0);
                        续= 4;
                        康特= 4;
                    }
                }其他{
                    如果(康特== 4){
                        hsv.scrollTo(I4,0);
                        续= 4;
                        康特= 3;
                    }否则如果(康特== 3){
                        hsv.scrollTo(I3,0);
                        续= 3;
                        康特= 2;
                    }否则如果(康特== 2){
                        hsv.scrollTo(I2,0);
                        续= 2;
                        康特= 1;
                    }否则如果(康特== 1){
                        hsv.scrollTo(I1,0);
                        续= 1;
                        康特= 1;
                    }
                }
            }            返回false;
        }
    });


解决方案

从<一个href=\"https://krishnalalstha.word$p$pss.com/2012/12/09/android-horizontalscrollview-with-center-lock/\"相对=nofollow>含中央LOCK ANDROID HORIZONTALSCROLLVIEW,


  

1)建立扩大Horizo​​ntalScrollView定制Java类和
  定义哪些居中当前所选项目的功能。某物
  是这样的。

 公共类CenterLockHorizo​​ntalScrollview扩展Horizo​​ntalScrollView {
    上下文语境;
    INT prevIndex = 0;    公共CenterLockHorizo​​ntalScrollview(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        this.context =背景;
        this.setSmoothScrollingEnabled(真);    }    公共无效setAdapter(上下文的背景下,CustomListAdapter mAdapter){        尝试{
            fillViewWithAdapter(mAdapter);
        }赶上(ZeroChildException E){            e.printStackTrace();
        }
    }    私人无效fillViewWithAdapter(CustomListAdapter mAdapter)
            抛出ZeroChildException {
        如果(getChildCount()== 0){
            抛出新ZeroChildException(
                    CenterLockHorizo​​ntalScrollView必须有一个孩子);
        }
        如果(getChildCount()== 0 || mAdapter == NULL)
            返回;        的ViewGroup父=(的ViewGroup)getChildAt(0);        parent.removeAllViews();        的for(int i = 0; I&LT; mAdapter.getCount();我++){
            parent.addView(mAdapter.getView(I,空,父母));
        }
    }    公共无效setCenter(INT指数){        的ViewGroup父=(的ViewGroup)getChildAt(0);        查看preVIEW = parent.getChildAt(prevIndex);
        preView.setBackgroundColor(Color.parseColor(#64CBD8));
        android.widget.LinearLayout.LayoutParams LP =新LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(5,5,5,5);
        preView.setLayoutParams(LP);        查看查看= parent.getChildAt(指数);
        view.setBackgroundColor(Color.RED);        INT屏幕宽度=((活动)上下文).getWindowManager()
                。.getDefaultDisplay()的getWidth();        INT scrollX =(view.getLeft() - (屏幕宽度/ 2))
                +(view.getWidth()/ 2);
        this.smoothScrollTo(scrollX,0);
        prevIndex =指数;
    }}


  
  

2)创建资源xml文件/布局

 &LT;的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent&GT;    &LT; com.krish.horizo​​ntalscrollview.CenterLockHorizo​​ntalScrollview
        机器人:ID =@ + ID /滚动视图
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentBottom =真正的&GT;        &LT;的LinearLayout
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:方向=横向&GT;
        &LT; / LinearLayout中&GT;
    &LT; /com.krish.horizo​​ntalscrollview.CenterLockHorizo​​ntalScrollview>    &LT;的LinearLayout
        机器人:ID =@ + ID / bottomLayout
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ ID /滚动视图
        机器人:方向=横向
        机器人:weightSum =2&GT;        &LT;按钮
            机器人:ID =@ + ID / BTN preV
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1
            机器人:文字=preV/&GT;        &LT;按钮
            机器人:ID =@ + ID / btnNext
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =1
            机器人:文字=下一步/&GT;
    &LT; / LinearLayout中&GT;    &LT;的TextView
        机器人:ID =@ + ID /文
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_above =@ ID / bottomLayout
        机器人:layout_alignParentTop =真
        机器人:layout_centerInParent =真
        机器人:文字颜色=#FF0000
        机器人:填充=@扪/ padding_medium/&GT;&LT; / RelativeLayout的&GT;


  
  

3)使用CenterLockHorizo​​ntalScrollview您的活动如下:

 公共类Horizo​​ntalScrollViewActivity延伸活动{
    CenterLockHorizo​​ntalScrollview centerLockHorizo​​ntalScrollview;
    CustomListAdapter customListAdapter;
    按钮BTN preV,btnNext;
    INT currIndex = 0;
    私人TextView的文本;
    ArrayList的&LT;串GT;名单=新的ArrayList&LT;串GT;(){        {
            加(曼城);
            加(曼联);
            加(切尔西);
            加(利物浦);
            加(热刺);
            加(埃弗顿);
            加(西汉姆);
            加(阿森纳);
            加(西扫帚);
            加(新城堡);
            加(Norich城);
            加(斯旺西);
            加(中风之城);        }
    };    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_horizo​​ntal_scroll_view);
        btnNext =(按钮)findViewById(R.id.btnNext);
        BTN preV =(按钮)findViewById(R.id.btn preV);
        文字=(TextView的)findViewById(R.id.text);
        centerLockHorizo​​ntalScrollview =(CenterLockHorizo​​ntalScrollview)findViewById(R.id.scrollView);
        customListAdapter =新CustomListAdapter(这一点,
                R.layout.news_list_item,清​​单);
        centerLockHorizo​​ntalScrollview.setAdapter(这一点,customListAdapter);
        btnNext.setOnClickListener(onClickListener);
        BTN prev.setOnClickListener(onClickListener);    }    OnClickListener onClickListener =新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            如果(v.getId()== R.id.btn preV){
                如果(currIndex!= 0){
                    currIndex--;
                    centerLockHorizo​​ntalScrollview.setCenter(currIndex);
                    text.setText(list.get(currIndex == 0?0:currIndex-1));
                }
            }否则如果(v.getId()== R.id.btnNext){                如果(currIndex&下;则为list.size()){
                    centerLockHorizo​​ntalScrollview.setCenter(currIndex);
                    currIndex ++;
                    text.setText(list.get(currIndex-1));
                }
            }        }
    };}


I'm trying to do a gallery of ImageViews but with HortizontalScrollView, because gallery is deprecated and it does not work well. But I don't find the form for slide the images like Gallery. I have done this code: - xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundColor"
android:id="@+id/general"
tools:context="com.mappleapps.tm2ibz.Activity.PoblacionsActivity">

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="198dp"
    android:id="@+id/layGalMap">
    <LinearLayout
        android:id="@+id/mygallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        />
</HorizontalScrollView>

Activity file:

hsv.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP)
            {
                int i1 = 0;
                int i2 = imageView.getWidth();
                int i3 = i2*2;
                int i4 = i2*3;
                int i5 = i2*4;

                float currentPosition = hsv.getScrollX();
                float pagesCount = hsv.getChildCount();
                float pageLengthInPx = imageView.getMeasuredWidth()/pagesCount;
                float currentPage = currentPosition/pageLengthInPx;

                Boolean isBehindHalfScreen =  currentPage-(int)currentPage > 0.5;

                if(isBehindHalfScreen){
                    if(contD == 1){
                        hsv.scrollTo(i2, 0);
                        contD = 2;
                        contE = 1;
                    }else if(contD == 2){
                        hsv.scrollTo(i3, 0);
                        contD = 3;
                        contE = 2;
                    }else if(contD == 3){
                        hsv.scrollTo(i4, 0);
                        contD = 4;
                        contE = 3;
                    }else if(contD == 4){
                        hsv.scrollTo(i5, 0);
                        contD = 4;
                        contE = 4;
                    }
                }else{
                    if(contE == 4){
                        hsv.scrollTo(i4, 0);
                        contD = 4;
                        contE = 3;
                    }else if(contE == 3){
                        hsv.scrollTo(i3, 0);
                        contD = 3;
                        contE = 2;
                    }else if(contE == 2){
                        hsv.scrollTo(i2, 0);
                        contD = 2;
                        contE = 1;
                    }else if(contE == 1){
                        hsv.scrollTo(i1, 0);
                        contD = 1;
                        contE = 1;
                    }
                }
            }

            return false;
        }
    });

解决方案

From ANDROID HORIZONTALSCROLLVIEW WITH CENTER LOCK,

1) create a custom java class extending HorizontalScrollView and define the function which center the current selected item. Something like this.

public class CenterLockHorizontalScrollview extends HorizontalScrollView {
    Context context;
    int prevIndex = 0;

    public CenterLockHorizontalScrollview(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.setSmoothScrollingEnabled(true);

    }

    public void setAdapter(Context context, CustomListAdapter mAdapter) {

        try {
            fillViewWithAdapter(mAdapter);
        } catch (ZeroChildException e) {

            e.printStackTrace();
        }
    }

    private void fillViewWithAdapter(CustomListAdapter mAdapter)
            throws ZeroChildException {
        if (getChildCount() == 0) {
            throw new ZeroChildException(
                    "CenterLockHorizontalScrollView must have one child");
        }
        if (getChildCount() == 0 || mAdapter == null)
            return;

        ViewGroup parent = (ViewGroup) getChildAt(0);

        parent.removeAllViews();

        for (int i = 0; i < mAdapter.getCount(); i++) {
            parent.addView(mAdapter.getView(i, null, parent));
        }
    }

    public void setCenter(int index) {

        ViewGroup parent = (ViewGroup) getChildAt(0);

        View preView = parent.getChildAt(prevIndex);
        preView.setBackgroundColor(Color.parseColor("#64CBD8"));
        android.widget.LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(5, 5, 5, 5);
        preView.setLayoutParams(lp);

        View view = parent.getChildAt(index);
        view.setBackgroundColor(Color.RED);

        int screenWidth = ((Activity) context).getWindowManager()
                .getDefaultDisplay().getWidth();

        int scrollX = (view.getLeft() - (screenWidth / 2))
                + (view.getWidth() / 2);
        this.smoothScrollTo(scrollX, 0);
        prevIndex = index;
    }

}

2)Create xml file in res/layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.krish.horizontalscrollview.CenterLockHorizontalScrollview
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>
    </com.krish.horizontalscrollview.CenterLockHorizontalScrollview>

    <LinearLayout
        android:id="@+id/bottomLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/scrollView"
        android:orientation="horizontal"
        android:weightSum="2" >

        <Button
            android:id="@+id/btnPrev"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Prev" />

        <Button
            android:id="@+id/btnNext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Next" />
    </LinearLayout>

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/bottomLayout"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:textColor="#FF0000"
        android:padding="@dimen/padding_medium" />

</RelativeLayout>

3)Use the CenterLockHorizontalScrollview in your activity as follows.

public class HorizontalScrollViewActivity extends Activity {
    CenterLockHorizontalScrollview centerLockHorizontalScrollview;
    CustomListAdapter customListAdapter;
    Button btnPrev, btnNext;
    int currIndex = 0;
    private TextView text;
    ArrayList<String> list = new ArrayList<String>() {

        {
            add("Manchester city");
            add("Manchester United");
            add("Chelsea");
            add("Liverpool");
            add("TottenHam");
            add("Everton");
            add("WestHam");
            add("Arsenal");
            add("West Broom");
            add("New Castle");
            add("Norich City");
            add("Swansea city");
            add("stroke city");

        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_horizontal_scroll_view);
        btnNext = (Button) findViewById(R.id.btnNext);
        btnPrev = (Button) findViewById(R.id.btnPrev);
        text=(TextView)findViewById(R.id.text);
        centerLockHorizontalScrollview = (CenterLockHorizontalScrollview) findViewById(R.id.scrollView);
        customListAdapter = new CustomListAdapter(this,
                R.layout.news_list_item, list);
        centerLockHorizontalScrollview.setAdapter(this, customListAdapter);
        btnNext.setOnClickListener(onClickListener);
        btnPrev.setOnClickListener(onClickListener);

    }

    OnClickListener onClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.btnPrev) {
                if (currIndex != 0) {
                    currIndex--;
                    centerLockHorizontalScrollview.setCenter(currIndex);
                    text.setText(list.get(currIndex==0?0:currIndex-1));
                }
            } else if (v.getId() == R.id.btnNext) {

                if (currIndex < list.size()) {
                    centerLockHorizontalScrollview.setCenter(currIndex);
                    currIndex++;
                    text.setText(list.get(currIndex-1));
                }
            }

        }
    };

}

这篇关于与Android的HoritzontalScrollView创建画廊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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