gpt4 book ai didi

android - 如何在 ViewPager 中刷新 fragment

转载 作者:行者123 更新时间:2023-12-04 00:35:05 26 4
gpt4 key购买 nike

我有一个 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;
}
}

这是我的选项卡 C 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);
}

}

这是我的选项卡 C:
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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com