Android的刷卡片段上点击 [英] Android Swipe Fragment on click

查看:133
本文介绍了Android的刷卡片段上点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段寻呼机,我建的第一个片段的按钮。
现在,我想让它滑动到第二个片段,当我点击该按钮。
我建立了这个至今:

 公共类方法文档扩展片段{        @TargetApi(Build.VERSION_ codeS.JELLY_BEAN)
        @覆盖
        公共查看onCreateView(最终LayoutInflater吹气,最后的ViewGroup容器,
                                 捆绑savedInstanceState){            查看机器人= inflater.inflate(R.layout.how_to,集装箱,FALSE);
            ((的TextView)android.findViewById(R.id.howTo))的setText(HOWTO);                按钮next_frag =(按钮)android.findViewById(R.id.next_frag);
                next_frag.setOnClickListener(新View.OnClickListener(){
                    @TargetApi(Build.VERSION_ codeS.JELLY_BEAN)
                    公共无效的onClick(视图v){
                        //滑动即可page`?
                        查看机器人= inflater.inflate(R.layout.how_to,集装箱,FALSE);                    }
                });
            返回机器人;        }
}

它没有工作。有谁知道如何解决这个问题。

编辑:
activity_main.xml中

 <?XML版本=1.0编码=UTF-8&GT?;
< android.support.v4.view.ViewPager
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID /寻呼机
    机器人:文字样式=大胆
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent/>

和我的按钮,将改变片段XML:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直
    机器人:背景=@绘制/ how_to_gradient>
    <按钮
        机器人:ID =@ + ID / next_frag
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:文字颜色=#FFF
        机器人:比重=中心
        机器人:layout_weight =1
        机器人:shadowDx =0
        机器人:shadowDy =0
        机器人:shadowRadius =5
        机器人:TEXTSIZE =21sp
        机器人:文字样式=大胆
        机器人:文字=测试
        机器人:背景=@绘制/ border_inner_orange/>< / LinearLayout中>


解决方案

尝试在按钮点击使用此:

  viewPager.setCurrentItem(nextposition);

或者如果你想有一个平滑滚动,你可以使用

  viewPager.setCurrentItem(位置,真正的);

编辑:

您可以通过获取视图寻呼机的当前页:

  viewPager.getCurrentItem();

获得数量和放大器;添加+1把值nextposition

编辑:

MainActivity:

里面MainActivity初始化MainActivity的静态实例:

 公共静态MainActivity mInstance = NULL;

然后MainActivity的onCreate方法里面这样做:

  mInstance =这一点;

以上这些是两个加法您MainActivity ..

 您现在的片段公共类方法文档扩展片段{        @TargetApi(Build.VERSION_ codeS.JELLY_BEAN)
        @覆盖
        公共查看onCreateView(最终LayoutInflater吹气,最后的ViewGroup容器,
                                 捆绑savedInstanceState){            查看机器人= inflater.inflate(R.layout.how_to,集装箱,FALSE);
            ((的TextView)android.findViewById(R.id.howTo))的setText(HOWTO);ViewPager viewPager =(ViewPager)MainActivity.mInstance.findViewById(R.id.pager);INT nextFragment = viewPager.getCurrentItem()+ 1;                按钮next_frag =(按钮)android.findViewById(R.id.next_frag);
                next_frag.setOnClickListener(新View.OnClickListener(){
                    @TargetApi(Build.VERSION_ codeS.JELLY_BEAN)
                    公共无效的onClick(视图v){
                        //滑动即可page`?viewPager.setCurrentItem(nextFragment);                    }
                });
            返回机器人;        }
}

I have a Fragment pager, I built a button on the first fragment. Now I want to make it swipe to the second fragment when I click on that button. I built this so far:

public class HowTo extends Fragment {

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                                 Bundle savedInstanceState) {

            View android = inflater.inflate(R.layout.how_to, container, false);
            ((TextView) android.findViewById(R.id.howTo)).setText("howto");

                Button next_frag = (Button) android.findViewById(R.id.next_frag);
                next_frag.setOnClickListener(new View.OnClickListener() {
                    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                    public void onClick(View v) {
                        // Swipe to page`?
                        View android = inflater.inflate(R.layout.how_to, container, false);

                    }
                });
            return android;

        }


}

It did not work. Does anyone know how to solve this issue.

EDIT: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:textStyle="bold"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

And xml with my button that is going to change fragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/how_to_gradient">
    <Button
        android:id="@+id/next_frag"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#fff"
        android:gravity="center"
        android:layout_weight="1"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:textSize="21sp"
        android:textStyle="bold"
        android:text="test"
        android:background="@drawable/border_inner_orange"/>

</LinearLayout>

解决方案

Try using this on Button Click:

viewPager.setCurrentItem(nextposition);

or if you want a smooth scroll you can use

viewPager.setCurrentItem(position,true);

Edit:

You can get the Current page of View pager by :

viewPager.getCurrentItem();

get the number & add +1 to put the value in nextposition

EDITED:

MainActivity:

inside MainActivity initialize a static instance of MainActivity:

public static MainActivity mInstance = null;

then inside onCreate method of MainActivity do like this:

mInstance = this;

these above are two additions in your MainActivity..

Now Your Fragment

public class HowTo extends Fragment {

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                                 Bundle savedInstanceState) {

            View android = inflater.inflate(R.layout.how_to, container, false);
            ((TextView) android.findViewById(R.id.howTo)).setText("howto");

ViewPager viewPager = (ViewPager) MainActivity.mInstance.findViewById(R.id.pager);

int nextFragment = viewPager.getCurrentItem() + 1;

                Button next_frag = (Button) android.findViewById(R.id.next_frag);
                next_frag.setOnClickListener(new View.OnClickListener() {
                    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
                    public void onClick(View v) {
                        // Swipe to page`?

viewPager.setCurrentItem(nextFragment);

                    }
                });
            return android;

        }
}

这篇关于Android的刷卡片段上点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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