Buenas Tardes,Estoy desarrollando una app con tabs y fragments,El problema que tengo es que al iniciar la app el recyclerView que esta dentro de una fragment no me muestra nada al principio,pero si me muevo hacia el ultimo tabs y vuelvo al tab que tiene el recyclerView recien ahi es como que reacciona ,Les envio el codigo para ver si me pueden ayudar
Este es el que carga los tabs y los fragments
package com.example.juego.frasear.activities;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.widget.TextView;
import com.example.juego.frasear.R;
import com.example.juego.frasear.adapters.adapterPrincipal;
import com.example.juego.frasear.fragments.homeFragment;
import com.example.juego.frasear.fragments.jugadoresFragment;
import com.example.juego.frasear.fragments.perfilFragment;
import com.example.juego.frasear.fragments.rankingFragment;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
/**
* Created by franco on 26/08/16.
*/
public class principal extends AppCompatActivity {
private Toolbar toolbar,toolbarJugadores,toolbarMiPerfil;
private TabLayout tabLayout;
private TextView toolbar_title,toolbarJug_title,
toolbarMiPerfil_title;
// that will host the section contents.
private ViewPager viewPager;
private Typeface font_roboto_light,font_bigboss,font_frase_menu,font_gametime;
String email_usuario;
//controlador c=new controlador();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_principal);
font_gametime = Typeface.createFromAsset(getAssets(), "fonts/Game_Time.ttf");
toolbar = (Toolbar) findViewById(R.id.toolbar);
//toolbar.setTitle("Frasear");
toolbar_title = (TextView) findViewById(R.id.toolbar_title);
toolbar_title.setTypeface(font_gametime);
toolbar.hideOverflowMenu();
setSupportActionBar(toolbar);
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
if(user==null)
{
irAlogin();
}
if(user!=null) {
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
// sectionsPagerAdapter that is used as the adapter for a ViewPager.
// Set up the ViewPager with the sections adapter.
viewPager = (ViewPager) findViewById(R.id.viewpager);
// viewPager.addOnPageChangeListener(pagerChangeListener());
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
// tabLayout.setupWithViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
} // fin oncreate
private void irAlogin()
{
Intent intent = new Intent(this, login.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
private void setupTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icono_home, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabOne);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icono_jugadores, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icono_ranking, 0, 0);
tabLayout.getTabAt(2).setCustomView(tabThree);
TextView tabFour = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabFour.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.icono_perfil, 0, 0);
tabLayout.getTabAt(3).setCustomView(tabFour);
}
private void setupViewPager(ViewPager viewPager) {
adapterPrincipal adapter = new adapterPrincipal(getSupportFragmentManager());
//ViewPagerAdapter adapter =new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new homeFragment(),"HOME" );
adapter.addFragment(new jugadoresFragment(), "PLAYERS");
adapter.addFragment(new rankingFragment(), "RANKING");
adapter.addFragment(new perfilFragment(), "PERFIL");
// mostrarToolbarsHome();
//adapter.esconderToolbars();
viewPager.setAdapter(adapter);
}
/* public ViewPager.OnPageChangeListener pagerChangeListener() {
return new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if(tabLayout.getTabAt(1).isSelected()) {
jugadoresFragment j =new jugadoresFragment();
}
}
@Override
public void onPageSelected(int position) {
if(tabLayout.getTabAt(1).isSelected()) {
jugadoresFragment j =new jugadoresFragment();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
};
}*/
/* public void mostrarToolbarsHome() {
toolbarJugadores.setVisibility(View.GONE);
toolbarMiPerfil.setVisibility(View.GONE);
toolbar.setVisibility(View.VISIBLE);
}
public void mostrarToolbarsJugadores()
{
toolbarJugadores.setVisibility(View.VISIBLE);
toolbarMiPerfil.setVisibility(View.GONE);
toolbar.setVisibility(View.GONE);
}
public void mostrarToolbarMiPerfil()
{
toolbarMiPerfil.setVisibility(View.VISIBLE);
toolbarJugadores.setVisibility(View.GONE);
toolbar.setVisibility(View.GONE);
}
*/
}
Luego tenemos el fragment del recyclerView
package com.example.juego.frasear.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.juego.frasear.R;
import com.example.juego.frasear.adapters.recyclerAdapter;
import com.example.juego.frasear.models.controlador;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import java.util.ArrayList;
/**
* Created by franco on 27/08/16.
*/
public class jugadoresFragment extends Fragment {
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
//ArrayAdapter<String> adaptadorListaAmigos;
controlador c;
//principal p;
// DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ArrayList<String> listaNombres;
ArrayList<String> listaUrl;
RecyclerView recyclerView;
//DataSnapshot data;
//private SwipeRefreshLayout refreshLayout;
public jugadoresFragment() {
c=new controlador();
// Required empty public constructor
//c.getNombreAsyncronicos();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listaNombres=c.getAmigosNombres(user.getEmail());
listaUrl=c.getAmigosUrl(user.getEmail());
String hola="hola";
//leadsNames=c.getAmigos();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View v=inflater.inflate(R.layout.fragment_jugadores, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
//recyclerView.setHasFixedSize(true);
recyclerView.setNestedScrollingEnabled(false);
LinearLayoutManager llmanager = new LinearLayoutManager(v.getContext());
llmanager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llmanager);
recyclerAdapter myAdapter = new recyclerAdapter(v.getContext(),listaNombres,listaUrl);
myAdapter.notifyDataSetChanged();
recyclerView.setAdapter(myAdapter);
return v;
}
}
Luego la clase del recyclerView
package com.example.juego.frasear.adapters;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.juego.frasear.R;
import java.util.ArrayList;
import jp.wasabeef.glide.transformations.CropCircleTransformation;
/**
* Created by franco on 12/09/16.
*/
public class recyclerAdapter extends RecyclerView.Adapter<recyclerAdapter.MyViewHolder> {
private ArrayList<String> items;
private Context context;
private ArrayList<String> imagenes;
public recyclerAdapter(Context context, ArrayList<String> items,ArrayList<String> imagenes) {
this.context = context;
this.items= items;
this.imagenes=imagenes;
}
@Override
public int getItemCount() {
return items.size();
}
@Override
public void onBindViewHolder(MyViewHolder itemsViewHolder, int i) {
itemsViewHolder.vTitle.setText(items.get(i));;
Glide.with(this.context).load(imagenes.get(i))
.bitmapTransform(new CropCircleTransformation(context))
.into(itemsViewHolder.vImagen);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.fragment_recyclerview, viewGroup, false);
return new MyViewHolder(itemView);
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
// Campos de la lista
public TextView vTitle;
public ImageView vImagen;
public MyViewHolder(View v) {
super(v);
vTitle = (TextView) v.findViewById(R.id.title);
vImagen= (ImageView) v.findViewById(R.id.imagenAmigos);
}
}
}
Luego tengo la clase controlador
package com.example.juego.frasear.models;
import android.os.AsyncTask;
import com.example.juego.frasear.databases.catalogo_amigos;
import com.example.juego.frasear.databases.catalogo_asynco;
import com.example.juego.frasear.databases.catalogo_usuarios;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
/**
* Created by franco on 06/09/16.
*/
public class controlador {
public boolean guardarUsuario(jugador jug) {
catalogo_usuarios cat=new catalogo_usuarios();
boolean rta=cat.guardarUsuario(jug);
return rta;
}
/*public void buscarUsuario()
{
catalogo_usuarios cat= new catalogo_usuarios();
cat.buscarUsuario();
}*/
public ArrayList<String> getAmigosNombres(String email)
{
catalogo_amigos cat=new catalogo_amigos();
ArrayList<String> rtas=cat.getAmigosNombres(email);
//String hola="asdsa";
return rtas;
}
public ArrayList<String> getAmigosUrl(String email)
{
catalogo_amigos cat=new catalogo_amigos();
ArrayList<String> rtas=cat.getAmigosUrl(email);
return rtas;
}
public void guardarAmigos(ArrayList<amigo> listaAmigos,String email)
{
catalogo_amigos cat=new catalogo_amigos();
cat.guardarAmigos(listaAmigos,email);
}
public void getNombreAsyncronicos()
{
catalogo_asynco a=new catalogo_asynco();
AsyncTask<String, Void, ArrayList<String>> array=a.execute("[email protected]");
try {
ArrayList<String> as= array.get();
String hola="hola";
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
luego tengo la clase catalogo_amigos que es donde hace la interaccion con firebase
package com.example.juego.frasear.databases;
import android.os.Handler;
import com.example.juego.frasear.models.amigo;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.Iterator;
/**
* Created by franco on 16/09/16.
*/
public class catalogo_amigos {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
ArrayList<String> listaNombres = new ArrayList<>();
ArrayList<String> listaUrl = new ArrayList<>();
Long nro;
Handler mhandler,mhandler1;
public catalogo_amigos()
{
mhandler=new Handler();
mhandler1=new Handler();
}
public void guardarAmigos(ArrayList<amigo> lista, String email) {
ArrayList<String> lista_name = new ArrayList<>();
ArrayList<String> lista_url = new ArrayList<>();
String delimitadores = "@";
String[] emailCortado = email.split(delimitadores);
/*for(int i=0;i<lista.size();i++)
{
amigo a=lista.get(i);
lista_name.add(i,a.getName());
}
//String nro=String.valueOf(i);
DatabaseReference amig = ref.child("amigos").child(emailCortado[0]);;
amig.setValue(lista_name);
for(int i=0;i<lista.size();i++)
{
amigo a=lista.get(i);
lista_url.add(i,a.getUrl());
}
DatabaseReference url = ref.child("imagenes_amigos").child(emailCortado[0]);
url.setValue(lista_url);
*/
DatabaseReference am = ref.child("amigos").child(emailCortado[0]);
am.setValue(lista);
}
public ArrayList<String> getAmigosNombres(final String email) {
//Aquí ejecutamos nuestras tareas costosas
String delimitadores = "@";
String[] emailCortado = email.split(delimitadores);
DatabaseReference amigos = ref.child("amigos").child(emailCortado[0]);
amigos.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterable<DataSnapshot> i = dataSnapshot.getChildren();
Iterator<DataSnapshot> radiatosIterator = i.iterator();
while (radiatosIterator.hasNext()) {
DataSnapshot c = radiatosIterator.next().child("name");
String name = c.getValue(String.class);
listaNombres.add(name);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return listaNombres;
}
public ArrayList<String> getAmigosUrl(final String email) {
mhandler1.post(new Runnable() {
@Override
public void run() {
String delimitadores = "@";
String[] emailCortado = email.split(delimitadores);
DatabaseReference amigos = ref.child("amigos").child(emailCortado[0]);
amigos.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterable<DataSnapshot> i = dataSnapshot.getChildren();
Iterator<DataSnapshot> radiatosIterator = i.iterator();
while (radiatosIterator.hasNext()) {
DataSnapshot c = radiatosIterator.next().child("url");
String url = c.getValue(String.class);
listaUrl.add(url);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
return listaUrl;
}
}
Desde ya muchas gracias,Espero su respuesta