ssamkyu
ssamkyu

Reputation: 105

Android viewpager with recyclerview not working in fragment

I am working with recyclerview with viewpager in fragment.

I want to make navigation on bottom of screen. enter image description here

and when I click each button, each fragment show in the frameLayout.

the problem is happen when I enter into the artist fragment second time.(at first time it show up find as I intended.) as you can see in this image enter image description here

there is noting shows up in the fragment. and swiping the screen not working properly. It should moving stick to the left or right but it stop at the point where I stop moving my finger.

here is my source.

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);

}

}

Upvotes: 0

Views: 1979

Answers (1)

cwbowron
cwbowron

Reputation: 1025

It's hard to tell exactly due to the navigational structure of your app, but think the solution to your problem is to use the childFragmentManager inside of the ArtistBoardFragment. i.e, instead of

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

Use

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

Upvotes: 5

Related Questions