带有recyclerview的Android viewpager无法处理片段

时间:2017-08-14 13:21:43

标签: android android-recyclerview android-viewpager fragment

我正在使用带有viewpager的recyclerview片段。

我想在屏幕底部进行导航。 enter image description here

当我点击每个按钮时,每个片段都显示在frameLayout中。

当我第二次进入艺术家片段时,问题就出现了。(第一次出现时我发现了。) 正如您在此图片中看到的那样enter image description here

片段中出现了注释。并且滑动屏幕不能正常工作。它应该向左或向右移动,但它停在我停止移动手指的位置。

这是我的来源。

activity_main_board.xml

<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:layout_height="match_parent">


<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/navigation"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true">

</FrameLayout>

<LinearLayout
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="7dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/image_background7" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="53dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <LinearLayout
            android:id="@+id/my_board_button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView10"
                android:layout_width="30dp"
                android:layout_height="30dp"
                app:srcCompat="@drawable/ic_action_my" />

            <TextView
                android:id="@+id/textView34"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="MY"
                android:textSize="10sp" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/artwork_board_button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView13"
                android:layout_width="30dp"
                android:layout_height="30dp"
                app:srcCompat="@drawable/ic_action_artwork" />

            <TextView
                android:id="@+id/textView35"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Artwork"
                android:textSize="10sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/artist_board_button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView14"
                android:layout_width="30dp"
                android:layout_height="30dp"
                app:srcCompat="@drawable/ic_action_artist" />

            <TextView
                android:id="@+id/textView36"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Artist"
                android:textSize="10sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/alarm_board_button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView20"
                android:layout_width="30dp"
                android:layout_height="30dp"
                app:srcCompat="@drawable/ic_action_alert" />

            <TextView
                android:id="@+id/textView40"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Alarm"
                android:textSize="10sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/menu_board_button"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center_vertical|center_horizontal"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView19"
                android:layout_width="30dp"
                android:layout_height="30dp"
                app:srcCompat="@drawable/ic_action_menu" />

            <TextView
                android:id="@+id/textView38"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="전체"
                android:textSize="10sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
</RelativeLayout>

MainBoradActivity.java

public class MainBoardActivity extends AppCompatActivity implements View.OnClickListener{

private static LinearLayout my_board_button;
private static LinearLayout artwork_board_button;
private static LinearLayout artist_board_button;
private static LinearLayout menu_board_button;
private static LinearLayout alarm_board_button;

private static FrameLayout container;

private static MyBoardFragment myBoardFragment;
private static ArtworkBoardFragment artworkBoardFragment;
private static ArtistBoardFragment artistBoardFragment;
private static AlarmBoardFragment alarmBoardFragment;
private static MenuBoardFragment menuBoardFragment;

private static FragmentManager fragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_board);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    container = (FrameLayout) findViewById(R.id.container);

    my_board_button = (LinearLayout) findViewById(R.id.my_board_button);
    artwork_board_button = (LinearLayout) findViewById(R.id.artwork_board_button);
    artist_board_button = (LinearLayout) findViewById(R.id.artist_board_button);
    menu_board_button = (LinearLayout) findViewById(R.id.menu_board_button);
    alarm_board_button = (LinearLayout) findViewById(R.id.alarm_board_button);


    my_board_button.setOnClickListener(this);
    artwork_board_button.setOnClickListener(this);
    artist_board_button.setOnClickListener(this);
    menu_board_button.setOnClickListener(this);
    alarm_board_button.setOnClickListener(this);

    myBoardFragment = new MyBoardFragment();
    artworkBoardFragment = new ArtworkBoardFragment();
    artistBoardFragment = new ArtistBoardFragment();
    alarmBoardFragment = new AlarmBoardFragment();
    menuBoardFragment = new MenuBoardFragment();

    fragmentManager = getSupportFragmentManager();

    fragmentManager.beginTransaction().add(R.id.container, myBoardFragment).commitAllowingStateLoss();

}

@Override
public void onClick(View view) {

    switch (view.getId()){

        case R.id.my_board_button:

            FragmentTransaction myTransaction = fragmentManager.beginTransaction();

            myTransaction.replace(R.id.container, myBoardFragment);
            myTransaction.addToBackStack("0");
            myTransaction.commitAllowingStateLoss();

            break;

        case R.id.artwork_board_button:

            FragmentTransaction artworkTransaction = fragmentManager.beginTransaction();
            artworkTransaction.replace(R.id.container, artworkBoardFragment);
            artworkTransaction.addToBackStack("1");
            artworkTransaction.commitAllowingStateLoss();

            break;

        case R.id.artist_board_button:

            FragmentTransaction artistTransaction = fragmentManager.beginTransaction();

            artistTransaction.replace(R.id.container, artistBoardFragment);
            artistTransaction.addToBackStack("2");
            artistTransaction.commitAllowingStateLoss();

            break;

        case R.id.alarm_board_button:

            FragmentTransaction alarmTransaction = fragmentManager.beginTransaction();

            alarmTransaction.replace(R.id.container, alarmBoardFragment);
            alarmTransaction.addToBackStack("3");
            alarmTransaction.commitAllowingStateLoss();

            break;

        case R.id.menu_board_button:

            FragmentTransaction menuTransaction = fragmentManager.beginTransaction();

            menuTransaction.replace(R.id.container, menuBoardFragment);
            menuTransaction.addToBackStack("4");
            menuTransaction.commitAllowingStateLoss();

            break;


    }

}


}

ArtistBoardFragment.java

public class ArtistBoardFragment extends Fragment {

private static TabLayout artist_tabs;
private static ViewPager artist_container;
private static FragmentPagerAdapter pageAdapter;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


    return inflater.inflate(R.layout.fragment_artist_board, container, false);

}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    artist_tabs = (TabLayout) view.findViewById(R.id.artist_tabs);
    artist_container = (ViewPager) view.findViewById(R.id.artist_container);



    pageAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {

        ArtistNewFragment artistNewFragment = new ArtistNewFragment();
        ArtistCategoryFragment artistCategoryFragment = new ArtistCategoryFragment();


        private final String[] menuFragmentNames = new String[]{

                "new",
                "category"

        };

        @Override
        public Fragment getItem(int position) {

            switch (position){

                case 0:

                    Bundle recentBundle = new Bundle();
                    recentBundle.putInt("page", position);
                    artistNewFragment.setArguments(recentBundle);


                    return artistNewFragment;

                case 1:

                    Bundle bestBundle = new Bundle();
                    bestBundle.putInt("page", position);
                    artistCategoryFragment.setArguments(bestBundle);

                    return artistCategoryFragment;


                default:

                    return null;

            }

        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            return menuFragmentNames[position];
        }

    };


    artist_container.setAdapter(pageAdapter);
    artist_container.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {



        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    artist_tabs.setupWithViewPager(artist_container);


}


@Override
public void onResume() {
    super.onResume();

}


}

ArtistNewFragment.java

public class ArtistNewFragment extends Fragment {

private static RecyclerView new_content_list;
int pastVisibleItems, visibleItemCount, totalItemCount;
private static TimelineAdapter timelineAdapter;
private static RequestManager requestManager;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


    return inflater.inflate(R.layout.fragment_artist_new_content, container, false);

}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    new_content_list = (RecyclerView) view.findViewById(R.id.new_content_list);

    requestManager = Glide.with(getActivity());

    final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,false);

    new_content_list.setLayoutManager(layoutManager);
    new_content_list.setHasFixedSize(true);
    new_content_list.setNestedScrollingEnabled(false);

    //final NotiAdapter notiAdapter = new NotiAdapter(getApplicationContext(), FunctionBase.createFilter, false, lastCheckTime);
    timelineAdapter = new TimelineAdapter(getActivity(), requestManager);


    timelineAdapter.setObjectsPerPage(3);
    new_content_list.setAdapter(timelineAdapter);

    new_content_list.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);

            Log.d("dx", String.valueOf(dx));
            Log.d("dy", String.valueOf(dy));

            if(dy > 0) {
                visibleItemCount = layoutManager.getChildCount();
                totalItemCount = layoutManager.getItemCount();
                pastVisibleItems = layoutManager.findFirstVisibleItemPosition();

                if ( (visibleItemCount + pastVisibleItems) >= totalItemCount) {
                    timelineAdapter.loadNextPage();
                }

            }

        }
    });



}


@Override
public void onResume() {
    super.onResume();

    timelineAdapter.loadObjects(0);

}

}

1 个答案:

答案 0 :(得分:4)

由于您的应用程序的导航结构,很难确切地说出来,但认为您的问题的解决方案是使用ArtistBoardFragment内部的childFragmentManager。即,而不是

pageAdapter = new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) ...

使用

pageAdapter = new FragmentPagerAdapter(getChildFragmentManager()) ...