ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – ViewPager不会使用FragmentPagerAdapter进行滑动

2019-06-20 22:13:22  阅读:150  来源: 互联网

标签:android android-ui


我在Activity中有两个片段,在纵向模式下,当用户在ListView中的某个位置单击时会显示第一个片段(ListFragment),而在横向模式下,两个片段都会显示.但是我想在纵向模式下使用滑动功能,并且应该显示下一个片段,如this example,

这是我的主要laucher Activity,它扩展了FragmentActivity,

package com.myexample.fragmentdemoexample;

import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.ViewGroup;

public class MainFragmentActivity extends FragmentActivity {
    private static int NUM_ITEMS = 4;

    MyAdapter mAdapter;

    ViewPager mPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mPager = (ViewPager) findViewById(R.id.pager);
        instantiateFragments();
        Log.i("MainFragmentActivity", "MainFragmentActivity is Created");
    }

    public void instantiateFragments() {
        List<Fragment> fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this, MyListFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, DetailFragment.class.getName()));
        mAdapter = new MyAdapter(getSupportFragmentManager(), fragments);
        mPager.setAdapter(mAdapter);
        mPager.setCurrentItem(0);
    }

    public class MyAdapter extends FragmentPagerAdapter {
        private FragmentManager frmanager;
        private List<Fragment> fragments;

        public MyAdapter(FragmentManager fm, List<Fragment> fragList) {
            super(fm);
            frmanager = fm;
            fragments = fragList;
        }

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }
    }
}

这是我的layout / main.xml文件

<?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:orientation="horizontal" >

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1" >
    </android.support.v4.view.ViewPager>

    <fragment
        android:id="@+id/listFragment"
        android:layout_width="150dip"
        android:layout_height="match_parent"
        class="com.myexample.fragmentdemoexample.MyListFragment"
        android:tag="listFragment" >
    </fragment>

    <fragment
        android:id="@+id/detailFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.myexample.fragmentdemoexample.DetailFragment"
        android:tag="detailFragment" >
        <!-- Preview: layout=@layout/details -->
    </fragment>

</LinearLayout>

和/layout-port/main.xml文件包含

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

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1" >
    </android.support.v4.view.ViewPager>

    <fragment
        android:id="@+id/listFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.myexample.fragmentdemoexample.MyListFragment" />

</LinearLayout>

我不知道为什么View Pager没有刷到下一个片段……我怎么能这样做?我是第一次使用Fragment API ..所以请指出正确的方向.

注意有关其他文件,请参阅this question

解决方法:

最后经过几个小时的骚扰后,通过从主布局文件中删除Fragment标签来解决问题.现在main.xml包含

<?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:orientation="horizontal" >

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1" >
    </android.support.v4.view.ViewPager>


</LinearLayout>

这对于有类似问题的其他人可能会有所帮助……

标签:android,android-ui
来源: https://codeday.me/bug/20190620/1248572.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有