Insert buttons and editText in an Android FragmentPagerAdapter

0

I have a FragmentPagerAdapter that loads in an activity of the app that I am developing. Right now what happened is the values of the background color that the adapter will have. Even that my intention is to make two Adapters where it contains an editText and two buttons that when clicking on a button triggers an action.

Basically what I have in mind is to have two Adapters with the same structure but one to put a certain type of data and in the other editText another type of data.

fragment_screen_slide_page.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:id="@+id/tvIndex"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:textColor="@color/letra"
    android:textIsSelectable="false"
    android:textSize="75sp" />

<ToggleButton
    android:id="@+id/btnFragment"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:gravity="center" />

<ToggleButton
    android:id="@+id/btnFragment2"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:gravity="center" />

<EditText
    android:layout_width="50sp"
    android:layout_height="150sp"
    android:id="@+id/eTxtFragment"/>

</LinearLayout>

ScreenSlidePageFragment.java:

public class ScreenSlidePageFragment extends Fragment {

/**
 * Key to insert the background color into the mapping of a Bundle.
 */
private static final String BACKGROUND_COLOR = "color";

/**
 * Key to insert the index page into the mapping of a Bundle.
 */
private static final String INDEX = "index";

private int color;
private int index;

/**
 * Instances a new fragment with a background color and an index page.
 *
 * @param color background color
 * @param index index page
 * @return a new page
 */
public static ScreenSlidePageFragment newInstance(int color, int index) {

    // Instantiate a new fragment
    ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();

    // Save the parameters
    Bundle bundle = new Bundle();
    bundle.putInt(BACKGROUND_COLOR, color);
    bundle.putInt(INDEX, index);
    fragment.setArguments(bundle);
    fragment.setRetainInstance(true);

    return fragment;

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // Load parameters when the initial creation of the fragment is done
    this.color = (getArguments() != null) ? getArguments().getInt(
            BACKGROUND_COLOR) : Color.GRAY;
    this.index = (getArguments() != null) ? getArguments().getInt(INDEX)
            : -1;

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.fragment_screen_slide_page, container, false);

    // Muestra la pagina en la que se encuentra el indice
    TextView tvIndex = (TextView) rootView.findViewById(R.id.tvIndex);
    tvIndex.setText(String.valueOf(this.index));

    // Cambia el color de fondo
    rootView.setBackgroundColor(this.color);

    // Inicializa el boton del fragment
    ToggleButton btnFragment = (ToggleButton) rootView.findViewById(R.id.btnFragment);
    ToggleButton btnFragment2 = (ToggleButton) rootView.findViewById(R.id.btnFragment2);

    EditText eTxtFragment = (EditText) rootView.findViewById(R.id.eTxtFragment);

    return rootView;

}
}

MyFragmentPagerAdapter.java

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

// List of fragments which are going to set in the view pager widget
List<Fragment> fragments;

/**
 * Constructor
 *
 * @param fm interface for interacting with Fragment objects inside of an
 *           Activity
 */
public MyFragmentPagerAdapter(FragmentManager fm) {
    super(fm);
    this.fragments = new ArrayList<Fragment>();
}

/**
 * Add a new fragment in the list.
 *
 * @param fragment a new fragment
 */
public void addFragment(Fragment fragment) {
    this.fragments.add(fragment);
}

@Override
public Fragment getItem(int arg0) {
    return this.fragments.get(arg0);
}

@Override
public int getCount() {
    return this.fragments.size();
}

@Override
public CharSequence getPageTitle(int position) {
    return "Página " + (position + 1);
}
}

Instance and overload the fragment in this way in the activity that I use:

        // Instantiate a ViewPager
    this.pager = (ViewPager) this.findViewById(R.id.viewPager);

    // Create an adapter with the fragments we show on the ViewPager
    MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(
            getSupportFragmentManager());
    adapter.addFragment(ScreenSlidePageFragment.newInstance(getResources()
            .getColor(R.color.background2), 0));
    adapter.addFragment(ScreenSlidePageFragment.newInstance(getResources()
            .getColor(R.color.background2), 1));
    this.pager.setAdapter(adapter);

And this is the idea of the approach:

Any idea is helpful, thanks !!!

    
asked by Eduardo 18.05.2017 в 18:51
source

0 answers