viewpager - tabs in android studio example
¿Cómo actualizar recyclerview en la segunda pestaña en viewpager desde la primera pestaña? (2)
Hay algunos cambios necesarios en su código. Si revisas Fragment''s LifeCycle , entenderás cómo funciona con Tabular View.
Echa un vistazo al Código que he hecho desde el código anterior.
public class CurrentTimetables extends Fragment {
RecyclerView recyclerView;
static MusicRecyclerAdapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Timetables> list;
Context context;
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.currenttimetable, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView = (RecyclerView) view.findViewById(R.id.timetablerecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
tabBackground = new TabBackground(CurrentTimetables.this, list, spinnerItems, adapter, spinnerAdapter);
tabBackground.populateConditionsList();
// If you have another Component add here using finViewById() and also you can do
// other process here. I just used setUserVisibleHint because it will execute when
// fragment will be visible to user and it will stop over calling to web service.
/**
* Populate Recyclerview data if setUserVisibleHint do not work.
*/
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
populate();
}
}
public void populate() {
String dbURL = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
RequestQueue requestQueue = Volley.newRequestQueue(context);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(dbURL, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray jsonArray) {
if (!list.isEmpty()) {
list.clear();
}
try {
list = new ArrayList<Timetables>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int timetableID = Integer.parseInt(jsonObject.getString("timetableID"));
String timetableName = jsonObject.getString("timetableName");
String subjectName = jsonObject.getString("subjectName");
Timetables timetables = new Timetables(timetableID, timetableName, subjectName);
list.add(timetables);
adapter = new MusicRecyclerAdapter(list, context);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
jsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(5 * 1000, 1, 1.0F));
jsonArrayRequest.setShouldCache(false);
requestQueue.add(jsonArrayRequest);
}
@Override
public void onResume() {
super.onResume();
populate();
}
}
Es posible que haya olvidado algo de su código y que tenga que agregarlo.
Uno de los problemas que estoy enfrentando actualmente es que si un usuario agrega algo a su horario, debe actualizarse en la vista del recyclerview
en la segunda pestaña Current Timetable
. Sin embargo, nunca se actualiza y el usuario tiene que volver al menú principal, volver a la actividad Timetables
y luego seleccionar Current Timetable
. Creo que es algo relacionado con mi ViewPager
y su adaptador. Simplemente no puedo ver dónde me estoy equivocando y estoy seguro de que es algo simple que me estoy perdiendo. Por favor, podrías modificar mi código para que funcione
Aquí está mi código: La clase viewpager
public class Timetables extends AppCompatActivity{
TabLayout tabLayout;
ViewPager viewPager;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timetables);
viewPager = (ViewPager) findViewById(R.id.viewPager);
TimetablesAdapter timetablesAdapter = new TimetablesAdapter(getSupportFragmentManager(), Timetables.this);
viewPager.setAdapter(timetablesAdapter);
viewPager.setOffscreenPageLimit(2);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
//viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
//viewPager.setCurrentItem(tab.getPosition());
}
});
}
}
La clase fragmentpageradapter:
public class TimestableAdapter extends FragmentPagerAdapter{
String tabNames[] = new String[] {"All timetables", "Current Timetable", "Expired"};
Context context;
public TimestableAdapter(FragmentManager fragmentManager, Context context){
super(fragmentManager);
this.context = context;
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new AllTimetables();
case 1:
return new CurrentTimetables();
case 2:
return new ExpiredTimetables();
default:
return null;
}
}
@Override
public int getCount() {
return tabNames.length;
}
@Override
public CharSequence getPageTitle(int position) {
return tabNames[position];
}
}
Esta es la clase CurrentTimetable
:
public class CurrentTimetables extends Fragment {
RecyclerView recyclerView;
static MusicRecyclerAdapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Timetables> list;
public CurrentTimetables(){
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.currenttimetable, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.timetablerecyclerView);
recyclerView.setHasFixedSize(true);
list = new ArrayList<Timetables>();
adapter = new MusicRecyclerAdapter(list, CurrentTimetables.this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapter);
tabBackground = new TabBackground(CurrentTimetables.this, list, spinnerItems, adapter, spinnerAdapter);
tabBackground.populateConditionsList();
inputs = new ArrayList<>();
populate();
return rootView;
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
}
@Override
public void onStart() {
super.onStart();
}
public void populate(){
String dbURL = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
RequestQueue requestQueue = Volley.newRequestQueue(fragment.getActivity());
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
(dbURL, new Response.Listener<JSONArray>(){
@Override
public void onResponse(JSONArray jsonArray) {
if(!list.isEmpty()){
list.clear();
}
try {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
int timetableID = Integer.parseInt(jsonObject.getString("timetableID"));
String timetableName = jsonObject.getString("timetableName");
String subjectName = jsonObject.getString("subjectName");
Timetables timetables = new Timetables(timetableID, timetableName, subjectName);
list.add(timetables);
adapter.notifyDataSetChanged();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}
);
requestQueue.add(jsonArrayRequest);
}
}
Realmente significaría que mucha gente podría ayudarme
Gracias
Tienes que crear una interfaz en mainActivity. Refiéralo en tus fragmentos. Es la mejor manera de comunicarse de un fragmento a otro. Por favor, consulte el siguiente enlace.
http://developer.android.com/training/basics/fragments/communicating.html
Eche un vistazo y si tiene alguna duda aquí para ayudar.