Rieth Rieth - 5 months ago 37
Android Question

Get id from button of a fragment inside a ViewPager

I have a main activity that have a ViewPager, inside this ViewPager i inserted two fragments:

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


Then in MyFrag i want to set a button click listener:

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();

}


The problem is
Button btn = (Button) container.findViewById(R.id.btn);
is null, because container is the ViewPager instead Fragment... How i get my fragment here?

Answer

In onCreateView of Fragment, change to this

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

    }