案例2_健康栏目的实现分析
2016-12-04 21:04
405 查看
一、实现结果
二、实现过程
三、核心代码
HealthViewPagerAdapter.java
public class HealthViewPagerAdapter extends PagerAdapter{
private List<View> viewList;
public HealthViewPagerAdapter(List<View> viewList) {
this.viewList = viewList;
}
public int getCount(){
return viewList.size();
}
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position));
return viewList.get(position);
}
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewList.get(position));
}
}
pagerofhealth.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/wvHealth"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<include layout="@layout/health_layout_titlebar"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 健康新知 -->
<TextView
android:id="@+id/tv_healthnews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:padding="6dp"
android:text="@string/healthnews"
android:textSize="18dp"/>
<!-- 疾病防治 -->
<TextView
android:id="@+id/tv_healthill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:padding="6dp"
android:text="@string/illdefense"
android:textSize="18dp"/>
</LinearLayout>
<TextView
android:id="@+id/cursor"
android:layout_width="125dp"
android:layout_height="5dp"
android:layout_marginLeft="20dp"
android:background="#990033"/>
<android.suqqort.v4.view.ViewPage
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
增加权限: <uses-permission android:name="android.permission.INTERNET" />
二、实现过程
三、核心代码
HealthViewPagerAdapter.java
public class HealthViewPagerAdapter extends PagerAdapter{
private List<View> viewList;
public HealthViewPagerAdapter(List<View> viewList) {
this.viewList = viewList;
}
public int getCount(){
return viewList.size();
}
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
public Object instantiateItem(ViewGroup container, int position) {
container.addView(viewList.get(position));
return viewList.get(position);
}
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewList.get(position));
}
}
HealthWebView.java
public class HealthWebView { private Context context; public HealthWebView(Context context) { this.context = context; } public View getView(String url) { View view = LayoutInflater.from(context).inflate( R.layout.pagerofhealth, null); WebView webView = (WebView) view.findViewById(R.id.wvHealth); webView.loadUrl(url); // 设置支持 JavaScript 脚本 webView.getSettings().setJavaScriptEnabled(true); /** * 禁止系统浏览器打开页面 */ webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); return view; } }
MainActivity
public class MainActivity extends Activity { private int currIndex;// 当前页卡编号 private TextView tvCursor; private TextView tvHealthNews; private TextView tvIllnessDefense; private ViewPager vpHealth; private ImageView ivHealthBack; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); initViews(); initCursor(); initViewPager(); setListeners(); } private void initViews() { ivHealthBack = (ImageView) findViewById(R.id.icons_health_back); tvCursor = (TextView) findViewById(R.id.cursor); tvHealthNews = (TextView) findViewById(R.id.tv_healthnews); tvIllnessDefense = (TextView) findViewById(R.id.tv_healthill); vpHealth = (ViewPager) findViewById(R.id.viewpager); } private void initCursor() { Display display = getWindow().getWindowManager().getDefaultDisplay(); DisplayMetrics metrics = new DisplayMetrics(); display.getMetrics(metrics); // 取得手机屏幕宽度的一半 int tabLineLength = metrics.widthPixels / 2; // 设置游标的宽度为屏幕宽度的一半 LayoutParams lp = (LayoutParams) tvCursor.getLayoutParams(); lp.width = tabLineLength; tvCursor.setLayoutParams(lp); } private void initViewPager() { vpHealth = (ViewPager) findViewById(R.id.viewpager); List<View> views = new ArrayList<View>(); views.add(new HealthWebView(this) .getView("http://cms.hxky.cn/wap/jkxz/")); views.add(new HealthWebView(this) .getView("http://cms.hxky.cn/wap/jbfz/")); vpHealth.setAdapter(new HealthViewPagerAdapter(views)); // 给 ViewPager 设置适配器 vpHealth.setCurrentItem(0);// 设置当前显示标签页为第一页 } private void setListeners() { // 返回按钮 ivHealthBack.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { finish(); } }); // 点击健康须知 tvHealthNews.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { vpHealth.setCurrentItem(0); } }); // 点击疾病防治 tvIllnessDefense.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { vpHealth.setCurrentItem(1); } }); vpHealth.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { currIndex = position; } @Override public void onPageScrolled(int position, float percent, int ag2) { LinearLayout.LayoutParams ll = (android.widget.LinearLayout.LayoutParams) tvCursor .getLayoutParams(); if (currIndex == position) { ll.leftMargin = (int) (currIndex * tvCursor.getWidth() + percent * tvCursor.getWidth()); } else if (currIndex > position) { ll.leftMargin = (int) (currIndex * tvCursor.getWidth() - (1 - percent) * tvCursor.getWidth()); } tvCursor.setLayoutParams(ll); } @Override public void onPageScrollStateChanged(int position) { } }); } }health_layout_titlebar.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="wrap_content" android:background="@drawable/titlebar_bg" > <ImageView android:id="@+id/icons_health_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:paddingLeft="6dp" android:src="@drawable/icons_health_back" /> <TextView android:id="@+id/title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:gravity="center" android:text="健康" android:textColor="#ffffff" android:textSize="15sp" /> </LinearLayout>
pagerofhealth.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/wvHealth"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<include layout="@layout/health_layout_titlebar"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 健康新知 -->
<TextView
android:id="@+id/tv_healthnews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:padding="6dp"
android:text="@string/healthnews"
android:textSize="18dp"/>
<!-- 疾病防治 -->
<TextView
android:id="@+id/tv_healthill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:padding="6dp"
android:text="@string/illdefense"
android:textSize="18dp"/>
</LinearLayout>
<TextView
android:id="@+id/cursor"
android:layout_width="125dp"
android:layout_height="5dp"
android:layout_marginLeft="20dp"
android:background="#990033"/>
<android.suqqort.v4.view.ViewPage
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
增加权限: <uses-permission android:name="android.permission.INTERNET" />
相关文章推荐
- 小案例-健康栏目的实现分析
- 案例2:健康栏目的实现
- 用android来实现图片的绘制以及旋转缩放案例分析
- 【典型案例】NASA使用MODIS 和在线影像分析技术实现全球洪水监测
- nginx_lua案例分析:动态路由实现
- 实现采用客户/服务器通信模式,基于TCP网络通信协议的多客户端简单应用之案例分析
- 网站日志统计案例分析与实现
- 管理软件预警通知(Notification)功能的实现案例分析
- Java SpringMVC实现国际化整合案例分析(i18n)
- servlet交互案例分析及终极实现 使用jdbc连接数据库实现
- 使用JDBC连接MySQL数据库--典型案例分析(九)----财务帐号的DAO设计及其实现
- 基于百度地图记录运动轨迹案例分析与实现
- struts2拦截器实现原理案例分析
- cas sso ajax的jsonp实现方案总结(新浪微薄、淘宝案例分析)
- 回溯算法的一些案例分析(c代码实现)
- 使用JDBC连接MySQL数据库--典型案例分析(六)----实现账户转账操作
- 用android来实现图片的绘制以及旋转缩放案例分析
- javascript-解析xml文件-在html中实现二级联动分析及案例
- 案例分析-实现用户名唯一的验证操作
- Hadoop经典案例Spark实现(一)——通过采集的气象数据分析每年的最高温度