如何在标题上显示和隐藏视图而不影响下面的ViewPager?

时间:2019-01-26 12:40:01

标签: android

根据我的要求,我在顶部有一个操作栏。在操作栏下方,我还有另一个标题。在该标题上,我有一个按钮。在该标题下,其余内容是视图寻呼机。现在,当我单击标题上的按钮时,一些文本视图列表应显示在下面的viewpager上。再次,当我单击按钮时,这些文本视图应隐藏。现在我的问题是,在显示和显示顶部标题文本视图时,视图分页器会受到影响。

以下是我的图片:

enter image description here

代码:

PagerLayout:
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/viewPager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/colorAccent"/>
    <LinearLayout
        android:id="@+id/mainLinearlayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/lin_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"

            >

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginTop="10dp"
                android:text="Total"
                android:textStyle="bold"
                android:textColor="@color/white"
                android:textSize="18sp" />

            <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"


                app:tabBackground="@drawable/tab_selector"
                app:tabIndicatorHeight="0dp" />


        </LinearLayout>




        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@color/black"
            >

            <Button
                android:id="@+id/btn1"
                android:layout_width="36dp"
                android:layout_height="10dp"
                android:layout_marginBottom="4dp"
                android:background="@color/calculate_grey"
                android:layout_gravity="center_horizontal"
                 />

            <TextView
                android:id="@+id/tv1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="10px"
                android:textColor="@color/white"
                android:visibility="gone"
                android:text="Text 1" />

            <TextView
                android:id="@+id/tv2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="10px"
                android:textColor="@color/white"
                android:visibility="gone"
                android:text="Text 2" />

            <TextView
                android:id="@+id/tv3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="10px"
                android:textColor="@color/white"
                android:visibility="gone"
                android:text="Text 3" />

            <Button
                android:id="@+id/btn2"
                android:layout_width="36dp"
                android:layout_height="10dp"
                android:layout_marginBottom="4dp"
                android:background="@color/calculate_grey"
                android:layout_gravity="center_horizontal"
                android:visibility="gone"
                />

        </LinearLayout>

    </LinearLayout>





</RelativeLayout>

Fragmemt:

public class ViewPagerFragment extends Fragment {

    TextView tv1,tv2,tv3;
    Button button1, button2;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //returning our layout file
        //change R.layout.yourlayoutfilename for each of your fragments
        View v = inflater.inflate(R.layout.pagerlayout, container, false);
        ViewPager pager = (ViewPager)v.findViewById(R.id.viewPager);
        pager.setAdapter(new ViewPagerAdapter(getChildFragmentManager()));

        TabLayout tabLayout = (TabLayout) v.findViewById(R.id.tab_layout);
        tabLayout.setupWithViewPager(pager, true);

        tv1 = (TextView)v.findViewById(R.id.tv1);
        tv2 = (TextView)v.findViewById(R.id.tv2);
        tv3 = (TextView)v.findViewById(R.id.tv3);

        button1 = (Button) v.findViewById(R.id.btn1);
        button2 = (Button) v.findViewById(R.id.btn2);
        button1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // do something
                button1.setVisibility(v.GONE);
                tv1.setVisibility(v.VISIBLE);
                tv2.setVisibility(v.VISIBLE);
                tv3.setVisibility(v.VISIBLE);
                button2.setVisibility(v.VISIBLE);
            }
        });

        button2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // do something
                button1.setVisibility(v.VISIBLE);
                tv1.setVisibility(v.GONE);
                tv2.setVisibility(v.GONE);
                tv3.setVisibility(v.GONE);
                button2.setVisibility(v.GONE);
            }
        });


        return v;
    }




}

1 个答案:

答案 0 :(得分:0)

您可以使用android:visibility =“ invisible”代替。同样,在Java中也没有设置它而将其设置为不可见。