简体   繁体   中英

only first fragment is being shown under tablayout

I used this and put a recycler view with card views under the tabs. Only the first tab is displaying a recycler view the others don't. The tabs look good and the first fragment looks good, but when I try to swipe to other tabs, the fragment doesn't show.

My code: Main activty layout:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
tools:context="codycoogan.equations.about_activity"
android:orientation="vertical"
>

<include layout="@layout/tool_bar"
    android:id="@+id/toolBar"
    />
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/viewPagerContainer"
    />


</LinearLayout>

The layout for the above framelayout which has tablayout and viewpager:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">

<android.support.design.widget.AppBarLayout    android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
xmlns:android="http://schemas.android.com/apk/res/android">



    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        android:background="@color/colorPrimary"
app:tabGravity="fill"

        />


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

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:id="@+id/pager"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:clickable="true"

    />

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

Set up for tablayout and viewpager:

public class mainViewPagerFragment extends Fragment {
private TabLayout tabLayout;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_main_view_pager, container, false);
    RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.favoritesRecycler);

    ViewPager viewPager = (ViewPager)view.findViewById(R.id.pager);
    setupViewPager(viewPager);
    tabLayout = (TabLayout)view.findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    return recyclerView;
}

private void setupViewPager(ViewPager viewPager) {
    ViewPageAdapter adapter = new ViewPageAdapter(getChildFragmentManager());
    adapter.addFragment(new RecyclerFragment(), "ONE");
    adapter.addFragment(new favoritesFragment(), "TWO");
    viewPager.setAdapter(adapter);


}



class ViewPageAdapter extends FragmentStatePagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPageAdapter(FragmentManager fragmentManager){
        super(fragmentManager);

    }

    @Override
    public Fragment getItem(int position) {

        return mFragmentList.get(position);
    }





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

    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {


         return mFragmentTitleList.get(position);
    }




}

}

My fragment:

public class RecyclerFragment extends Fragment { //FOR GEOMETRY
private Cursor cursor;
private SQLiteDatabase db;
private Vibrator vibrator;

private int[] imageArray = {

        R.mipmap.circle55,
        R.mipmap.circle55,
        R.mipmap.circle55,
        R.mipmap.circle55,
        R.mipmap.octagon55,

};
private ArrayList<Integer> image = new ArrayList<>();

private ArrayList<String> names = new ArrayList<>();


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

    new listPop().execute();
    populateImageArray();
    vibrator = (Vibrator)getActivity().getSystemService(Context.VIBRATOR_SERVICE);
    RecyclerView recyclerView = (RecyclerView) inflater.inflate(R.layout.recylcler_layout, container, false);


    recylcerAdapter RecylcerAdapter =
            new recylcerAdapter(image.toArray(new  Integer[image.size()]), names.toArray(new String[names.size()]));
    LinearLayoutManager manager = new LinearLayoutManager(getActivity());
    manager.canScrollVertically();
    recyclerView.setLayoutManager(manager);
    recyclerView.setAdapter(RecylcerAdapter);

    RecylcerAdapter.setListener(new recylcerAdapter.Listener() {
        public void onClick(int position) {
            vibrator.vibrate(50);

            Intent intent = new Intent(getActivity(), EquationsActivity.class);
            intent.putExtra(EquationsActivity.POSITION, position);
            getActivity().startActivity(intent);

        }
    });

    return recyclerView;
}



private class listPop extends AsyncTask<Integer, Void, Boolean> {


    @Override
    protected Boolean doInBackground(Integer... equate) {

        SQLiteOpenHelper databaseHelper = new DatabaseHelper(getActivity());
        try {
            db = databaseHelper.getReadableDatabase();
            cursor = db.query("EQUATIONTABLE", new String[]{"_id", "NAME"}, null, null,
                    null, null, "SORTORDER ASC");

            cursor.moveToFirst();

            while (!cursor.isAfterLast()) {
                names.add(cursor.getString(cursor.getColumnIndex("NAME")));
                cursor.moveToNext();
            }

            return true;
        } catch (SQLiteException e) {
            e.printStackTrace();
            return false;
        } finally {
            cursor.close();
            db.close();
        }
    }


    @Override
    protected void onPostExecute(Boolean success) {
        checkFirstRun();

        if (!success) {
            Toast toast = Toast.makeText(getActivity(), "Database Error", Toast.LENGTH_LONG);
            toast.show();
        }

    }
}

I figured out my issue! The problem is that you have to put the below code under in onPostExecute of the Async task that I ran first on OnCreateView

recylcerAdapter RecylcerAdapter =
        new recylcerAdapter(image.toArray(new  Integer[image.size()]), names.toArray(new String[names.size()]));
LinearLayoutManager manager = new LinearLayoutManager(getActivity());
manager.canScrollVertically();
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(RecylcerAdapter);

RecylcerAdapter.setListener(new recylcerAdapter.Listener() {
    public void onClick(int position) {
        vibrator.vibrate(50);

        Intent intent = new Intent(getActivity(), EquationsActivity.class);
        intent.putExtra(EquationsActivity.POSITION, position);
        getActivity().startActivity(intent);

    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM