如何将PageAdapter中的数据发送到扩展Activity类的活动

时间:2015-12-07 04:26:51

标签: java android android-studio android-viewpager

我的Android应用中存在问题。在我的应用程序中点击btn_back_story01之后,我需要从一个PageViewer(称为customeSwipeAdapter)向一个扩展Activity类的活动发送一个布尔变量(称为newPermit)。 pageViewer是:

public class CustomSwipeAdapter01 extends PagerAdapter {

private int[] image_Resources = {R.drawable.sample_01, R.drawable.sample_02, R.drawable.sample_03, R.drawable.sample_04, R.drawable.sample_05, R.drawable.sample_06, R.drawable.sample_07};
private Context ctx;
private LayoutInflater layoutInflater;
boolean newPermit;



public CustomSwipeAdapter01(Context ctx) {
    this.ctx = ctx;
}

@Override
public int getCount() {
    return image_Resources.length;
}

@Override
public boolean isViewFromObject(View view, Object o) {
    return (view == (RelativeLayout) o);
}

@Override
public Object instantiateItem(final ViewGroup container, final int position) {
    layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View item_view = layoutInflater.inflate(R.layout.activity_story01, container, false);
    ImageView imageView = (ImageView) item_view.findViewById(R.id.image_view);
    TextView textView = (TextView) item_view.findViewById(R.id.image_count);
    Button btn_back_story01 = (Button) item_view.findViewById(R.id.btn_back_story01);
    imageView.setImageResource(image_Resources[position]);
    int itemNo = position + 1;
    textView.setText(itemNo + "/" + getCount());
    container.addView(item_view);
    newPermit=false;






    btn_back_story01.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            newPermit = true;
            ((Activity) ctx).finish();
            ctx.startActivity(intent);
            boolean over=false;



        }
    });

    return item_view;
}


@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((RelativeLayout) object);
}}

,活动是:

public class Story01 extends Activity {

ViewPager viewPager;
CustomSwipeAdapter01 adapter;
Button btn_back_story01;
boolean permit, oldPermit;
protected TestClass app;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_viewer);


    app = (TestClass) getApplication();

    viewPager = (ViewPager) findViewById(R.id.view_pager);
    btn_back_story01 = (Button) findViewById(R.id.btn_back_story01);
    adapter = new CustomSwipeAdapter01(this);
    viewPager.setAdapter(adapter);
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        View decorView = getWindow().getDecorView();

        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
    }
}

@Override
protected void onStart() {
    permit = false;
    super.onStart();
}



@Override
protected void onStop() {
    if (permit == false) {
        app.musicStop();
    }
    Intent intent = new Intent(getApplicationContext(), MainStory01.class);
    startActivity(intent);
    finish();
    super.onStop();
}}

我知道这很简单但是当你不知道一件简单的事情时,它就会变得困难。

1 个答案:

答案 0 :(得分:1)

您可以使用侦听器。您可以创建一个可以进行回调的界面。然后你让你的活动实现这个,并确保它传递给你的适配器使用。

<强> CustomSwipeAdapter01

public class CustomSwipeAdapter01 extends PagerAdapter {

    public interface NewPermitListener {

        void onPermitClicked(boolean over);
    }

    private NewPermitListener mListener;
    ...

    public CustomSwipeAdapter01(Context ctx, NewPermitListener listener) {
        this.ctx = ctx;
        // pass the listener in the constructor
        mListener = listener;
    }

    ...

    @Override
    public Object instantiateItem(final ViewGroup container, final int position) {
        ...
        btn_back_story01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ...
                // call the method
                mListener.onPermitClicked(false);
            }
        });

        return item_view;
    }
}

<强> Story01

public class Story01 extends Activity implements CustomSwipeAdapter01.NewPermitListener{

    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewer);
        ...
        // pass the listener in here
        adapter = new CustomSwipeAdapter01(this, this);
        viewPager.setAdapter(adapter);
    }

    @Override
    public void onPermitClicked(boolean over) {
        // do something with boolean
    }

    ...
}