RecyclerView in Fragment with slide Tabs

2.6k views Asked by At

I have an application where I use slide tabs with fragment and in each fragment I have recyclerview with custom adapter, I have 5 tabs now the apps works ok while I slide, the correct data is shown, the first 4 tabs share same layout for the recyclerview adapter the 5th doesn't, now if I click the 4th tab the app crashes it says that it can't get the textview after debugging awhile I've found out that the layout passed to the adapter and the ids of the textviews don't match, so in this case, the layout for the 4th tab is passed to the adapter, but the ids of the textviews are from the 5th tab in which the layout is different, why this happens? this is how the adapter in a fragment looks:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_two, container, false);

    Display display = getActivity().getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

    DisplayMetrics metrics = getResources().getDisplayMetrics();

    List<Info> mDataset = new ArrayList<Info>();

    mDataset.add(new Info("Display", Build.DISPLAY,""));
    mDataset.add(new Info("Width", width+" px",""));
    mDataset.add(new Info("Height", height+" px",""));
    mDataset.add(new Info("Density", ""+metrics.density,""));

    if ( v != null ) {

        mRecyclerView = (RecyclerView) v.findViewById(R.id.my_recycler_view2);
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());

        mAdapter = new MyAdapter(mDataset, getContext(), "Display");
        mRecyclerView.setAdapter(mAdapter);

    }

    return v;
}

And this is the adapter, I use same adapter in each fragment:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<Info> mDataset;
private int mLayout;
private Context mContext;
private static int mTextInfoA;
private static int mTextInfoB;
private static int mTextInfoC;
private static String mFragment;

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(List<Info> mDataset, Context mContext, String mFragment) {
    this.mDataset = mDataset;
    this.mContext = mContext;
    this.mFragment = mFragment;

    if( mFragment.equals("Device") || mFragment.equals("Battery") || mFragment.equals("Display") || mFragment.equals("Ram") ) {
        this.mLayout = R.layout.cardview_layout;
        this.mTextInfoA = R.id.info_name;
        this.mTextInfoB = R.id.info_value;
    } else if( mFragment.equals("Storage") ){
        this.mLayout = R.layout.cardview_storage_layout;
        this.mTextInfoA = R.id.storage_total_value;
        this.mTextInfoB = R.id.storage_free_value;
        this.mTextInfoC = R.id.storage_textview;
    }
}

// Create new views (invoked by the layout manager)
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(mLayout, parent, false);

    ViewHolder vHolder = new ViewHolder(v);

    return vHolder;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    Info info = mDataset.get(i);
    viewHolder.mTextInfoa.setText(info.a);
    viewHolder.mTextInfob.setText(info.b);

    if (!info.c.isEmpty()) {
    if (info.c.equals("Internal Storage")) {
        viewHolder.mTextInfoc.setBackgroundColor(Color.parseColor("#2196F3"));
    } else if (info.c.equals("External Storage")) {
        viewHolder.mTextInfoc.setBackgroundColor(Color.parseColor("#F44336"));
    }
        viewHolder.mTextInfoc.setText(info.c);
}
}

@Override
public int getItemCount() {
    return mDataset.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    public TextView mTextInfoa;
    public TextView mTextInfob;
    public TextView mTextInfoc;

    public ViewHolder(View itemView) {
        super(itemView);

        mTextInfoa = (TextView) itemView.findViewById(mTextInfoA);
        mTextInfob = (TextView) itemView.findViewById(mTextInfoB);
        mTextInfoc = (TextView) itemView.findViewById(mTextInfoC);
    }

}

Clicking on the first three tabs its ok but if I'm on the 1th tab and I click directly on the 4th or 5th it crashes with that error.

My Activity OnCreate Method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);



    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

My main layout:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="scrollable" />

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

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

1

There are 1 answers

0
user3051949 On

I've fixed it myself, the problem was that the variables in the adapter were static.