[英]Refresh fragment when other fragment changes the first's fragment model
I want to refresh the fragment when the view pager change to it. 我想在视图分页器更改为该片段时刷新它。
package com.mcivisoft.rcbeam;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import com.mcivisoft.rcbeam.R;
public class FragBeamRec extends Fragment {
public static FragBeamRec newInstance() {
FragBeamRec fragment = new FragBeamRec();
return fragment;
}
public FragBeamRec() {}
private EditText tasss = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_frag_beam_rec, container, false);
tasss = (EditText)v.findViewById(R.id.txttttaasss);
tasss.setText(String.valueOf(var.asspass));
ActivityBeamRec.mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
if (position == 2) {
Fragment frg = null;
frg = getFragmentManager().findFragmentByTag("FragBeamRec");
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
@Override
public void onPageScrollStateChanged(int state) {}
});
return v;
}
}
This isn't working, can anyone tell me why. 这行不通,谁能告诉我原因。 I have been trying to refresh this fragment for three days now please help.
我一直在尝试刷新此片段三天,请帮助。
The reason I want it to refresh that I got other fragment in the same activity (tabbed activity), in the first fragment there is a button that do some calculation and put the result in a global variable, and this fragment show the value of the public variable, and because it's created before the button is clicked it show 0.0
that's why I want it to refresh so it show the current value of the global variable. 我希望刷新的原因是我在同一活动(选项卡式活动)中得到了另一个片段,在第一个片段中,有一个按钮进行一些计算并将结果放入全局变量中,并且该片段显示了公共变量,并且由于它是在单击按钮之前创建的,因此显示
0.0
,这就是为什么我要刷新它以便显示全局变量的当前值的原因。
Instead of refreshing the fragment, you should simply refresh the view. 除了刷新片段以外,您应该只刷新视图。 See the guide for communicating between fragments ( http://developer.android.com/training/basics/fragments/communicating.html ) for a tutorial.
有关教程,请参见有关片段之间通信的指南( http://developer.android.com/training/basics/fragments/communicating.html )。 The same concepts there apply here.
这里有相同的概念。
Basically you will want to notify the FragBeamRec
fragment that the calculation is complete inside the button's OnClickListener
like this 基本上,您将希望像这样在按钮的
OnClickListener
内通知FragBeamRec
片段计算已完成
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int result = 4 + 4;
fragBeamRec.onCalculationComplete(result);
}
});
with an interface 带接口
public interface OnCalcComplete {
void onCalculationComplete(int result);
}
Then in the fragment have something like 然后在片段中有类似
public class FragBeamRec extends Fragment implements OnCalcComplete {
public void onCalculationComplete(int result) {
textView.setText(result);
}
...
}
I did it by replacing private EditText tasss = null;
我通过替换
private EditText tasss = null;
by public static EditText tasss = null;
通过
public static EditText tasss = null;
this way I can access it from anywhere.and in the button on click listener FragBeamRec.tasss.setText(String.valueOf(var.asspass));
这样,我可以从任何地方访问它。在单击侦听
FragBeamRec.tasss.setText(String.valueOf(var.asspass));
中的按钮上FragBeamRec.tasss.setText(String.valueOf(var.asspass));
I think your entire implementation is shaky 我认为您的整个实施过程都很不稳定
You have FragBeamRec inside a ViewPager which is inside ActivityBeamRec. 您在ActivityBeamRec内部的ViewPager中具有FragBeamRec。
Going by this, If you have 4 fragments inside your ViewPager, chances are that FragBeamRec has never even been created when changes occur to the said data model. 这样一来,如果ViewPager内有4个片段,则可能在所述数据模型发生更改时甚至从未创建FragBeamRec。
The best implementation is fire all refresh from the ActivityBeamRec. 最好的实现是从ActivityBeamRec刷新所有内容。 Register the viewPagerChangeListener inside of the activity.
在活动内部注册viewPagerChangeListener。 You might also tie a Listener interface that is fired on the ActivityBeamRec activity when the model changes.
您还可以绑定模型更改时在ActivityBeamRec活动上触发的侦听器接口。
You can now get the Fragment from the Activity using 您现在可以使用以下方式从活动中获取片段
private Fragment getFragmentFromPager(int index) {
return getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.parentViewPager + ":" + index);
}
By passing in the Fragment index position inside the viewPager, you would get a Fragment object with which you can carryout any form of refresh 通过在viewPager中传递Fragment索引位置,您将获得一个Fragment对象,您可以使用该对象执行任何形式的刷新
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.