如何滚动查看分页器内容

时间:2017-10-28 17:36:36

标签: android android-viewpager android-coordinatorlayout android-collapsingtoolbarlayout

我在折叠工具栏布局中有一个带有标签布局的视图寻呼机。每个选项卡包含另一个视图寻呼机。我的问题是当我滚动浏览寻呼机的内容时,它没有。当我尝试滚动查看寻呼机的内容时,其父级“折叠工具栏布局”会滚动。我活动的布局

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
android:background="@color/colorBackground"
android:id="@+id/layout_parent">
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/MyTheme">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapse_toolbar"
        android:layout_width="match_parent"
        app:titleEnabled="false"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">
        <!--android:layout_height="520dp"-->
        <LinearLayout
            android:id="@+id/ll_parent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:paddingTop="50dp"
            android:orientation="vertical">
            <android.support.design.widget.TabLayout
                android:id="@+id/tabs_upper"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:tabIndicatorColor="@android:color/white"
                android:background="@color/colorPrimary" />
            <com.example.custom_view.custom_view_pager.MyWrapContentViewPager
                android:id="@+id/pager_upper"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="10dp"
                android:scrollbars="vertical"/>
           <!--android:layout_height="330dp"-->
            <TextView
                android:id="@+id/text_m"
                android:layout_width="100dp"
                android:layout_height="30dp"
                android:text="More"
                android:textColor="@color/color_red"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="20dp"
                android:layout_marginLeft="50dp"/>
        </LinearLayout>

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar" />
        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:tabIndicatorColor="@android:color/white"
            android:background="@drawable/tab_bar_bg"
            android:layout_gravity="bottom"/>

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

我在tab元素中使用的一个片段:

public class TestFragment extends Fragment{

private ViewPager pagerSummary ;
private ArrayList<String> slider_image_list;

private TextViewPagerAdapter adapter;


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_whole_book_new, null);
    setUpSlideView(root);

    return root;
}


private void setUpSlideView(ViewGroup view){
    // method for initialisation
    initSlider(view);

}

private void initSlider(ViewGroup view) {
    pagerSummary = view.findViewById(R.id.pager_whole_book)

    slider_image_list = new ArrayList<>();
    slider_image_list.add(getActivity().getResources().getString(R.string.law_one));
    slider_image_list.add(getActivity().getResources().getString(R.string.law_two);

    adapter = new TextViewPagerAdapter(slider_image_list);
    pagerSummary.setAdapter(adapter);
    pagerSummary.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            //Toast.makeText(getActivity(), "wholebook onPageScrolled", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPageSelected(int position) {
            addBottomDots(position);
            //Toast.makeText(getActivity(), "wholebook onPageSelected", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            //Toast.makeText(getActivity(), "wholebook onPageScrollStateChanged", Toast.LENGTH_SHORT).show();
        }
    });


}


public class TextViewPagerAdapter extends ObjectAtPositionPagerAdapter {

    private final ArrayList<String> strings;
    TextView textView1 ;

    public TextViewPagerAdapter(ArrayList<String> strings) {
        super();
        this.strings = strings;
    }

    @Override
    public int getCount() {
        return strings.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public Object instantiateItemObject(ViewGroup container, int position) {
        View view = View.inflate(container.getContext(), R.layout.test_item, null);

        textView1 = view.findViewById(R.id.text_test);
        textView1.setText(getActivity().getResources().getString(R.string.law_one)+
                getActivity().getResources().getString(R.string.law_one)+
                getActivity().getResources().getString(R.string.law_one)+
                getActivity().getResources().getString(R.string.law_one)+
                getActivity().getResources().getString(R.string.law_one));

        container.addView(view);

        return view;
    }

    @Override
    public void destroyItemObject(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "View" + position;
    }

}

}

我的片段布局

<LinearLayout
        android:id="@+id/ll_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <com.example.custom_view.custom_view_pager.WrapContentViewPager
            android:id="@+id/pager_whole_book"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

也是test_item.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:scrollbars="vertical">
<TextView
android:id="@+id/text_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /></ScrollView>

1 个答案:

答案 0 :(得分:0)

看看NestedScrollView。只需包装您想要在其中滚动的内容。