Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何垂直滚动Viewpager_Android_Scroll_Android Viewpager_Android Scrollview_Android Nestedscrollview - Fatal编程技术网

Android 如何垂直滚动Viewpager

Android 如何垂直滚动Viewpager,android,scroll,android-viewpager,android-scrollview,android-nestedscrollview,Android,Scroll,Android Viewpager,Android Scrollview,Android Nestedscrollview,我无法垂直滚动查看寻呼机。在子布局中使用NestedScrollView时,页面将变为空白。我尝试了许多解决方案,但都失败了 这是我的主要片段nav_swipe.xml <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"

我无法垂直滚动查看寻呼机。在子布局中使用NestedScrollView时,页面将变为空白。我尝试了许多解决方案,但都失败了

这是我的主要片段nav_swipe.xml

<?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" >


        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/pager_title_strip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:background="#33b5e5"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"
            android:textColor="#fff" />

    </android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>
}

这是MyPagerAdapter.java

public class MyPagerAdapter extends FragmentStatePagerAdapter {
    private Context context;
    private String[] tabTitlesArray = null;
    public MyPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        tabTitlesArray = context.getResources().getStringArray(R.array.tab_titles);
        this.context= context;
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new AFragment();
        Bundle args = new Bundle();
        args.putString(AFragment.ARG_OBJECT, tabTitlesArray[i]);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return tabTitlesArray.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {

        //return tabTitleArray[position];
        return "OBJECT " + (position + 1);
    }
}
这是affragment.java

public class AFragment extends Fragment {
    public static final String ARG_OBJECT = "object";
    public AFragment() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_a, container, false);
        Bundle args = getArguments();
        ((TextView) rootView.findViewById(R.id.text1)).setText(args.getString(ARG_OBJECT));
        return rootView;
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }
}
请尝试以下代码:

编辑布局

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">


<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">



    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"/>

    </LinearLayout>

</ScrollView>
{


}

请使用ViewPager.PageTransformer给人垂直ViewPager的错觉。实现垂直滚动而不是水平滚动

/**
 * Uses a combination of a PageTransformer and swapping X & Y coordinates
 * of touch events to create the illusion of a vertically scrolling ViewPager. 
 * 
 * Requires API 11+
 * 
 */
public class VerticalViewPager extends ViewPager {

public VerticalViewPager(Context context) {
    super(context);
    initiate();
}

public VerticalViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    initiate();
}

private void initiate() {
    // The majority of the magic happens here
    setPageTransformer(true, new VerticalPageTransformer());
    // The easiest way to get rid of the overscroll drawing that happens on the left and right
    setOverScrollMode(OVER_SCROLL_NEVER);
}

private class VerticalPageTransformer implements ViewPager.PageTransformer {

    @Override
    public void transformPage(View view, float position) {

        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            view.setAlpha(0);

        } else if (position <= 1) { // [-1,1]
            view.setAlpha(1);

            // Counteract the default slide transition
            view.setTranslationX(view.getWidth() * -position);

            //set Y position to swipe in from top
            float yPosition = position * view.getHeight();
            view.setTranslationY(yPosition);

        } else { // (1,+Infinity]
            // This page is way off-screen to the right.
            view.setAlpha(0);
        }
    }
}

/**
 * Swaps the X and Y coordinates of your touch event.
 */
private MotionEvent swapXY(MotionEvent ev) {
    float width = getWidth();
    float height = getHeight();

    float newX = (ev.getY() / height) * width;
    float newY = (ev.getX() / width) * height;

    ev.setLocation(newX, newY);

    return ev;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev){
    boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
    swapXY(ev); // return touch coordinates to original reference frame for any child views
    return intercepted;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    return super.onTouchEvent(swapXY(ev));
}

}
/**
*使用PageTransformer和交换X&Y坐标的组合
*通过触摸事件创建垂直滚动的ViewPager的幻觉。
* 
*需要API 11+
* 
*/
公共类VerticalViewPage扩展了ViewPager{
公共VerticalViewPage(上下文){
超级(上下文);
发起();
}
公共VerticalViewPage(上下文、属性集属性){
超级(上下文,attrs);
发起();
}
私有void initiate(){
//大部分魔法都发生在这里
setPageTransformer(true,新的VerticalPageTransformer());
//消除发生在左侧和右侧的过卷图形的最简单方法
设置超滚动模式(超滚动模式);
}
私有类VerticalPageTransformer实现ViewPager.PageTransformer{
@凌驾
公共页面(视图、浮动位置){
if(位置<-1){/[-无穷大,-1)
//这个页面在屏幕的左边。
视图。setAlpha(0);

}否则如果(位置当我使用您的解决方案时,Viewpager页面变为空白。结果相同。没有更改。现在我添加了更多。请检查。当我添加NestedScrollView时,页面变为空白。如果我删除NestedScrollView,则内容显示和静态。我向您发布了我项目中的代码,并对其进行了测试。滚动视图对我有效。请使用主活动的布局。)我给了你/!\I更新了片段的布局。使用我给你的代码创建片段适配器复制我给你的主要代码让我知道,但它应该可以工作我无法找出问题所在。一些页面显示和滚动,一些使用ScrollView时为空。即使使用我给的代码,这也无法工作你?这个对我很好用…你确定你没有忘记任何行吗?使用。你能分享一下你是如何在
fragment_a.xml
@Mirwise Khan Child fragment layout is“fragment_a.xml”中使用
ScrollView
的吗
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">


<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">



    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"/>

    </LinearLayout>

</ScrollView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/main_layout"
android:layout_height="match_parent"
tools:context=".com.example.thomas.activity.MainActivity">






<android.support.design.widget.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">



    <android.support.design.widget.TabLayout
        android:id="@+id/main_tabs"
        android:layout_width="match_parent"
        app:tabIndicatorColor="@android:color/white"
        app:tabSelectedTextColor="@android:color/white"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    </android.support.design.widget.TabLayout>

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

<android.support.v4.view.ViewPager
    android:id="@+id/main_tabs_pager"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/appBarLayout">
</android.support.v4.view.ViewPager>
public class FragmentAdaptater extends FragmentPagerAdapter
private final List<Fragment>  lstFragment = new ArrayList<>();
private final List<String> lstTitles = new ArrayList<>();

public FragmentAdaptater(FragmentManager fm) {
    super(fm);
}


@Override
public Fragment getItem(int i) {
    return lstFragment.get(i);
}

@Nullable
@Override
public CharSequence getPageTitle(int position) {
    return lstTitles.get(position);
}

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


public void AddFragment (Fragment fragment , String title)
{
    lstFragment.add(fragment);
    lstTitles.add(title);
}
public class MainActivity extends AppCompatActivity {


private Toolbar mToolbar;
private FragmentPagerAdapter fragmentadaptater2;
private ViewPager mviewpager;     
private TabLayout mtablayout;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_main_2);
    mviewpager = (ViewPager) findViewById(R.id.main_tabs_pager);
    mToolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
    mtablayout = (TabLayout) findViewById(R.id.main_tabs);

    setSupportActionBar(mToolbar);




    fragmentadaptater2 = new FragmentAdaptater(getSupportFragmentManager());


    ((FragmentAdaptater) fragmentadaptater2).AddFragment(new YOURFRAGMENT(),"YOUR FRAGMENT NAME");
    ((FragmentAdaptater) fragmentadaptater2).AddFragment(new YOURFRAGMENT(),"YOUR FRAGMENT NAME");

        ((FragmentAdaptater) fragmentadaptater2).AddFragment(new YOURFRAGMENT(),"YOUR FRAGMENT NAME");




    mviewpager.setAdapter(fragmentadaptater2);
    mtablayout = (TabLayout) findViewById(R.id.main_tabs);
    mtablayout.setupWithViewPager(mviewpager);
/**
 * Uses a combination of a PageTransformer and swapping X & Y coordinates
 * of touch events to create the illusion of a vertically scrolling ViewPager. 
 * 
 * Requires API 11+
 * 
 */
public class VerticalViewPager extends ViewPager {

public VerticalViewPager(Context context) {
    super(context);
    initiate();
}

public VerticalViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    initiate();
}

private void initiate() {
    // The majority of the magic happens here
    setPageTransformer(true, new VerticalPageTransformer());
    // The easiest way to get rid of the overscroll drawing that happens on the left and right
    setOverScrollMode(OVER_SCROLL_NEVER);
}

private class VerticalPageTransformer implements ViewPager.PageTransformer {

    @Override
    public void transformPage(View view, float position) {

        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
            view.setAlpha(0);

        } else if (position <= 1) { // [-1,1]
            view.setAlpha(1);

            // Counteract the default slide transition
            view.setTranslationX(view.getWidth() * -position);

            //set Y position to swipe in from top
            float yPosition = position * view.getHeight();
            view.setTranslationY(yPosition);

        } else { // (1,+Infinity]
            // This page is way off-screen to the right.
            view.setAlpha(0);
        }
    }
}

/**
 * Swaps the X and Y coordinates of your touch event.
 */
private MotionEvent swapXY(MotionEvent ev) {
    float width = getWidth();
    float height = getHeight();

    float newX = (ev.getY() / height) * width;
    float newY = (ev.getX() / width) * height;

    ev.setLocation(newX, newY);

    return ev;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev){
    boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
    swapXY(ev); // return touch coordinates to original reference frame for any child views
    return intercepted;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    return super.onTouchEvent(swapXY(ev));
}

}