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);
}
}
}
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?
– Diomar Rodrigues
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.
– Caique Oliveira
Thanks I’ll see if I can.
– Diomar Rodrigues
I cannot implement the codes that handle the functioning of the Recyclerview class.
– Diomar Rodrigues
In the fragment class, I declared and started the variables that treat recyclerView
– Diomar Rodrigues
RecyclerView recyclerView;
 RecyclerView.Adapter adapter;
 RecyclerView.LayoutManager layoutManager;
 String [] quant_de_mensagem, f_name;
 int [] img_res = {R.drawable.cbjr};
 ArrayList<Dataprovider> arrayList = new ArrayList<Dataprovider>();
– Diomar Rodrigues
https://gist.github.com/jcaiqueoliveira/d13d0e6965a1717d7a27
– Caique Oliveira
Wlw partner, I’ll send an example to github
– Diomar Rodrigues
Dear my brother you are fucking msObrigadão!!! look at the error, was returning Return Inflater.inflate(R.layout.segundo_fragmento,null);
– Diomar Rodrigues
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?
– Diomar Rodrigues
when rotating the device it redesigns the screens. On the slowness ask another question that this is already beyond the scope of initial doubt
– Caique Oliveira
Ta msm so thank you.
– Diomar Rodrigues