I have a ViewPager located at the bottom of my app, and I'm trying to get its index, id or any reference to the ViewPager that is displayed on the screen so that depending on which is displayed, the button they share makes an action u another in the onClick () method. A basic idea of what he raised is:
//Funcion de escucha del Button
Button btnTick = (Button) rootView.findViewById(R.id.buttonInvertirTick);
btnTick.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
if(index1.isShowed()==true){
//Tareas a realizar
} else (index2.isShowed()==true){
//Tareas a realizar
}
}
});
Deputy code
fragment_screen_slide_page.xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/LayoutEditText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/tvIndex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="false"
android:layout_marginTop="11dp"
android:layout_marginLeft="7dp"
android:textSize="25dp" />
<EditText
android:id="@+id/valores"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:inputType="phone" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/LayoutEditText"
android:orientation="horizontal">
<Button
android:id="@+id/buttonInvertirTick"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"
android:background="@drawable/success"/>
</LinearLayout>
</LinearLayout>
</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";
/**
* Key to insert the text into the mapping of a Bundle.
*/
private static final String TEXTO = "index";
private int color;
private int index;
private String textoSta;
/**
* Instancia un nuevo fragment con un background color, el indice de la pagina y un texto
*
* @param color background color
* @param index index page
* @param texto texto de tipo de cotizacion
* @return a new page
*/
public static ScreenSlidePageFragment newInstance(int color, int index, String texto) {
// Instantiate a new fragment
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
// Save the parameters
Bundle bundle = new Bundle();
bundle.putInt(BACKGROUND_COLOR, color);
bundle.putInt(INDEX, index);
bundle.putString(TEXTO, texto);
fragment.setArguments(bundle);
fragment.setRetainInstance(true);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Carga los parametros con los que se sobrecargo cuando la precarga inicial del fragment se completa
this.color = (getArguments() != null) ? getArguments().getInt(
BACKGROUND_COLOR) : Color.DKGRAY;
this.textoSta = (getArguments() != null) ? getArguments().getString(TEXTO)
: "";
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);
// Establece el texto del TextView con el que se sobrecargo
TextView tvIndex = (TextView) rootView.findViewById(R.id.tvIndex);
tvIndex.setText(textoSta);
// Establece el color de fondo con el que se sobrecargo
rootView.setBackgroundColor(color);
//Funcion de escucha del Button
Button btnTick = (Button) rootView.findViewById(R.id.buttonInvertirTick);
btnTick.setOnClickListener(new View.OnClickListener() {
public void onClick (View v) {
Context context = getActivity();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
});
return rootView;
}
}
The way in which I installed the ViewPager in the classes:
// Instantiate a ViewPager
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.backgroundPager), 0, "Saldo a invertir: "));
adapter.addFragment(ScreenSlidePageFragment.newInstance(getResources()
.getColor(R.color.backgroundPager), 1, "Valores a comprar: "));
pager.setAdapter(adapter);
I hope your help!