健康栏目

本博客介绍了一款健康资讯应用的实现细节,包括使用WebView加载网页内容,并通过ViewPager实现页面切换功能。具体涉及到了HealthWebView类的实现,以及如何在MainActivity中设置ViewPager和监听器。
HealthWebView.java
package com.example.health;  
  
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);  
    webView.getSettings().setJavaScriptEnabled(true);  
    webView.setWebViewClient(new WebViewClient(){  
        public boolean shouldOverrideUrlLoading(WebView view,String url){  
            view.loadUrl(url);  
            return true;  
        }  
    });  
    return view;  
}  
}  


HealthViewPagerAdapter.java

package com.example.health;  
  
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;  
}  
@Override  
public int getCount()  
{  
    // TODO Auto-generated method stub  
    return viewList.size();  
}  
@Override  
public boolean isViewFromObject(View view, Object object)  
{  
    // TODO Auto-generated method stub  
    return view==object;  
}  
@Override  
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));  
}  
  
  
  
}  


mainactivity.java

package com.example.health;  
  
import java.util.ArrayList;  
import java.util.List;  
  
import android.app.ActionBar.LayoutParams;  
import android.app.Activity;  
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.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.ImageView;  
import android.widget.LinearLayout;  
import android.widget.TextView;  
  
public class MainActivity extends Activity  
{  
  
    private int currIndex;  
    private TextView tvCursor;  
    private TextView tvHealthNews;  
    private TextView tvIllnessDefense;  
    private ViewPager vpHealth;  
    private ImageView ivHealthBack;  
    protected void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        initViews();  
        initCursor();  
        initViewPager();  
        setListeners();  
    }  
    private void initViews()  
    {  
        ivHealthBack=(ImageView) findViewById(R.id.icon_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));  
        vpHealth.setCurrentItem(0);  
    }  
  
    private void setListeners()  
    {  
        ivHealthBack.setOnClickListener(new OnClickListener(){  
        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) {  
                // TODO Auto-generated method stub  
                vpHealth.setCurrentItem(1);  
            }  
          
        });  
        vpHealth.setOnPageChangeListener(new OnPageChangeListener(){  
        public void onPageSelected(int position){  
            currIndex=position;  
        }  
        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);  
        }  
        public void onPageScrollStateChanged(int position){  
              
        }  
    });  
          
    }  
  
} 

<?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/icon_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>  


pagerofhealthy.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>  
healty_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/icon_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>  



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值