滑动标签布局调用方法

Sliding Tab Layout Call Method

我不得不向您寻求有关从滑动选项卡布局中的片段调用方法的帮助。我检查了很多答案,但它们取决于我的项目不包含的 id 或标签。我用这个: https://github.com/codepath/android_guides/wiki/Google-Play-Style-Tabs-using-SlidingTabLayout 我有两个活动(将两种数据发送到 SecondActivity 的 MainActivity)。 SecondActivity 包括带有 3 个片段的选项卡。在第一个片段(名为 Contrs)中,我看到了接收到的数据(一种),但我想在我的操作栏中创建一个按钮,它会改变第二种数据的结果。它在 activity 中工作,没有片段,但没有片段。这是代码:

主要活动:

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;
}

我想在我的 SecondActivity 中使用这个方法 (alt)。

可以使用事件总线,比较简单

http://square.github.io/otto/

将您的片段注册到活动中

并从 activity 发送消息。

你的情况

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
}

activity :

@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
  }

  ...
}