[android]怎么可以我将数据从发送到 PageAdapter activity ,扩展 Activity 类

发布时间: 2016/9/26 4:47:47
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个问题我 android 的应用程序。在我的应用程序在 btn_back_story01 上单击之后,我需要发送一个布尔变量 (即称为 newPermit) 从 PageViewer (即称为 customeSwipeAdapter) 到 activity ,延伸 Activity 类。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);
}}

activity 是︰

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

我知道很简单,但是当你不知道一个简单的事情,这将是 difficault。

解决方法 1:

您可以使用一个listener。您创建一个界面,您可以与回调。然后你使你的 activity 实现这一点并确保其传递到你 adapter 使用。

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
    }

    ...
}
官方微信
官方QQ群
31647020