作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 MainActivity
和一个 HomeTabs
三个标签(A B C)都是Fragment
.我设置了 onRefresh
在选项卡 C 上。当我触发时 onRefresh
它的观点已经消失。这是为什么?我无法解决它。
任何帮助,将不胜感激。提前致谢。
这是我的 HomeTabs
:
public class HomeTabs extends Fragment {
private Context context;
private TabLayout tabLayout;
private int[] imageResId = {R.drawable.input_icon_color,
R.drawable.note_icon_color,
R.drawable.analytics_icon_color};
public HomeTabs() {
// Required empty public constructor
}
public static HomeTabs newInstance() {
return new HomeTabs();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home_tabs, container, false);
tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.tab1)));
tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.tab2)));
tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.tab3)));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPagerDataReport);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(viewPagerAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.getTabAt(0).setIcon(R.drawable.input_icon);
tabLayout.getTabAt(1).setIcon(imageResId[1]);
tabLayout.getTabAt(2).setIcon(imageResId[2]);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());/
int id = tab.getPosition();
if (id == 0) {
tabLayout.getTabAt(0).setIcon(R.drawable.input_icon);
tabLayout.getTabAt(1).setIcon(imageResId[1]);
tabLayout.getTabAt(2).setIcon(imageResId[2]);
}
if (id == 1) {
tabLayout.getTabAt(0).setIcon(imageResId[0]);
tabLayout.getTabAt(1).setIcon(R.drawable.note_icon);
tabLayout.getTabAt(2).setIcon(imageResId[2]);
}
if (id == 2) {
tabLayout.getTabAt(0).setIcon(imageResId[0]);
tabLayout.getTabAt(1).setIcon(imageResId[1]);
tabLayout.getTabAt(2).setIcon(R.drawable.analytics_icon);
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
// My tab C position is 2 , when i refresh that i want to show tab C
if (getActivity().getIntent().hasExtra("refresh")){
tabLayout.getTabAt(2).select();
}
return view;
}
}
Fragment
关于刷新:
@Override
public void onRefresh() {
refreshLayout.setRefreshing(false);
MainActivity mainActivity = (MainActivity) getActivity();
mainActivity.refreshFragment();
}
MainActivity
:
public class MainActivity extends AppCompatActivity {
private FrameLayout frameLayout;
private Toolbar toolBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolBar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolBar);
getSupportActionBar().setTitle("");
toolBar.setTitle(R.string.myAppName);
toolBar.setNavigationIcon(R.drawable.medical_history32);
toolBar.setTitleTextColor(Color.WHITE);
frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
if (savedInstanceState == null) {
switchFragment(HomeTabs.newInstance());
}
}
public void switchFragment(Fragment fragment) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frameLayout, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
new DialogHandler(this).quickDialog(
getResources().getString(R.string.quick),
getResources().getString(R.string.confirm),
getResources().getString(R.string.cancel),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
}
return super.onKeyDown(keyCode, event);
}
public void refreshFragment() {
//switchFragment(HomeTabs.newInstance());
Intent intent = new Intent(this,MainActivity.class);
intent.putExtra("refresh",true);
startActivity(intent);
}
}
public class MyLineChart extends Fragment implements SwipeRefreshLayout.OnRefreshListener{
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_my_line_chart, container, false);
//................set my data
return view;
}
@Override
public void onRefresh() {
refreshLayout.setRefreshing(false);
}
最佳答案
有很多方法可以做到。
如果您使用的是 ViewPager
找到最佳解决方案 here .
您必须实现(覆盖)getItemPosition
页面适配器中返回的方法(位置不变)
@Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
mPagerAdapter.notifyDataSetChanged();
关于android - 如何在 ViewPager 中刷新 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44796377/
我是一名优秀的程序员,十分优秀!