>我必须请您帮助从滑动选项卡布局中的片段调用方法。我检查了很多答案,但它们取决于我的项目不包括它们的 id 或标签。我使用这个:https://github.com/codepath/android_guides/wiki/Google-Play-Style-Tabs-using-SlidingTabLayout我有两个活动(MainActivity,它向SecondActivity发送两种数据)。第二活动包括带有 3 个片段的选项卡。在第一个片段(名为 Contrs)中,我看到收到的数据(一种),但我想在我的操作栏中制作按钮,以更改第二种数据的结果。它适用于没有片段的活动,但不适用于片段。这是代码:
主要活动:
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);
...}
第二项活动:
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;
}
控制:
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;
}
而这种方法(alt)我想在我的第二活动中使用。
您可以使用事件总线,它更简单
http://square.github.io/otto/
将片段注册到事件
并发送了来自活动的消息。
在您的情况下
总线提供商.java
public final class BusProvider {
private static final Bus BUS = new Bus();
public static Bus getInstance() {
return BUS;
}
private BusProvider() {
// No instances.
}
}
点击项目事件.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
}
...
}