zhang_yugang 2015年08月07日
解决不了了 求大神们指点(关于 viewpage和webview的问题)
问题描述:
     我用viewpage动态加载webview视图,显示没有问题,但是我想在点击webview页面时让 一个线性layout显示或隐藏。
代码编译没有错误,但是却实现不了效果。 设定代码在下面ViewPageAdapter的case MotionEvent.ACTION_UP:中,
直接上代码

Viewpage layout XML文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/linearLayout02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/guidePages"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

</FrameLayout>


WEBviewLayout XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/testline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#99CC00"
android:orientation="horizontal" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">

<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

</LinearLayout>

</LinearLayout>


JAVA主acitvity代码
package com.example.epubzipopen;

import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

final int RIGHT = 0;
final int LEFT = 1;
private ViewPager viewPager;
private ViewGroup main;
LayoutInflater inflater;
private ArrayList<String> list1=new ArrayList<String>();
private ViewPageAdapter viewpaperadapater;

WebView wView;
@SuppressLint("SetJavaScriptEnabled") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);

Intent intent=getIntent();
list1=intent.getStringArrayListExtra("PageList");

viewPager = (ViewPager)findViewById(R.id.guidePages);

viewpaperadapater= new ViewPageAdapter(this,list1);
viewPager.setAdapter(viewpaperadapater);
viewPager.setOnPageChangeListener(viewpaperadapater);
viewPager.setCurrentItem(0);


}

}


WEBview数据动态显示 ViewPageAdapter代码
package com.example.epubzipopen;

import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;

@SuppressLint("SetJavaScriptEnabled") public class ViewPageAdapter extends PagerAdapter implements OnPageChangeListener {
private Context context;
private ArrayList<String> list=new ArrayList<String>();
private LayoutInflater inflater;
private String commonFilepath="";

public ViewPageAdapter (Context context, ArrayList<String> Data) {
this.list = Data;
inflater = LayoutInflater.from(context);
commonFilepath=Environment.getExternalStorageDirectory().toString();
this.context = context;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return list != null ? list.size() : 0;
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}

@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return super.getItemPosition(object);
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
// TODO Auto-generated method stub
((ViewPager) arg0).removeView(arg0);
}

@Override
public Object instantiateItem(ViewGroup view, int position) {
View userLayout = inflater.inflate(R.layout.activity_main, null);
WebView webview = (WebView)userLayout.findViewById(R.id.webView1);

WebSettings wSet = webview.getSettings();
wSet.setJavaScriptEnabled(true);
wSet.setJavaScriptEnabled(true);
wSet.setBuiltInZoomControls(true);
wSet.setSupportZoom(true);
webview.requestFocusFromTouch();
webview.setWebChromeClient(new WebChromeClient());
String strHtmlPath="file://"+commonFilepath+"/Download/EpubZipOut/OEBPS/"+list.get(position);
webview.loadUrl(strHtmlPath);
webview.setOnTouchListener(new OnTouchListener() {
@SuppressLint("ClickableViewAccessibility") @Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_UP:
{
// 设置的地方在这LOOK here
View userLayout = inflater.inflate(R.layout.activity_main, null);
LinearLayout testline = (LinearLayout)userLayout.findViewById(R.id.testline);
testline.setVisibility(View.GONE);
}
break;
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
default:
return false;
}
return false;
}
});
((ViewPager) view).addView(userLayout, 0);
return userLayout;
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View container) {}

@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

}

@Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub

}
}



...全文
173 点赞 收藏 8
写回复
8 条回复

还没有回复,快来抢沙发~

发动态
发帖子
Android
创建于2009-10-09

4.6w+

社区成员

9.0w+

社区内容

移动平台 Android
社区公告
暂无公告