健康栏目
来源:互联网 发布:serializearray转json 编辑:程序博客网 时间:2024/04/29 16:33
一、运行效果图
二、核心代码
healthactivity.java
package com.example.healthprograma;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.database.Cursor;import android.os.Bundle;import android.support.v4.view.ViewPager;import android.support.v4.view.ViewPager.OnPageChangeListener;import android.util.DisplayMetrics;import android.view.Display;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.LayoutParams;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class HealthActivity extends Activity{private int currIndex;// 当前页卡编号private TextView tvCursor;private TextView tvHealthNews;private TextView tvIllnessDefense;private ViewPager vpHealth;private ImageView ivHealthBack;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_health);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(){@Overridepublic void onClick(View arg0){finish();}});// 点击健康须知tvHealthNews.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View view){vpHealth.setCurrentItem(0);}});// 点击疾病防治tvIllnessDefense.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View view){vpHealth.setCurrentItem(1);}});vpHealth.setOnPageChangeListener(new OnPageChangeListener(){@Overridepublic void onPageSelected(int position){currIndex = position;}@Overridepublic 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);}@Overridepublic void onPageScrollStateChanged(int position){}});}}HealthViewPagerAadapter.java
package com.example.healthprograma;import java.util.List;import android.support.v4.view.PagerAdapter;import android.view.View;import android.view.ViewGroup;public class HealthViewPagerAdapter extends PagerAdapter{private List<View> viewList;public HealthViewPagerAdapter(List<View> viewList){this.viewList = viewList;}@Overridepublic int getCount(){return viewList.size();}@Overridepublic boolean isViewFromObject(View view, Object object){return view == object;}@Overridepublic Object instantiateItem(ViewGroup container, int position){container.addView(viewList.get(position));return viewList.get(position);}@Overridepublic void destroyItem(ViewGroup container, int position, Object object){container.removeView(viewList.get(position));}}
HealthWebView.java
package com.example.healthprograma;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.webkit.WebView;import android.webkit.WebViewClient;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(){@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url){view.loadUrl(url);return true;}});return view;}}
<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=".HealthActivity" > <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="18sp" /> <!-- 疾病防治 --> <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="18sp" /> </LinearLayout> <TextView android:id="@+id/cursor" android:layout_width="125dp" android:layout_height="5dp" android:layout_marginLeft="20dp" android:background="#990033" /> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>
<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=".HealthActivity" > <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="18sp" /> <!-- 疾病防治 --> <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="18sp" /> </LinearLayout> <TextView android:id="@+id/cursor" android:layout_width="125dp" android:layout_height="5dp" android:layout_marginLeft="20dp" android:background="#990033" /> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>
<?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>
三、运行中遇到的问题
1 0
- 健康栏目
- 健康栏目
- 健康栏目
- 健康栏目
- 健康栏目
- 健康栏目
- 健康栏目的实现
- 健康栏目的实现
- 健康栏目的实现
- 健康栏目的实现
- 健康栏目的实现
- 健康栏目实现
- 健康栏目的实现
- 实验十三 健康栏目 WebView
- Walker之健康栏目的实现
- 小案例-健康栏目的实现分析
- 案例2:健康栏目的实现
- 案例2_健康栏目的实现分析
- 进行数据的访问(mxd、shapefile、gdb、mdb、栅格图像)
- 我眼中的架构师:一个优秀的架构师应该具备什么?
- java 对象数组的排序(实现Comparable接口)和二分查找
- 23种设计模式
- ajaxfileupload.js类实现异步文件上传
- 健康栏目
- 【无人车研究】A*算法实现路径规划
- 多用户 xdebug 远程调试 PhpStorm
- Maven项目上总有一个小红叉,但是其他地方没有,这是为什么?
- 决策树过拟合问题的解决思路
- leetcode数组(一)
- HDOJ 1215 统计难题(字典树)
- 小店购物 JSOI2008 BZOJ 2260
- win7 64位机器xampp安装成功,打开localhost却显示IIS7的问题解决方案。