使用 FragmentPagerAdapter 刷新片段中的数据
Refreshing Data in a fragment using FragmentPagerAdapter
我的寻呼机中有三个片段,我正在使用 Bundle 添加一些字符串
在我的适配器中实例化每个片段。我正在使用这些字符串来过滤掉我从 database.I 获取的数据 尝试简单地使用 notifydatasetchanged() 方法,但它也不起作用 我尝试在下面的 querysubmit() 方法中单独实例化适配器,但它也不起作用。我找不到任何解决方案,有人通过适配器将信息传递给片段以刷新它们。
所以基本上这就是我正在做的
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// some code here basically variables
// filterString is what i am passing to adapter to filter data
pager = (ViewPager) view.findViewById(R.id.Projectviewpager);
adapter = new
ProjectTabsAdapter(getChildFragmentManager(),filterString,getContext());
pager.setAdapter(adapter);
tabs.setupWithViewPager(pager);
// This is where i want to send string to refresh the fragment in pager.
searchView.setOnQueryTextListener(new
MaterialSearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
filterString = query;
// i want to send the string to fragment here
}
}
我的适配器
public class ProjectTabsAdapter extends FragmentPagerAdapter {
private Context mContext ;
private String mSkill;
private String tabTitles[] = new String[]{"tab1", "tab2
", "tab3"};
public ProjectTabsAdapter( FragmentManager fm,String skill,Context
context) {
super(fm);
mSkill = skill;
mContext= context;
}
@Override
public Fragment getItem(int position) {
// sending bundle to each fragment
if (position == 0) {
Tab1 fragment = new Tab1();
Toast.makeText(mContext, "Filter String"+ mSkill,
Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString("skill_set", mSkill);
fragment.setArguments(bundle);
return fragment;
} else if (position == 1){
return new tab2();
} else if (position == 2){
return new tab3();
} else {
return null;
}
}
public int getCount() {
return 3;
}
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
您可以在适配器和选项卡中定义 public 刷新方法:
public class ProjectTabsAdapter extends FragmentPagerAdapter {
private Context mContext ;
private String mSkill;
private Tab1 fragment;
private String tabTitles[] = new String[]{"tab1", "tab2
", "tab3"};
public ProjectTabsAdapter( FragmentManager fm,String skill,Context
context) {
super(fm);
mSkill = skill;
mContext= context;
}
public void refreshTab1(String skill) {
fragment.refreshSkill(skill); //define public void refreshSkill(String skill) in the Tab1 fragment
}
@Override
public Fragment getItem(int position) {
// sending bundle to each fragment
if (position == 0) {
fragment = new Tab1();
Toast.makeText(mContext, "Filter String"+ mSkill,
Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString("skill_set", mSkill);
fragment.setArguments(bundle);
return fragment;
} else if (position == 1){
return new tab2();
} else if (position == 2){
return new tab3();
} else {
return null;
}
}
public int getCount() {
return 3;
}
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
然后在你的监听器中调用刷新方法:
searchView.setOnQueryTextListener(new
MaterialSearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
filterString = query;
// i want to send the string to fragment here
adapter.refreshTab1(filterString);
}
}
我的寻呼机中有三个片段,我正在使用 Bundle 添加一些字符串 在我的适配器中实例化每个片段。我正在使用这些字符串来过滤掉我从 database.I 获取的数据 尝试简单地使用 notifydatasetchanged() 方法,但它也不起作用 我尝试在下面的 querysubmit() 方法中单独实例化适配器,但它也不起作用。我找不到任何解决方案,有人通过适配器将信息传递给片段以刷新它们。
所以基本上这就是我正在做的
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// some code here basically variables
// filterString is what i am passing to adapter to filter data
pager = (ViewPager) view.findViewById(R.id.Projectviewpager);
adapter = new
ProjectTabsAdapter(getChildFragmentManager(),filterString,getContext());
pager.setAdapter(adapter);
tabs.setupWithViewPager(pager);
// This is where i want to send string to refresh the fragment in pager.
searchView.setOnQueryTextListener(new
MaterialSearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
filterString = query;
// i want to send the string to fragment here
}
}
我的适配器
public class ProjectTabsAdapter extends FragmentPagerAdapter {
private Context mContext ;
private String mSkill;
private String tabTitles[] = new String[]{"tab1", "tab2
", "tab3"};
public ProjectTabsAdapter( FragmentManager fm,String skill,Context
context) {
super(fm);
mSkill = skill;
mContext= context;
}
@Override
public Fragment getItem(int position) {
// sending bundle to each fragment
if (position == 0) {
Tab1 fragment = new Tab1();
Toast.makeText(mContext, "Filter String"+ mSkill,
Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString("skill_set", mSkill);
fragment.setArguments(bundle);
return fragment;
} else if (position == 1){
return new tab2();
} else if (position == 2){
return new tab3();
} else {
return null;
}
}
public int getCount() {
return 3;
}
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
您可以在适配器和选项卡中定义 public 刷新方法:
public class ProjectTabsAdapter extends FragmentPagerAdapter {
private Context mContext ;
private String mSkill;
private Tab1 fragment;
private String tabTitles[] = new String[]{"tab1", "tab2
", "tab3"};
public ProjectTabsAdapter( FragmentManager fm,String skill,Context
context) {
super(fm);
mSkill = skill;
mContext= context;
}
public void refreshTab1(String skill) {
fragment.refreshSkill(skill); //define public void refreshSkill(String skill) in the Tab1 fragment
}
@Override
public Fragment getItem(int position) {
// sending bundle to each fragment
if (position == 0) {
fragment = new Tab1();
Toast.makeText(mContext, "Filter String"+ mSkill,
Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString("skill_set", mSkill);
fragment.setArguments(bundle);
return fragment;
} else if (position == 1){
return new tab2();
} else if (position == 2){
return new tab3();
} else {
return null;
}
}
public int getCount() {
return 3;
}
public CharSequence getPageTitle(int position) {
return tabTitles[position];
}
}
然后在你的监听器中调用刷新方法:
searchView.setOnQueryTextListener(new
MaterialSearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
filterString = query;
// i want to send the string to fragment here
adapter.refreshTab1(filterString);
}
}