我必须请求您帮助从Sliding Tab Layout中的片段调用方法。我检查了很多答案,但它们取决于我的项目不包括它们的id或标签。我用这个: javadocs for URL class 我有两个活动(MainActivity将两种数据发送到SecondActivity)。 SecondActivity包含带有3个片段的选项卡。在第一个片段(名为Contrs)中,我看到收到的数据(一种),但我想在我的操作栏中创建按钮,该按钮会更改第二种数据的结果。它在没有碎片但没有碎片的情况下工作。这是代码:
MainActivity:
public class MainActivity extends Activity { ...
public void count (View view) {
double g, x, y;
g = Double.parseDouble(a.getText().toString());
x = g*8.5;
y = g*5.5;
Intent sendingIntent = new Intent(this,SecondActivity.class);
double x1 = new BigDecimal(x).setScale(2,RoundingMode.HALF_UP).doubleValue();
double y1 = new BigDecimal(y).setScale(2,RoundingMode.HALF_UP).doubleValue();
sendingIntent.putExtra("x1", x1);
sendingIntent.putExtra("y1", y1);
startActivity(sendingIntent);
...}
SecondActivity:
public class SecondActivity extends ActionBarActivity {
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.results, menu);
MenuItem item = menu.findItem(R.id.alt);
Intent receiveIntent = this.getIntent();
y1 = receiveIntent.getDoubleExtra("y1", y1);
if (y1 == 0) {
item.setVisible(false);
} else {
item.setVisible(true);
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.alt:
**"IN HERE I WANT TO MAKE A METHOD WHICH IS IN MY FRAGMENT"**
break;
case R.id.info:
Intent intset1 = new Intent(this, A.class);
this.startActivity(intset1);
break;
case R.id.help:
Intent intset2 = new Intent(this, B.class);
this.startActivity(intset2);
return true;
default:
return super.onOptionsItemSelected(item);
}
return false;
}
Contrs:
public class Contrs extends Fragment {
...
public boolean aa = true;
public TextView vvv;
public double x1;
public double y1;
final DecimalFormat df = new DecimalFormat("0.00");
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.activity_contributions,container,false);
x1 = getActivity().getIntent().getDoubleExtra("x1", x1);
y1 = getActivity().getIntent().getDoubleExtra("y1", y1);
vvv = (TextView) v.findViewById(R.id.r1);
return v;
}
public void v1 () { // first kind of data
vvv.setText(String.valueOf(df.format(x1)));
}
public void v2 () { // second kind of data
vvv.setText(String.valueOf(df.format(y1)));
}
public void alt (){ // switch between kinds
if (aa)
v1();
else
v2();
aa = !aa;
}
我希望在SecondActivity中使用此方法(alt)。
答案 0 :(得分:0)
你可以使用事件总线,这是更简单的
将您的片段注册到活动
并从活动中发送消息。
在你的情况下
BusProvider.java
public final class BusProvider {
private static final Bus BUS = new Bus();
public static Bus getInstance() {
return BUS;
}
private BusProvider() {
// No instances.
}
}
ClickedItemEvent.java
public class ClickedItemEvent { //the name of the event
}
活动:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.alt:
BusProvider.getInstance().post(new ClickedItemEvent());
片段:
public class YourFragment extends ... {
...
@Override public void onResume() {
super.onResume();
BusProvider.getInstance().register(this);
}
@Override public void onPause() {
super.onPause();
BusProvider.getInstance().unregister(this);
}
@Subscribe public void onLocationChanged(ClickedItemEvent event) {
//do anything you want
}
...
}