Button activity with viewPager?

Please do not judge me, I am very new to Android development ... I want to make an application with viewPager ... I have three pages with three different layouts ... But I just could not figure out how to work with the button on the second page in viewPager. I found this question. How to write onClick method button in viewpager? "and set my codes as follows:

ViewPagerAdapter:

@Override public Object instantiateItem( View pager, int position ) { View v = new View(pager.getContext()); LayoutInflater inflater = (LayoutInflater) pager.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); int resId = 0; switch (position) { case 0: resId = R.layout.deneme; break; case 1: resId = R.layout.deneme2; break; case 2: resId = R.layout.deneme3; break; } v = inflater.inflate(resId, null); ((ViewPager) pager).addView(v, 0); return v; } 

Here I get errors: "The findViewById (int) method is undefined for the type new View.OnClickListener () {}" in the line

  LinearLayout l = (LinearLayout) findViewById(R.id.layout2); 

and "The getResources () method is undefined for the type new View.OnClickListener () {}" in the line

  l.setBackgroundDrawable(getResources().getDrawable(R.drawable.background)); 

The main:

  public class ViewPagerProjectActivity extends Activity { Button btn; View deneme2lout; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ViewPagerAdapter adapter = new ViewPagerAdapter( this ); ViewPager pager = (ViewPager)findViewById( R.id.viewpager ); pager.setAdapter( adapter ); pager.setCurrentItem(0); LayoutInflater getView = getLayoutInflater(); deneme2lout = (View) getView.inflate(R.layout.deneme2,null); btn = (Button) deneme2lout.findViewById(R.id.button1); } public void buttonClick(View v) { if(v.equals(btn)) { deneme2lout.setBackgroundDrawable(getResources().getDrawable(R.drawable.background)); } } } 

Since I get, I can’t use these two codes ... So how should I do this? I need to change the background of the second page when I click the button in the second layout.

And here are my deneme2.xml codes:

 <?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="175dp" android:layout_height="95dp" android:layout_x="69dp" android:layout_y="174dp" android:textSize="25dp" android:text="Change" /> </AbsoluteLayout> 
+4
source share
3 answers

This is because, as he explains, OnClickListener does not have these two methods that you use inside OnClickListener. Replace:

 LinearLayout l = (LinearLayout) findViewById(R.id.layout2); 

from

 LinearLayout l = (LinearLayout) v.findViewById(R.id.layout2); 

if he says v should be final, that should be good. And replace:

 getResources().getDrawable(R.drawable.background) 

from

 ViewPagerAdapter.this.context.getResources().getDrawable(R.drawable.background) 
+2
source

As for the second part of your question, it is easy! Although you are now using View v , you are still “overwriting” it with new inflation at the end:

 int resId = 0; switch (position) { case 0: resId = R.layout.deneme; break; case 1: resId = R.layout.deneme2; // Inflate v the first time v = inflater.inflate(R.layout.deneme2, null, false); @SuppressWarnings("deprecation") final AbsoluteLayout l = (AbsoluteLayout) v.findViewById(R.id.layout2); // Add the OnClickListener to the button, which is associated with the above instance of v btn = (Button) v.findViewById(R.id.button1); btn.setOnClickListener( new OnClickListener() { public void onClick(View m) { l.setBackgroundResource(R.drawable.background); } }); break; case 2: resId = R.layout.deneme3; } // Overwrite the instance of v with the OnClickListener with one without the OnClickListener v = inflater.inflate(resId, null); ((ViewPager) pager).addView(v, 0); return v; 

So, changing the following bits should make it work fine:

  • Change View v = new View(pager.getContext()); on View v = null;
  • Change the final inflation (just before adding the view) to if (v == null) v = inflater.inflate(resId, null);
+2
source

In response to your last question, you can install the onClick listener outside the adapter and as part of the action, for example:

 public void buttonClick(View v) { //do something } 

In your xml containing the button, make sure you have android: onClick = "buttonClick" as 1 of the properties.

In your xml containing the button, make sure you have android: onClick = "buttonClick" as 1 of the properties.

Edit - I don’t think you applied the changes that I suggested from your last code ... here is what I thought:

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ViewPagerAdapter adapter = new ViewPagerAdapter( this ); ViewPager pager = (ViewPager)findViewById( R.id.viewpager ); pager.setAdapter( adapter ); pager.setCurrentItem(0); LayoutInflater getView = getLayoutInflater(); deneme2lout = (View) getView.inflate(R.layout.deneme2,null); } public void buttonClick(View v) { deneme2lout.setBackgroundDrawable(getResources().getDrawable(R.drawable.background)); } 

If this is not the case, please let us know about the error so that we can lead you in the right direction.

0
source

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


All Articles