我无法从Activity调用Fragment方法

时间:2018-08-21 19:14:19

标签: java android android-studio

我有一个活动。当我单击按钮时,我想在Fragment中运行该方法。但这行不通。我尝试了很多方法,但无法正常工作。

  

我的活动;

public class MyActivity extends Activity {

Button button;
TabLayout tabLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myactivity_main);

    tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("First"));
    tabLayout.addTab(tabLayout.newTab().setText("Second"));

    viewPager= (CustomViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new MyFragmentAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
    });        

    button = (Button)findViewById(R.id.button);
    button .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            MyFragment firstfragment = (MyFragment ) getFragmentManager().findFragmentByTag(“tab1”); 

            firstfragment.MyMethod();
        }
    });
    }
}
  

MyFragmentAdapter

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.webviewapp.Fragment.PromotionFragment;
import com.webviewapp.Fragment.WebFragment;

public class MyFragmentAdapter extends FragmentPagerAdapter {

int tabCount;

public MyFragmentAdapter(FragmentManager fm, int numberOfTabs) {
    super(fm);
    this.tabCount = numberOfTabs;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            FirstFragment tab1 = new FirstFragment ();
            return tab1;
        case 1:
            SecondFragment tab2 = new SecondFragment ();
            return tab2;
        default:
            return null;
    }
}

@Override
public int getCount() {
    return tabCount;
}
}
  

FirstFragment

 public class FirstFragmentextends Fragment {

 Button fragmentbutton;

 public FirstFragment() {
    // Required empty public constructor
}


@SuppressLint("SetJavaScriptEnabled")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_web, container, false);

    fragmentbutton = (Button) view.findByViewId(R.id.fragmentbutton );
}

 public void MyMethod(){

   fragmentbutton.setVisibility(View.GONE);
}

}

很抱歉造成混乱。我要执行的操作是在按“活动”中的按钮时运行MyMethod。 MyMethod是片段中的一种特殊方法。再次致歉,因为我一开始无法添加代码。

2 个答案:

答案 0 :(得分:1)

您使用FragmentManager来获取片段的句柄。 您正在代码中创建一个Fragment的新实例:

final FirstFragment firstfragment= new FirstFragment ();

新对象未绑定到任何活动的地方。

FragmentManager

答案 1 :(得分:1)

如果要从FirstFragment调用方法

FirstFragment firstFragment = (FirstFragment) getSupportFragmentManager().getFragments().get(0);
firstFragment.MyMethod();

如果要从SecondFragment调用方法

SecondFragment secondFragment = (SecondFragment) getSupportFragmentManager().getFragments().get(1);
secondFragment.MyMethod();

基于适配器getItem方法中的位置的索引0或1。

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            FirstFragment tab1 = new FirstFragment();
            return tab1;
        case 1:
            SecondFragment tab2 = new SecondFragment();
            return tab2;
        default:
            return null;
    }
}

然后尝试一下。