如何在滚动视图分页器上保持相同xml的不同线性布局的可见性

时间:2017-11-08 10:12:08

标签: android android-viewpager

我正在使用ViewPager。 我为寻呼机适配器制作了单一的XML(我有4页寻呼机),在那个XML中我只使用多个线性布局并将其可见性设置为Gone。默认情况下,第一个线性布局的可见性已打开,因此它可以轻松设置为寻呼机的第一页,我正在尝试的是当我滚动寻呼机时第一个线性布局可见性应该消失,第二个线性布局的可见性应该打开(相同的XML)并设置为寻呼机等。

这是我的xml(适配器)

<LinearLayout
    android:id="@+id/sub_l3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/_10sdp"
    android:background="#f5f5f5"
    android:orientation="vertical"
    android:visibility="gone">

    <TextView
        android:id="@+id/u_photos"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_15sdp"
        android:fontFamily="serif"
        android:text="Upload Photos \n (Ads with photos sell faster)"
        android:textColor="@color/black"
        android:textSize="@dimen/_12sdp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_10sdp"
        android:background="#f5f5f5"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/image_upload_1"
            android:layout_width="@dimen/_100sdp"
            android:layout_height="@dimen/_100sdp"
            android:src="@color/colorPrimaryDark" />


        <ImageView
            android:id="@+id/image_upload_2"
            android:layout_width="@dimen/_100sdp"
            android:layout_height="@dimen/_100sdp"
            android:layout_marginLeft="@dimen/_70sdp"
            android:src="@color/colorPrimaryDark" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_10sdp"
        android:background="#f5f5f5"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/image_upload_3"
            android:layout_width="@dimen/_100sdp"
            android:layout_height="@dimen/_100sdp"
            android:src="@color/colorPrimaryDark" />


        <ImageView
            android:id="@+id/image_upload_4"
            android:layout_width="@dimen/_100sdp"
            android:layout_height="@dimen/_100sdp"
            android:layout_marginLeft="@dimen/_70sdp"
            android:src="@color/colorPrimaryDark" />
    </LinearLayout>

</LinearLayout>

<LinearLayout
    android:id="@+id/sub_l1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/_10sdp"
    android:background="#f5f5f5"
    android:orientation="vertical"
   >

    <TextView
        android:id="@+id/ad_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="serif"
        android:text="Ad Title"
        android:textColor="@color/black"
        android:textSize="@dimen/_12sdp" />

    <EditText
        android:id="@+id/ed_ad_title"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_30sdp"
        android:layout_marginLeft="@dimen/_5sdp"
        android:layout_marginRight="@dimen/_5sdp"
        android:layout_marginTop="@dimen/_10sdp"
        android:background="@drawable/form_bg"
        android:ems="10"
        android:fontFamily="serif"
        android:hint="Ad Title"
        android:inputType="textPersonName"
        android:padding="@dimen/_3sdp"
        android:textColor="@color/black"
        android:textSize="@dimen/_12sdp" />

    <TextView
        android:id="@+id/category"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_15sdp"
        android:fontFamily="serif"
        android:text="Category"
        android:textColor="@color/black"
        android:textSize="@dimen/_12sdp" />

    <EditText
        android:id="@+id/ed_category"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_30sdp"
        android:layout_marginLeft="@dimen/_5sdp"
        android:layout_marginRight="@dimen/_5sdp"
        android:layout_marginTop="@dimen/_10sdp"
        android:background="@drawable/form_bg"
        android:ems="10"
        android:fontFamily="serif"
        android:hint="Category"
        android:inputType="textPersonName"
        android:padding="@dimen/_3sdp"
        android:textColor="@color/black"
        android:textSize="@dimen/_12sdp" />
</LinearLayout>


<LinearLayout
    android:id="@+id/sub_l2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/_10sdp"
    android:background="#f5f5f5"
    android:orientation="vertical"
    android:visibility="gone">

    <TextView
        android:id="@+id/description"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_15sdp"
        android:fontFamily="serif"
        android:text="Ad Description"
        android:textColor="@color/black"
        android:textSize="@dimen/_12sdp" />

    <EditText
        android:id="@+id/ed_description"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_30sdp"
        android:layout_marginLeft="@dimen/_5sdp"
        android:layout_marginRight="@dimen/_5sdp"
        android:layout_marginTop="@dimen/_10sdp"
        android:background="@drawable/form_bg"
        android:ems="10"
        android:fontFamily="serif"
        android:hint="Ad Description"
        android:inputType="textPersonName"
        android:padding="@dimen/_3sdp"
        android:textColor="@color/black"
        android:textSize="@dimen/_12sdp" />
</LinearLayout>

<LinearLayout
    android:id="@+id/sub_l4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/_10sdp"
    android:background="#f5f5f5"
    android:orientation="vertical"
    android:visibility="gone">

    <TextView
        android:id="@+id/user_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="serif"
        android:text="Name"
        android:textColor="@color/black"
        android:textSize="@dimen/_12sdp" />

    <EditText
        android:id="@+id/ed_user_name"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_30sdp"
        android:layout_marginLeft="@dimen/_5sdp"
        android:layout_marginRight="@dimen/_5sdp"
        android:layout_marginTop="@dimen/_10sdp"
        android:background="@drawable/form_bg"
        android:ems="10"
        android:fontFamily="serif"
        android:hint="Name"
        android:inputType="textPersonName"
        android:padding="@dimen/_3sdp"
        android:textColor="@color/black"
        android:textSize="@dimen/_12sdp"/>

    <TextView
        android:id="@+id/user_num"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_15sdp"
        android:fontFamily="serif"
        android:text="Phone number"
        android:textColor="@color/black"
        android:textSize="@dimen/_12sdp" />

    <EditText
        android:id="@+id/ed_user_num"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_30sdp"
        android:layout_marginLeft="@dimen/_5sdp"
        android:layout_marginRight="@dimen/_5sdp"
        android:layout_marginTop="@dimen/_10sdp"
        android:background="@drawable/form_bg"
        android:ems="10"
        android:fontFamily="serif"
        android:hint="Phone number"
        android:inputType="number"
        android:padding="@dimen/_3sdp"
        android:textColor="@color/black"
        android:textSize="@dimen/_12sdp"/>

    <TextView
        android:id="@+id/user_city"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_15sdp"
        android:fontFamily="serif"
        android:text="Enter a city"
        android:textColor="@color/black"
        android:textSize="@dimen/_12sdp"/>

    <EditText
        android:id="@+id/ed_user_city"
        android:layout_width="match_parent"
        android:layout_height="@dimen/_30sdp"
        android:layout_marginLeft="@dimen/_5sdp"
        android:layout_marginRight="@dimen/_5sdp"
        android:layout_marginTop="@dimen/_10sdp"
        android:background="@drawable/form_bg"
        android:ems="10"
        android:fontFamily="serif"
        android:hint="Enter a city"
        android:inputType="number"
        android:padding="@dimen/_3sdp"
        android:textColor="@color/black"
        android:textSize="@dimen/_12sdp" />


    <Button
        android:id="@+id/submit_add_btn"
        android:layout_width="@dimen/_130sdp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="@dimen/_17sdp"
        android:background="@color/green"
        android:gravity="center"
        android:text="Submit"
        android:textAllCaps="false"
        android:textColor="@color/white" />
</LinearLayout>

这是我的setOnPageChangeListener(..) //这里sub_l1,sub_l2,sub_l3,sub_l4是线性布局//

form_pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            pages = position;
            if(pages==0){
                sub_l1.setVisibility(View.VISIBLE);
            }else
            if (pages == 1) {
                sub_l1.setVisibility(View.GONE);
                sub_l2.setVisibility(View.VISIBLE);
            }else
            if (pages == 2) {
                sub_l2.setVisibility(View.GONE);
                sub_l1.setVisibility(View.GONE);
                sub_l3.setVisibility(View.VISIBLE);
            }else
            if(pages==3){
                sub_l2.setVisibility(View.GONE);
                sub_l1.setVisibility(View.GONE);
                sub_l3.setVisibility(View.GONE);
                sub_l4.setVisibility(View.VISIBLE);
            }

        }

        @Override
        public void onPageSelected(int position) {
            Submit_add.page = position;
            switch (position) {
                case 0:
                    Submit_add.radioGroup.check(R.id.radioButton);
                    break;
                case 1:
                    Submit_add.radioGroup.check(R.id.radioButton2);
                    break;
                case 2:
                    Submit_add.radioGroup.check(R.id.radioButton3);
                    break;
                case 3:
                    Submit_add.radioGroup.check(R.id.radioButton4);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });

我浪费了我一天的时间请帮忙 提前谢谢。

2 个答案:

答案 0 :(得分:0)

 ViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
 {

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
    sub[position].setVisibility(View.VISIBLE);

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
});

答案 1 :(得分:0)

试试这个:

@Override
public void onPageSelected(int position) {
Submit_add.page = position;
switch (position) {
    case 0:
        sub_l1.setVisibility(View.VISIBLE);
        sub_l2.setVisibility(View.GONE);
        sub_l3.setVisibility(View.GONE);
        sub_l4.setVisibility(View.GONE);
        Submit_add.radioGroup.check(R.id.radioButton);
        break;
    case 1:
        sub_l1.setVisibility(View.GONE);
        sub_l2.setVisibility(View.VISIBLE);
        sub_l3.setVisibility(View.GONE);
        sub_l4.setVisibility(View.GONE);
        Submit_add.radioGroup.check(R.id.radioButton2);
        break;
    case 2:
        sub_l1.setVisibility(View.GONE);
        sub_l2.setVisibility(View.GONE);
        sub_l3.setVisibility(View.VISIBLE);
        sub_l4.setVisibility(View.GONE);
        Submit_add.radioGroup.check(R.id.radioButton3);
        break;
    case 3:
        sub_l2.setVisibility(View.GONE);
        sub_l1.setVisibility(View.GONE);
        sub_l3.setVisibility(View.GONE);
        sub_l4.setVisibility(View.VISIBLE);
        Submit_add.radioGroup.check(R.id.radioButton4);
        break;
    }
}

说明: 据我所知,问题是滚动回来时。例如,如果我们滚动到第三页然后第一个和第二个布局将消失,第三个布局将是可见的,这就是我们想要的。但是如果我们滚动回第二页,那么第一个布局将会消失,第二个布局将可见,但第三个布局也将可见,因为我们从不隐藏它。 (第四种布局总是消失,因为我们从未滚动到第4页)。