How to make my button do something in Fragments, ViewPager

First I have to say that I am not good at English and "brand new" for Android programming.

I want to create an application, such as an Android Android screen, that can move left and right to see views. In my application, each view has a button to do something; Now I have done the sliding part with ViewPager and Fragments. When I launch my application, it can smoothly move left-right, but I can’t find a solution to make a button work in each view. How can i do this? Thanks so much for each of your answers. The following is my code (I changed it from examples over the Internet).

This main fragment class contains many fragments.

public class LessonsActivity extends FragmentActivity{ /** maintains the pager adapter */ private PagerAdapter mPagerAdapter; /* * (non-Javadoc) * * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle) */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.viewpager_layout); // initialsie the pager this.initialisePaging(); } /** * Initialise the fragments to be paged */ private void initialisePaging() { List<Fragment> fragments = new Vector<Fragment>(); fragments.add(Fragment.instantiate(this, Lesson_1.class.getName())); fragments.add(Fragment.instantiate(this, Lesson_2.class.getName())); fragments.add(Fragment.instantiate(this, Lesson_3.class.getName())); fragments.add(Fragment.instantiate(this, Lesson_4.class.getName())); this.mPagerAdapter = new PagerAdapter( super.getSupportFragmentManager(), fragments); // ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager); pager.setAdapter(this.mPagerAdapter); } 

}

The following is the snippet class that I used to display the layout.

 public class Lesson_1 extends Fragment { /** * (non-Javadoc) * * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, * android.view.ViewGroup, android.os.Bundle) */ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { // We have different layouts, and in one of them this // fragment containing frame doesn't exist. The fragment // may still be created from its saved state, but there is // no reason to try to create its view hierarchy because it // won't be displayed. Note this is not needed -- we could // just run the code below, where we would create and return // the view hierarchy; it would just never be used. return null; } return (LinearLayout) inflater.inflate(R.layout.lessons1, container, false); } 

}

And this is my PagerAdapterClass

 public class PagerAdapter extends FragmentPagerAdapter { private List<Fragment> fragments; /** * @param fm * @param fragments */ public PagerAdapter(FragmentManager fm, List<Fragment> fragments) { super(fm); this.fragments = fragments; } /* (non-Javadoc) * @see android.support.v4.app.FragmentPagerAdapter#getItem(int) */ @Override public Fragment getItem(int position) { return this.fragments.get(position); } /* (non-Javadoc) * @see android.support.v4.view.PagerAdapter#getCount() */ @Override public int getCount() { return this.fragments.size(); } 

}

+4
source share
2 answers

In Fragment , in the onCreateView() method, you return the view for a specific Fragment . This means that you can manipulate it. Instead of immediately returning an (inflated) view, put it in a variable and manipulate that variable, for example:

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { ... return null; } LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(R.layout.lessons1, container, false); // note that we're looking for a button with id="@+id/myButton" in your inflated layout // Naturally, this can be any View; it doesn't have to be a button Button mButton = (Button) mLinearLayout.findViewById(R.id.myButton); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // here you set what you want to do when user clicks your button, // eg launch a new activity } }); // after you've done all your manipulation, return your layout to be shown return mLinearLayout; } 
+24
source
 public class EmpresasFragment extends Fragment { private LinearLayout lLayoutFrgEmpresas; private Button btnNewEmpresa; private Button btnMapEmpresas; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { lLayoutFrgEmpresas=(LinearLayout) inflater.inflate(R.layout.fragment_empresas, container, false); btnNewEmpresa=(Button) lLayoutFrgEmpresas.findViewById(R.id.frEmp_nuevaEmpresa); btnNewEmpresa.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Log.d(getClass().getCanonicalName(),"Se ha presionado el botton de nueva empresa"); } }); return lLayoutFrgEmpresas; } } 
+1
source

Source: https://habr.com/ru/post/1403058/


All Articles