Button activity with viewPager?

2019-04-10 03:39发布

Please don't judge me, i'm extremely new on android developing... I wanna make an app with viewPager... I've got three pages with three different layouts... But i just couldn't figure out how to deal with a button on second page on viewPager. I found this question " How to write button onClick method in viewpager? " and set my codes like this:

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'm getting errors: "The method findViewById(int) is undefined for the type new View.OnClickListener(){}" on line

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

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

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

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));
    }
}

 }

So as i get, i cannot use those two codes... So how should i do it? I need to change background of the second page when clicked the button on second layout.

And here's 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>

3条回答
兄弟一词,经得起流年.
2楼-- · 2019-04-10 03:50

As for the second part of your question, it's simple! Although you now use View v all along, you still "overwrite" it with a 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 perfectly:

  • Change View v = new View(pager.getContext()); to View v = null;
  • Change the final inflation (just before adding the view) to if (v == null) v = inflater.inflate(resId, null);
查看更多
唯我独甜
3楼-- · 2019-04-10 04:05

In response to your most recent question, you can set an onClick listener outside of the adapter and within the activity like this:

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 I suggested from your most recent code... here is what I was thinking:

@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 that's not it, then please let us know the error so we can lead you in the right direction.

查看更多
\"骚年 ilove
4楼-- · 2019-04-10 04:08

That's because, as it explains, OnClickListener doesn't have these two methods which you're using inside an OnClickListener. Replace:

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

with

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

if it then says v should be final, that should be okay. And replace:

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

with

ViewPagerAdapter.this.context.getResources().getDrawable(R.drawable.background)
查看更多
登录 后发表回答