How to update fragment in viewpage file through another action

I have a situation for my Android application. Scenario: I use a viewpage adapter, I have 3 pages, which are actually fragments. My problems are that I want to update them from the dialog activity. the list from whose onclick event I want to update the fragment.

First of all, it is possible, if so, please make a chip and help me.

Greetings Gaurav

This is my view page adapter.

/* * Copyright (C) 2012 Gaurav Rawat * */ package com.punksrant.ifinddryday.adapter; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import com.punksrant.ifinddryday.fragment.AllFragment; import com.punksrant.ifinddryday.fragment.HomeFragment; import com.punksrant.ifinddryday.fragment.SynchFragment; import com.punksrant.ifinddryday.utils.IFindDryDaysConstants; import com.viewpagerindicator.TitleProvider; public class ViewPagerAdapter extends FragmentPagerAdapter implements TitleProvider { private static String[] titles = IFindDryDaysConstants.titles; public ViewPagerAdapter(FragmentManager fm) { super(fm); } @Override public String getTitle(int position) { return titles[position % titles.length].toUpperCase(); } @Override public int getCount() { return titles.length; } @Override public Fragment getItem(int position) { Fragment fragment; switch (position) { case 0: fragment = HomeFragment.newInstance(); break; case 1: fragment = AllFragment.newInstance(); break; case 2: fragment = SynchFragment.newInstance(titles[position % titles.length]); break; default: fragment = HomeFragment.newInstance(); } return fragment; } } ' and in this I want to refresh the home fragment from my dialog class '/* * Copyright (C) 2012 Gaurav Rawat * */ package com.punksrant.ifinddryday.activity; import roboguice.inject.ContentView; import roboguice.inject.InjectView; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockFragmentActivity; import com.google.inject.Inject; import com.punksrant.ifinddryday.model.Coordinates; import com.punksrant.ifinddryday.utils.DryDayJSONResParser; import com.punksrant.ifinddryday.utils.IFindDryDayViewUtils; @ContentView(R.layout.statedialog) public class Dialog extends RoboSherlockFragmentActivity { @InjectView(R.id.dialogList) ListView listView; @Inject SharedPreferences preferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); String[] states = DryDayJSONResParser.getSupportedStates(); listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, states)); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String state = ((TextView) view).getText().toString(); Coordinates coordinates = IFindDryDayViewUtils .getCoordinatesFromPreferences(preferences); coordinates.setState(state); IFindDryDayViewUtils.updateCityFromLocation(coordinates, preferences); finish(); } }); } }, 
+6
source share
1 answer

(loans to blodeible rui.araujo) There are several ways to achieve this.

The first option is simpler, but the bit is more inefficient.

Override getItemPosition in the PagerAdapter as follows:

 public int getItemPosition(Object object) { return POSITION_NONE; } 

Thus, when you call notifyDataSetChanged() , the view pager deletes all views and reloads them all. Thus, a reboot effect is obtained.

The second option proposed by alvarolb is the setTag() method in instantiateItem() when creating a new view. Then, instead of notifyDataSetChanged() you can use findViewWithTag() to find the view you want to update.

The second approach is very flexible and high performance. Kudos to alvarolb for original research.

+1
source

Source: https://habr.com/ru/post/914007/


All Articles