我有三個片段使用tablayout。問題是我將數據保存在片段1中,並希望顯示第二片段中的數據,但第二片段在單擊或滑動到第二片段時未更新,因爲片段由於默認偏移限制1而未再次創建。當我再次重新啓動應用程序時,顯示數據。任何解決方案高度讚賞Android片段更新問題
//我的MainActivity代碼在這裏
public class MainActivity extends AppCompatActivity
{
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
R.drawable.calender,
R.drawable.favourite,
R.drawable.setting_icon
};
PagerAdapter pagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
viewPager = (ViewPager) findViewById(R.id.viewpager);
pagerAdapter = new PagerAdapter(getSupportFragmentManager(), MainActivity.this);
viewPager.setAdapter(pagerAdapter);
// viewPager.setOffscreenPageLimit(0);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setIcon(R.drawable.calender_hover);
for (int i = 0; i < tabLayout.getTabCount(); i++)
{
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(pagerAdapter.getTabView(i));
}
}
class PagerAdapter extends FragmentStatePagerAdapter
{
String tabTitles[] = new String[] { "Tab One", "Tab Two", "Tab Three", };
private int[] tabIcons = { R.drawable.tab_selector_calendar, R.drawable.tab_selector_favorite, R.drawable.tab_selector_settings
};
Context context;
public PagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return tabTitles.length;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentAyah();
case 1:
return new FragmentFavorite();
case 2:
return new FragmentSettings();
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
// return tabTitles[position];
return null;
}
public View getTabView(int position) {
View tab = LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_tab, null);
ImageView imgView = (ImageView) tab.findViewById(R.id.imgView);
imgView.setBackgroundResource(tabIcons[position]);
// TextView tv = (TextView) tab.findViewById(R.id.custom_text);
// tv.setText(tabTitles[position]);
return tab;
}
}
}
and Fragment code
//標籤1 FragmentAyah代碼在這裏在這裏
public class FragmentAyah extends Fragment
{
private RecyclerView recyclerView;
private List<Ayat> ayatList = new ArrayList<>();
private AyatAdapter ayatAdapter;
DBHelper dbHelperA;
List<AyatVariables> datalist = new ArrayList<AyatVariables>();
String formattedDate = "";
SimpleDateFormat df;
private Calendar c;
public FragmentAyah()
{
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_ayah, null);
recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view);
ayatAdapter = new AyatAdapter(ayatList,getActivity());
dbHelperA = new DBHelper(getActivity());
c = Calendar.getInstance();
df = new SimpleDateFormat("yyyy-MM-dd");
formattedDate = df.format(c.getTime());
getAllValuesFromDB();
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new SimpleDividerItemDecoration(getActivity()));
recyclerView.setAdapter(ayatAdapter);
ayatAdapter.notifyDataSetChanged();
return root;
}
private void prepareMovieData() {
// AyatVariables ayat = new AyatVariables("Surah Al-Fatiha","Ayat","1",":11","Translation11");
for (int i = 0; i < datalist.size(); i++) {
Ayat ayat = new Ayat(datalist.get(i).getID(), datalist.get(i).getSURAH_NAME(),
datalist.get(i).getTEXTS(), datalist.get(i).getSURAH(),
datalist.get(i).getAYA(), datalist.get(i).getEN_TRANSLATION(),
datalist.get(i).getDATES());
ayatList.add(ayat);
ayatAdapter.notifyDataSetChanged();
}
}
@Override
public void onDestroyView()
{
super.onDestroyView();
Log.e("onDestroyView======>","onDestroyView");
ayatAdapter.notifyDataSetChanged();
ayatList.clear();
}
public void getAllValuesFromDB()
{
try {
dbHelperA.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
datalist.clear();
datalist = dbHelperA.getDataWithAllTranslation(formattedDate);
// Log.e("datasize=========>",""+datalist.size());
dbHelperA.close();
prepareMovieData();
}
}
//標籤2 FragmentFavorite代碼
public class FragmentFavorite extends Fragment {
private RecyclerView recyclerView;
private List<FAyat> ayatList = new ArrayList<>();
private AyatFAdapter ayatAdapter;
DBHelper dbHelperA;
DBHelper2 dbHelperF;
List<AyatVariables> datalist = new ArrayList<AyatVariables>();
List<FavVariables> fVar = new ArrayList<FavVariables>();
String formattedDate = "";
SimpleDateFormat df;
private Calendar c;
public FragmentFavorite() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_favorits, null);
recyclerView = (RecyclerView) root.findViewById(R.id.frecycler_view);
ayatAdapter = new AyatFAdapter(ayatList, getActivity());
dbHelperA = new DBHelper(getActivity());
dbHelperF = new DBHelper2(getActivity());
c = Calendar.getInstance();
df = new SimpleDateFormat("yyyy-MM-dd");
formattedDate = df.format(c.getTime());
getAllValuesFromDB();
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(ayatAdapter);
ayatAdapter.notifyDataSetChanged();
return root;
}
private void prepareMovieData() {
// AyatVariables ayat = new AyatVariables("Surah Al-Fatiha","Ayat","1",":11","Translation11");
for (int i = 0; i < datalist.size(); i++) {
FAyat ayat = new FAyat(datalist.get(i).getID(), datalist.get(i).getSURAH_NAME(),
datalist.get(i).getTEXTS(), datalist.get(i).getSURAH(),
datalist.get(i).getAYA(), datalist.get(i).getEN_TRANSLATION(),
datalist.get(i).getDATES());
ayatList.add(ayat);
ayatAdapter.notifyDataSetChanged();
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
ayatList.clear();
}
public void getAllValuesFromDB()
{
getIDfromFav();
try {
dbHelperA.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 0; i<fVar.size();i++) {
// datalist = dbHelperA.getAllDataForFav(fVar.get(i).getFavID());
datalist.add(dbHelperA.getAllDataForFav(fVar.get(i).getFavID()));
Log.e("datasize=========>",""+datalist.size());
}
dbHelperA.close();
prepareMovieData();
}
public void getIDfromFav()
{
fVar.clear();
fVar = dbHelperF.getAllFav();
Log.e("fVar--size",""+fVar.size());
dbHelperF.close();
}
}
你能告訴我們你的代碼嗎? – Irshu
可以有很多解決方法來實現這一點。您可以使用廣播接收器,並在片段1發生某些更改時實施片段2中的更改。您還可以實施事件模式。您也可以編寫自己的界面,並通過使用活動,可以更改相應的片段。 –
@Yasir Tahir,你可以請示例 –