我有一个具有ViewPager的主活动,在此ViewPager中我插入了两个片段:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
MyFrag f1 = new MyFrag();
MyFrag f2 = new MyFrag();
adapter.addFragment(f1, "F1");
adapter.addFragment(f2, "F2");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList();
private final List<String> mFragmentTitleList = new ArrayList();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
然后在MyFrag中我想设置一个按钮点击监听器:
public class MyFrag extends Fragment{
public void MyFrag(){
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
final Bundle savedInstanceState) {
// Inflate the layout for this fragment
addFragment(new OtherFrag());
Button btn = (Button) container.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addFragment(new MyOtherFrag());
}
});
return inflater.inflate(R.layout.myfrag_fragment, container, false);
}
public void addFragment(Fragment fragment){
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragContainer, fragment, "MyOtherFrag");
fragmentTransaction.commit();
getChildFragmentManager().executePendingTransactions();
}
问题是Button btn = (Button) container.findViewById(R.id.btn);
为空,因为容器是ViewPager而不是片段......我如何在这里得到我的片段?
答案 0 :(得分:1)
要从MyFrag
片段布局访问按钮,请使用从onCreateView
方法返回的相同视图:
@Override
public View onCreateView(...) {
View view=inflater.inflate(R.layout.myfrag_fragment, container, false);
Button btn = (Button) view.findViewById(R.id.btn);
....
return view;
}
答案 1 :(得分:0)
在片段的onCreateView中,更改为此
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
final Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.myfrag_fragment, container, false);
addFragment(new OtherFrag());
Button btn = (Button) view.findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addFragment(new MyOtherFrag());
}
});
return view;
}