Fragment does not appear in a Toolbar with Tablayout

Asked

Viewed 287 times

1

I am trying to show 2 list layouts in the same Activity using Viewpager, but for some reason the layouts are not being shown on the application screen (they are blank), however, when debugging, oncreateview lines are being read, ie Fragment is being called.

layoutdaactivity.xml

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

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

    <include
        android:id="@+id/toolbar"
        layout="@layout/app_bar"
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"/>

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


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



</RelativeLayout>

Activitytabs.java

public class ListSubcategoriaActivity extends AppCompatActivity {
private Toolbar toolbar;
private ViewPager pager;
private SubcategoriaPagerAdapter adapter;
private TabLayout tabs;
private CharSequence titles[] = {"Receita", "Despesa"};
int numboftabs = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_subcat);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    adapter = new SubcategoriaPagerAdapter(getSupportFragmentManager(), titles,  numboftabs);
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);
    pager.setCurrentItem(0);
    tabs = (TabLayout) findViewById(R.id.tabs);
    tabs.setSelectedTabIndicatorColor(getResources().getColor(R.color.tabsScrollColor));
    tabs.setTabTextColors(getResources().getColor(R.color.tabsScrollColor), getResources().getColor(R.color.tabsScrollColor));
    tabs.setupWithViewPager(pager);
    changeTabsFont();
}



private void changeTabsFont() {

    ViewGroup vg = (ViewGroup) tabs.getChildAt(0);
    int tabsCount = vg.getChildCount();
    for (int j = 0; j < tabsCount; j++) {
        ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
        int tabChildsCount = vgTab.getChildCount();
        for (int i = 0; i < tabChildsCount; i++) {
            View tabViewChild = vgTab.getChildAt(i);
            if (tabViewChild instanceof TextView) {
                ((TextView) tabViewChild).setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);
            }
        }
    }
}

}

Pageradapter.java

public class SubcategoriaPagerAdapter extends FragmentStatePagerAdapter {
CharSequence[] titles;
int numOfTabs;

public SubcategoriaPagerAdapter(FragmentManager fm, CharSequence mTitles[], int mNumOfTabs) {
    super(fm);
    this.titles = mTitles;
    this.numOfTabs = mNumOfTabs;
}

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

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

@Override
public Fragment getItem(int position) {
    if (position == 0) {
        SubcategoriaReceita tb1 = new SubcategoriaReceita();
        return tb1;
    } else {
        SubcategoriaDespesa tb2 = new SubcategoriaDespesa();
        return tb2;
    }
}


}

Fragment1.java

public class SubcategoriaReceita extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private RecyclerView mRecyclerView;

private OnFragmentInteractionListener mListener;

public SubcategoriaReceita() {
    // Required empty public constructor
}

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment SubcategoriaReceita.
 */
// TODO: Rename and change types and number of parameters
public static SubcategoriaReceita newInstance(String param1, String param2) {
    SubcategoriaReceita fragment = new SubcategoriaReceita();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_subcategoria_receita, container, false);
    mRecyclerView = (RecyclerView) v.findViewById(R.id.recySub);
    refreshLista();

    return v ;
}

private void refreshLista(){
    CategoriaDAO cDAO = new CategoriaDAO(getContext());
    SubCategoriaDAO sDAO = new SubCategoriaDAO(getContext());
    List<Categoria> cList = cDAO.getLista(0);
    for (Categoria c : cList){
        List<SubCategoria> sList = sDAO.getLista(c.getId());
        c.setChildList(sList);
    }
    SubcategoriaExpandableAdapter adapter = new SubcategoriaExpandableAdapter(getContext(), cList);
    mRecyclerView.setAdapter(adapter);
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p/>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}
}

Fragment1.xml

<FrameLayout 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">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recySub"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</FrameLayout>

Recyclerview Adapter (Its onBind methods are not stopping at breakpoint when I debug)

public class SubcategoriaExpandableAdapter extends ExpandableRecyclerAdapter<CategoriaViewHolder,SubcategoriaViewHolder> {
private LayoutInflater mInflater;


public SubcategoriaExpandableAdapter(Context context, @NonNull List<? extends ParentListItem> categoriaList){
    super(categoriaList);
    mInflater = LayoutInflater.from(context);
}

@Override
public SubcategoriaViewHolder onCreateChildViewHolder(ViewGroup childViewGroup) {
    View view = mInflater.inflate(R.layout.subcategoria_row, childViewGroup, false);
    return new SubcategoriaViewHolder(view);
}

@Override
public CategoriaViewHolder onCreateParentViewHolder(ViewGroup parentViewGroup) {
    View view = mInflater.inflate(R.layout.categoria_row, parentViewGroup, false);
    return new CategoriaViewHolder(view);
}

@Override
public void onBindParentViewHolder(CategoriaViewHolder parentViewHolder, int position, ParentListItem parentListItem) {
    Categoria categoria = (Categoria) parentListItem;
    parentViewHolder.bind(categoria);
 }

@Override
public void onBindChildViewHolder(SubcategoriaViewHolder childViewHolder, int position, Object childListItem) {
    SubCategoria subCategoria = (SubCategoria) childListItem;
    childViewHolder.bind(subCategoria);
}


}

Screen of the problem

Thanks in advance guys.

  • There seems to be no errors for me, you’ve already put a Log to see if your list is not coming empty from DAO?

  • Already, the problem itself is not the list, because it comes populated, the Fragment is working, only the Recycler is not being populated (I will post the Adapter) @Luiz

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.