固定按钮,即使是viewPager刷卡的停留 [英] Fixed button that stays even when viewPager is swiped

查看:172
本文介绍了固定按钮,即使是viewPager刷卡的停留的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的两个XML(浮动对话框主题),其中我使用viewPager与关闭按钮[X]。

关闭按钮位于两个XML的右上角。然而,正如我向左或向右滑动,按钮被刷卡来来回回过这看起来很奇怪。任何想法,以使按钮住得固定,当我刷卡网页?

从页面的一个code(两页是相似的):

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    安卓了minWidth =350dp
    安卓了minHeight =700dp
    机器人:背景=#CECECE
    机器人:ID =@ + ID / dialogLayout
    机器人:方向=垂直><的TableRow
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT><的TextView
    机器人:ID =@ + ID / lblInfo
    机器人:layout_marginLeft =10dp
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginTop = - 6DP
    机器人:文字=信息
    机器人:文字样式=大胆
    机器人:文字颜色=#000
    机器人:TEXTSIZE =40dp/><的ImageButton
    机器人:ID =@ + ID / btnClose
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginLeft =320dp
    机器人:layout_marginTop = - 4DP
    机器人:layout_marginRight = - 4DP
    机器人:背景=#0000
    机器人:SRC =@绘制/关闭/>< /&的TableRow GT;<的TextView
    机器人:ID =@ + ID / lblInfo
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =400dp
    机器人:layout_alignParentRight =真
    机器人:layout_marginLeft =10dp
    机器人:文字颜色=#000
    机器人:TEXTSIZE =17dp/>< / LinearLayout中>

该viewPager XML

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =612dp
    机器人:layout_height =700dp>    < android.support.v4.view.ViewPager
        机器人:ID =@ + ID / viewPager
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>< / LinearLayout中>


解决方案

在到底是什么我所做的就是放置关闭按钮(btnClose)和页面标题(lblTitle)的viewPager外从@Luksprog建议。当我申请onPageChangeListener的页面标题,这样TextView的将相应地更改标题翻转页面的关闭按钮是固定的! codeS:

 公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.viewpagerbase);    //添加onClickListener为btnClose
    的ImageButton btnClose =(的ImageButton)findViewById(R.id.btnClose);
    btnClose.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){
            onBack pressed();
        }
    });
    //附加的ViewPage适配器
    MyPagerAdapter适配器=新MyPagerAdapter();
    ViewPager myPager =(ViewPager)findViewById(R.id.viewPager);
    myPager.setAdapter(适配器);
    myPager.setCurrentItem(0);
    //进行页面设置改变监听
    myPager.setOnPageChangeListener(本);} //的onCreate结束

I have have a close button [x] in two of my xml (with floating dialog theme), which i am using viewPager with.

The close button is located at the top right hand corner of the two xml. However, as i swipe left or right, the button gets swipe to and fro too which looks really weird. Any idea to make the button stay fixed even when i swipe the pages?

Code from one of the page (both pages are similar):

    <?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:minWidth="350dp" 
    android:minHeight="700dp"
    android:background="#CECECE"
    android:id="@+id/dialogLayout"
    android:orientation="vertical">

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<TextView
    android:id="@+id/lblInfo"
    android:layout_marginLeft="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="-6dp"
    android:text="Information"
    android:textStyle="bold"
    android:textColor="#000"
    android:textSize="40dp" />

<ImageButton
    android:id="@+id/btnClose"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="320dp"
    android:layout_marginTop="-4dp"
    android:layout_marginRight="-4dp"
    android:background="#0000"
    android:src="@drawable/close" />

</TableRow>

<TextView
    android:id="@+id/lblInfo"
    android:layout_width="wrap_content"
    android:layout_height="400dp"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="10dp"
    android:textColor="#000"
    android:textSize="17dp" />



</LinearLayout>

The viewPager xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="612dp"
    android:layout_height="700dp">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

解决方案

In the end what I did was to place the close button (btnClose) and page title (lblTitle) outside the viewPager as advised from @Luksprog . The close button is fixed when i applied onPageChangeListener for the page title so that the TextView would change the title accordingly to the page flipped! Codes :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.viewpagerbase);

    //add onClickListener for the btnClose
    ImageButton btnClose = (ImageButton)findViewById(R.id.btnClose);
    btnClose.setOnClickListener( new OnClickListener() {
        public void onClick(View v) {
            onBackPressed();
        }
    });


    //attach the viewPage adapter 
    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.viewPager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(0);
    //set listener for page change 
    myPager.setOnPageChangeListener(this);

}   //end of onCreate

这篇关于固定按钮,即使是viewPager刷卡的停留的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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