我的Android应用程式有问题。在我的应用程序中,单击btn_back_story01之后,我需要将一个布尔变量(称为newPermit)从PageViewer(称为customeSwipeAdapter)发送到扩展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);
}}
活动是:
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();
}}
我知道这很简单,但是当您不了解简单的东西时,就会感到困难。
最佳答案
您可以使用侦听器。您创建一个可以进行回调的接口。然后,使活动实现该活动并确保将其传递给适配器以使用。
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;
}
}
故事01
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
}
...
}