我是触摸记录触摸的touchevent列表,我使用了viewpager进行swping ..因为我们知道java的缺点是传递值的东西,我仍然试图在匿名内部类中传递布尔值。 switch stmt中更改的值不会保留在switch stmt之外。 flag2值.anyone有解决方案吗?我的代码如下
parent.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
//flag2=!flag2;
switch(event.getAction())
{
case MotionEvent.ACTION_MOVE:
awesomePager.requestDisallowInterceptTouchEvent(true);
//flag2=false;
break;
case MotionEvent.ACTION_UP:
awesomePager.requestDisallowInterceptTouchEvent(true);
if(flag)
{
upperdock.setClickable(false);
upperdock.bringToFront();
tocparent.bringToFront();
tocbottom.bringToFront();
upperdock.setVisibility(RelativeLayout.VISIBLE);
tocparent.setVisibility(LinearLayout.VISIBLE);
tocbottom.setVisibility(LinearLayout.VISIBLE);
flag=false;
// obj.one(event.getAction());
}
else
{
parent.bringToFront();
upperdock.setVisibility(RelativeLayout.INVISIBLE);
tocparent.setVisibility(LinearLayout.INVISIBLE);
tocbottom.setVisibility(LinearLayout.INVISIBLE);
flag=true;
//obj.one(event.getAction());
}
flag2=true;
break;
case MotionEvent.ACTION_CANCEL:
awesomePager.requestDisallowInterceptTouchEvent(false);
flag2=false;
break;
}
return flag2;
}
});
public class SimpleViewPagerforAndroidActivity extends Activity { /** Called when the activity is first created. */ boolean flag2=true; //rest of the code }
请帮忙......需要它..
package com.beanie.samples.viewpager;
//set of imports
public class SimpleViewPagerforAndroidActivity extends Activity {
/** Called when the activity is first created. */
volatile boolean flag2=true;
AwesomePagerAdapter awe1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
cxt = this;
data = new ArrayList<WebView>();
awesomePager = (ViewPager) findViewById(R.id.viewPager);
awesomePager.setAdapter(new AwesomePagerAdapter(this,data));
Bundle extras = getIntent().getExtras();
Boolean dock2checked = extras.getBoolean("dock1");
if(dock2checked)
{
Toast toast1 = Toast.makeText(getBaseContext(), Boolean.toString(dock2checked), 5000);
toast1.show();
}
else
{
Toast toast1 = Toast.makeText(getBaseContext(), Boolean.toString(dock2checked), 5000);
toast1.show();
}
awesomePager.setOffscreenPageLimit(11);
//
web0 = new WebView(cxt);
web0.loadUrl("file:///android_asset/1/01-Cover.html");
WebSettings webSettings0 = web0.getSettings();
webSettings0.setJavaScriptEnabled(true);
web1 = new WebView(cxt);
web1.loadUrl("file:///android_asset/1/01-Cover.html");
WebSettings webSettings1 = web1.getSettings();
webSettings1.setJavaScriptEnabled(true);
web2 = new WebView(cxt);
web2.loadUrl("file:///android_asset/5/05-01.html");
WebSettings webSettings2 = web2.getSettings();
webSettings2.setJavaScriptEnabled(true);
web3 = new WebView(cxt);
web3.loadUrl("file:///android_asset/1/01-Cover.html");
WebSettings webSettings3 = web3.getSettings();
webSettings3.setJavaScriptEnabled(true);
web4 = new WebView(cxt);
web4.loadUrl("file:///android_asset/5/05-01.html");
WebSettings webSettings4 = web4.getSettings();
webSettings4.setJavaScriptEnabled(true);
web5 = new WebView(cxt);
web5.loadUrl("file:///android_asset/1/01-Cover.html");
WebSettings webSettings5 = web5.getSettings();
webSettings5.setJavaScriptEnabled(true);
web6 = new WebView(cxt);
web6.loadUrl("file:///android_asset/5/05-01.html");
WebSettings webSettings6 = web6.getSettings();
webSettings6.setJavaScriptEnabled(true);
web7 = new WebView(cxt);
web7.loadUrl("file:///android_asset/1/01-Cover.html");
WebSettings webSettings7 = web7.getSettings();
webSettings7.setJavaScriptEnabled(true);
web8 = new WebView(cxt);
web8.loadUrl("file:///android_asset/5/05-01.html");
WebSettings webSettings8 = web8.getSettings();
webSettings8.setJavaScriptEnabled(true);
web9 = new WebView(cxt);
web9.loadUrl("file:///android_asset/1/01-Cover.html");
WebSettings webSettings9 = web9.getSettings();
webSettings9.setJavaScriptEnabled(true);
web10 = new WebView(cxt);
web10.loadUrl("file:///android_asset/5/05-01.html");
WebSettings webSettings10 = web10.getSettings();
webSettings10.setJavaScriptEnabled(true);
data.add(web0);
data.add(web1);
data.add(web2);
data.add(web3);
data.add(web4);
data.add(web5);
data.add(web6);
data.add(web7);
data.add(web8);
data.add(web9);
data.add(web10);
tocparent=(LinearLayout) findViewById(R.id.tocparent);
dock2=(Button)findViewById(R.id.button1);
//dock2.setVisibility(Button.INVISIBLE);
tocparent.setVisibility(LinearLayout.INVISIBLE);
topmost=(RelativeLayout) findViewById(R.id.topmost);
parent=(RelativeLayout) findViewById(R.id.rl1);
upperdock=(RelativeLayout) findViewById(R.id.upperdock);
tocparent=(LinearLayout) findViewById(R.id.tocparent);
tocbottom=(LinearLayout) findViewById(R.id.linearLayout3);
upperdock.setVisibility(RelativeLayout.INVISIBLE);
tocparent.setVisibility(LinearLayout.INVISIBLE);
tocbottom.setVisibility(LinearLayout.INVISIBLE);
web0.loadUrl("javascript:fhhf()");
parent.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
//flag2=!flag2;
switch(event.getAction())
{
case MotionEvent.ACTION_MOVE:
awesomePager.requestDisallowInterceptTouchEvent(true);
//flag2=false;
break;
case MotionEvent.ACTION_UP:
awesomePager.requestDisallowInterceptTouchEvent(true);
if(flag)
{
upperdock.setClickable(false);
upperdock.bringToFront();
tocparent.bringToFront();
tocbottom.bringToFront();
upperdock.setVisibility(RelativeLayout.VISIBLE);
tocparent.setVisibility(LinearLayout.VISIBLE);
tocbottom.setVisibility(LinearLayout.VISIBLE);
flag=false;
// obj.one(event.getAction());
}
else
{
parent.bringToFront();
upperdock.setVisibility(RelativeLayout.INVISIBLE);
tocparent.setVisibility(LinearLayout.INVISIBLE);
tocbottom.setVisibility(LinearLayout.INVISIBLE);
flag=true;
//obj.one(event.getAction());
}
flag2=true;
break;
case MotionEvent.ACTION_CANCEL:
awesomePager.requestDisallowInterceptTouchEvent(false);
flag2=false;
//Toast.makeText(getApplicationContext(), s, 220).show();
//obj.one(event.getAction());
break;
}
/* if(event.getAction()==MotionEvent.ACTION_UP)
{
flag2=true;
}
else if(event.getAction()==MotionEvent.ACTION_CANCEL)
{
flag2=false;
}*/
return flag2;
}
});
awesomePager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int arg0) {
//Toast toast1 = Toast.makeText(getBaseContext(), "PageSelected"+ arg0, 5000);
//Toast1.show();
if(arg0==0)
{
//web0.loadUrl("javascript:fhhf()");
// awesomePager.getChildAt(arg0 - 1);
web0.loadUrl("javascript:fhhf1()");
web0.loadUrl("javascript:fhhf()");
}
else if(arg0==1)
{
web1.loadUrl("javascript:fhhf1()");
web1.loadUrl("javascript:fhhf()");
}
else if(arg0==3)
{
web3.loadUrl("javascript:fhhf1()");
//Toast toast2 = Toast.makeText(getBaseContext(), "fun"+ arg0, 5000);
//Toast2.show();
}
else if(arg0==5)
web5.loadUrl("javascript:fhhf()");
else if(arg0==7)
web7.loadUrl("javascript:fhhf()");
else if(arg0==9)
web9.loadUrl("javascript:fhhf()");
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
int o = awesomePager.getCurrentItem();
WebView temp = (WebView) awesomePager.getChildAt(o);
if(arg0 == 1)
{
temp.loadUrl("javascript:fhhf1()");
}
else if(arg0 == 0)
{
temp.loadUrl("javascript:fhhf()");
}
}
});
}
public boolean fun1()
{
return true;
}
public boolean fun2()
{
return false;
}
private class AwesomePagerAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener {
private List<WebView> data;
private Context ctx1;
public AwesomePagerAdapter(Context ctx, List<WebView> data) {
this.ctx1 = ctx;
this.data = data;
}
@Override
public int getCount() {
return NUM_AWESOME_VIEWS;
}
@Override
public Object instantiateItem(View collection, int position) {
data.get(position).setBackgroundColor(R.color.black);
return data.get(position);
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((WebView) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==((WebView)object);
}
@Override
public void finishUpdate(View arg0) {
}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {}
public void onPageScrollStateChanged(int position) {
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
public void onPageSelected(int position) {
}
}
}
提前致谢
答案 0 :(得分:0)
首先在 SimpleViewPagerforAndroidActivity 中制作一个方法,如下所示你也可以使用 void 如果你不想返回任何东西
boolean method(Boolean flag){
flag2=flag;
return flag2;
}
在其他课程中制作对象,如下所示
SimpleViewPagerforAndroidActivity abc = new SimpleViewPagerforAndroidActivity();
并在开关语句调用方法
中的ontouch事件中abc.method(false or true);
这将更新你的 flag2 ,你可以通过在日志中打印来检查
Log.d("flag2",""+abc.flag2);
答案 1 :(得分:0)
也许尝试强制它在UI线程上运行?
IE:
public void passBoolean(final boolean boolToPass){
runOnUiThread(new Runnable() {
public void run() {
//Do whatever here on the UI thread
}
});
}