Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I make my button to do something in Fragments,ViewPager
    primarykey
    data
    text
    <p>First I must say that I'm not good at English and "completely new" to Android Programming.</p> <p>I want to create an app like Android's home screen which can slide left-right to see the views. On my app, each view will have a button to do something; Now I have accomplished the sliding part by using ViewPager and Fragments. When I run my app, it can smoothly go left-right views but I can't find the solution to make a button in each view to working. How can I do this? Thank you very much for every of your answers. Followings are my code(I modified it from examples over the internet).</p> <p>This the main fragment class that contains many fragments in it.</p> <pre><code>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&lt;Fragment&gt; fragments = new Vector&lt;Fragment&gt;(); 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); } </code></pre> <p>}</p> <p>This below is the fragment class that I used to display the layout.</p> <pre><code>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's 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); } </code></pre> <p>}</p> <p>And this is my PagerAdapterClass</p> <pre><code>public class PagerAdapter extends FragmentPagerAdapter { private List&lt;Fragment&gt; fragments; /** * @param fm * @param fragments */ public PagerAdapter(FragmentManager fm, List&lt;Fragment&gt; 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(); } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload