Implementation of recyclerView inside tabLayout

0

I would like help solving the following problem, I know it may be simple for many but for me it is still a bit tricky then.

I have a simple Android project with 3 tabs, being 1,2,3. I use a different fragment for each of them, the one in fragment 1 would like to implement a recyclerView ... in which of the classes should I implement the reference variables for RecyclerView? in the Main or fragment class that I want to appear in RecyclerView? Thanks for helping.

I have 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;
        }
    }

}

Fragment classes

  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 template class for 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);
        }


    }
}
    
asked by anonymous 20.03.2016 / 07:29

1 answer

0

The recyclerView must be added in the View in which you will display. Since you will display one of the 3 fragments, then you should add recyclerview to 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.

    
21.03.2016 / 04:19