Implementation of recyclerView within tabLayout

Asked

Viewed 290 times

0

I would like help to solve the following problem, I know it may be simple for many but for me it is still a little complicated, so.

I have a simple Android project with 3 tabs, being them 1,2,3. I use a different fragment for each of them, or in fragment 1 I would like to implement a recyclerView...in which class should I implement the reference variables for Recyclerview? on Main or in the fragment class I wish to appear to Recyclerview? I thank anyone who can help.

I got the Main class

public class MainActivity extends AppCompatActivity {


    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_padrao);
        setSupportActionBar(toolbar);

        mFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = mFragmentManager.beginTransaction();
        mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();

    }
}

I have the Tabfragment class

public class TabFragment extends Fragment {

    public static TabLayout tabLayout;
    public static ViewPager viewPager;
    public static int int_itens = 3 ;

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

            View x =  inflater.inflate(R.layout.tab_layout,null);
            tabLayout = (TabLayout) x.findViewById(R.id.tabs);
            viewPager = (ViewPager) x.findViewById(R.id.viewpager); 

        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                    tabLayout.setupWithViewPager(viewPager);
                   }
        });

        return x;
    }

    class MyAdapter extends FragmentPagerAdapter{

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position)
        {
          switch (position){
              case 0 : return new PrimeiroFragmento();
              case 1 : return new SegundoFragmento();
              case 2 : return new TerceiroFragmento();
          }
        return null;
        }

        @Override
        public int getCount() {

            return int_itens;

        }

        @Override
        public CharSequence getPageTitle(int position) {

            switch (position){
                case 0 :
                    return "Primeira";
                case 1 :
                    return "Segunda";
                case 2 :
                    return "Terceira";
            }
                return null;
        }
    }

}

Classes of fragments

  public class PrimeiroFragmento extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.primeiro_fragmento,null);

    }

}


public class SegundoFragmento extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.segundo_fragmento,null);
    }
}



public class TerceiroFragmento extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.terceiro_fragmento,null);
    }
}

Now here I have the model class to recyclerView

public class Dataprovider {

    public Dataprovider(int img_res, String f_name, String quant_de_mensagem){
        this.setImg_res(img_res);
        this.setQuant_de_mensagem(quant_de_mensagem);
        this.setF_name(f_name);

    }
    private int img_res;
    private String f_name, quant_de_mensagem;

    public void setImg_res(int img_res) {
        this.img_res = img_res;
    }

    public int getImg_res() {
        return img_res;
    }

    public String getF_name() {
        return f_name;
    }

    public String getQuant_de_mensagem() {
        return quant_de_mensagem;
    }

    public void setF_name(String f_name) {
        this.f_name = f_name;
    }

    public void setQuant_de_mensagem(String quant_de_mensagem) {
        this.quant_de_mensagem = quant_de_mensagem;
    }
}

and finally the adapter.

public class ReclyclerAdapter extends RecyclerView.Adapter<ReclyclerAdapter.RecyclerViewHorder>{

    private ArrayList<Dataprovider> arrayList = new ArrayList<Dataprovider>();

    public ReclyclerAdapter(ArrayList<Dataprovider> arrayList){

        this.arrayList = arrayList;

    }


    @Override
    public RecyclerViewHorder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);

        RecyclerViewHorder recyclerViewHorder = new RecyclerViewHorder(view);
        return recyclerViewHorder;
    }

    @Override
    public void onBindViewHolder(RecyclerViewHorder holder, int position) {
        Dataprovider dataprovider = arrayList.get(position);
        holder.imageView.setImageResource(dataprovider.getImg_res());
        holder.f_name.setText(dataprovider.getF_name());
        holder.quant_de_mensagem.setText(dataprovider.getQuant_de_mensagem());

    }

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

    public static class RecyclerViewHorder extends RecyclerView.ViewHolder{
        ImageView imageView;
        TextView f_name, quant_de_mensagem;


        public RecyclerViewHorder(View view) {
            super(view);
            imageView = (ImageView)view.findViewById(R.id.img);
            f_name = (TextView)view.findViewById(R.id.f_name);
            quant_de_mensagem = (TextView)view.findViewById(R.id.quant_de_mensagem);
        }


    }
}

1 answer

0


The recyclerView should be added to the View in which you will display. As you will display one of the 3 fragments, then you should add the recyclerview in one of these 3. The same principle works for the components. You should always add a component ( a button for example ) on the screen you would like to show/ use.

  • Thank you for trying to help. I agree with your description but my doubt is regarding the implementation of the code in java, it goes in the Main class or in the fragment class where I would like to show the Recyclerview?

  • Come on, activities and Fragments have a layout. You can only reference a component that exists in this layout you set. In the case of Activity the layout is passed in setContentView within the onCreate() method. In Fragment this occurs in onCreateView(). In your case you should add to Fragment.

  • Thanks I’ll see if I can.

  • I cannot implement the codes that handle the functioning of the Recyclerview class.

  • In the fragment class, I declared and started the variables that treat recyclerView

  • RecyclerView recyclerView;&#xA; RecyclerView.Adapter adapter;&#xA; RecyclerView.LayoutManager layoutManager;&#xA; String [] quant_de_mensagem, f_name;&#xA; int [] img_res = {R.drawable.cbjr};&#xA; ArrayList<Dataprovider> arrayList = new ArrayList<Dataprovider>();

  • https://gist.github.com/jcaiqueoliveira/d13d0e6965a1717d7a27

  • Wlw partner, I’ll send an example to github

  • Dear my brother you are fucking msObrigadão!!! look at the error, was returning Return Inflater.inflate(R.layout.segundo_fragmento,null);

  • Now I have another problem testing the app list takes time to appear and when it appears, if I rotate the device to form horizontal the list disappears and it takes to return. .. has any hint?

  • when rotating the device it redesigns the screens. On the slowness ask another question that this is already beyond the scope of initial doubt

  • Ta msm so thank you.

Show 7 more comments

Browser other questions tagged

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