智慧北京开发第四天(下)

ListView点击封装,查看新闻条目的详情

TabDetailPager.java
@Override
    public View initView() {
        View view = View.inflate(mActivity, R.layout.tab_detail_pager, null);
        newTopView = View.inflate(mActivity, R.layout.new_top_ads, null);
        ViewUtils.inject(this, view);
        ViewUtils.inject(this, newTopView);
        //将头条新闻以头布局的形式加给listview
        lvTabList.addHeaderView(newTopView);
        //设置下拉刷新监听
        lvTabList.setOnRefreshListener(new RefreshListView.OnRefreshListener() {
            @Override
            public void onReFresh() {
                getDataFromServer();
            }

            @Override
            public void onLoadMore() {
                if(mMoreUrl != null){
                    getMoreDataFromServer();
                }else{
                    Toast.makeText(mActivity,"最后一页了",Toast.LENGTH_LONG).show();
                    lvTabList.onRefreshComplete(false);//收起加载更多的布局

                }
            }
        });

        lvTabList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                System.out.println("被点击:"+position);

                //跳转新闻详情页
                Intent intent = new Intent();
                intent.setClass(mActivity, NewsDetailActivity.class);
                intent.putExtra("url",tabNewsDatas.get(position).url);
                mActivity.startActivity(intent);
            }
        });
        return view;
    }
RefreshListView.java
OnItemClickListener mItemClickListener;
    //对这个ListView中item的点击事件进行包装,那样的话拿到的第一个item的position就是0,否则是2(因为ListView中前面添加了两个头)
    @Override
    public void setOnItemClickListener(OnItemClickListener listener) {
        super.setOnItemClickListener(this);//这里传我们包装的OnItemClickListener
        mItemClickListener = listener;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(mItemClickListener!=null){
            mItemClickListener.onItemClick(parent, view, position - getHeaderViewsCount(), id);
        }
    }
activity_news_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/title_red_bg">

        <ImageButton
            android:id="@+id/btn_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:src="@drawable/back" />

        <ImageButton
            android:id="@+id/btn_share"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:background="@null"
            android:src="@drawable/icon_share" />

        <ImageButton
            android:id="@+id/btn_size"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:layout_toLeftOf="@id/btn_share"
            android:background="@null"
            android:src="@drawable/icon_textsize" />
    </RelativeLayout>


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

</LinearLayout>
效果图:
NewsDetailActivity.java
package com.xbmu.wisdombj;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class NewsDetailActivity extends Activity {
    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);
        mWebView = (WebView) findViewById(R.id.wv_web);

        String url = getIntent().getStringExtra("url");
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);//表示支持js
        settings.setBuiltInZoomControls(true);//显示放大缩小按钮
        settings.setUseWideViewPort(true);//支持双击缩放
        mWebView.loadUrl(url);//加载网页
    }

}
运行效果:

标记已读:

TabDetailPager.java
lvTabList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                System.out.println("被点击:" + position);
                // 35311,34221,34234,34342
                //在本地记录已读状态
                String ids = SharePreferenceUtils.getString(mActivity,"read_ids","");
                String readId = tabNewsDatas.get(position).id;
                if(!ids.contains(readId)){
                    ids = ids + readId + ",";
                    SharePreferenceUtils.putString(mActivity,"read_ids",ids);
                }
                newsItemAdapter.notifyDataSetChanged();

                //跳转新闻详情页
                Intent intent = new Intent();
                intent.setClass(mActivity, NewsDetailActivity.class);
                intent.putExtra("url", tabNewsDatas.get(position).url);
                mActivity.startActivity(intent);
            }
        });

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = View.inflate(mActivity, R.layout.new_list_item, null);
                viewHolder = new ViewHolder();
                viewHolder.ivNewIcon = (ImageView) convertView.findViewById(R.id.iv_pic);
                viewHolder.tvNewTitle = (TextView) convertView.findViewById(R.id.tv_title);
                viewHolder.tvNewDate = (TextView) convertView.findViewById(R.id.tv_date);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.tvNewTitle.setText(getItem(position).title);
            viewHolder.tvNewDate.setText(getItem(position).pubdate);
            bitmapUtils.display(viewHolder.ivNewIcon, getItem(position).listimage);

            String ids = SharePreferenceUtils.getString(mActivity,"read_ids","");
            if(ids.contains(getItem(position).id)){
                viewHolder.tvNewTitle.setTextColor(Color.GRAY);
            }else{
                viewHolder.tvNewTitle.setTextColor(Color.BLACK);
            }

            return convertView;
        }
SharePreferenceUtils.java

package com.xbmu.wisdombj.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * 专门访问和设置SharePreference的工具类,保存和配置一些设置信息
 * Created by Administrator on 2016/2/4 0004.
 */
public class SharePreferenceUtils {
    private static final String SHARE_PREFS_NAME = "config";
    private static SharedPreferences mSharedPreferences;
    public static void putBoolean(Context ctx,String key,boolean value){
        if(mSharedPreferences == null){
            mSharedPreferences = ctx.getSharedPreferences(SHARE_PREFS_NAME,Context.MODE_PRIVATE);
        }
        mSharedPreferences.edit().putBoolean(key,value).commit();
    }
    public static Boolean getBoolean(Context ctx,String key,boolean defaultValue){
        if(mSharedPreferences == null){
            mSharedPreferences = ctx.getSharedPreferences(SHARE_PREFS_NAME,Context.MODE_PRIVATE);
        }
        return mSharedPreferences.getBoolean(key,defaultValue);
    }
    public static void putString(Context ctx,String key,String value){
        if(mSharedPreferences == null){
            mSharedPreferences = ctx.getSharedPreferences(SHARE_PREFS_NAME,Context.MODE_PRIVATE);
        }
        mSharedPreferences.edit().putString(key, value).commit();
    }
    public static String getString(Context ctx,String key,String defaultValue){
        if(mSharedPreferences == null){
            mSharedPreferences = ctx.getSharedPreferences(SHARE_PREFS_NAME,Context.MODE_PRIVATE);
        }
        return mSharedPreferences.getString(key,defaultValue);
    }
}
运行效果:

优化新闻详情页&Webview介绍

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/title_red_bg" >

        <ImageButton
            android:id="@+id/btn_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:src="@drawable/back" />

        <ImageButton
            android:id="@+id/btn_share"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:background="@null"
            android:src="@drawable/icon_share" />

        <ImageButton
            android:id="@+id/btn_size"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dp"
            android:layout_toLeftOf="@id/btn_share"
            android:background="@null"
            android:src="@drawable/icon_textsize" />
    </RelativeLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

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

        <ProgressBar
            android:id="@+id/pb_progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:indeterminateDrawable="@drawable/custom_progress" />
    </FrameLayout>

</LinearLayout>
NewsDetailActivity.java
package com.xbmu.wisdombj;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ProgressBar;

public class NewsDetailActivity extends Activity implements View.OnClickListener{
    private WebView mWebView;
    private ImageButton btnBack;
    private ImageButton btnSize;
    private ImageButton btnShare;

    private ProgressBar pbProgress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);
        mWebView = (WebView) findViewById(R.id.wv_web);
        btnBack = (ImageButton) findViewById(R.id.btn_back);
        btnSize = (ImageButton) findViewById(R.id.btn_size);
        btnShare = (ImageButton) findViewById(R.id.btn_share);

        btnBack.setOnClickListener(this);
        btnSize.setOnClickListener(this);
        btnShare.setOnClickListener(this);

        pbProgress = (ProgressBar) findViewById(R.id.pb_progress);

        String url = getIntent().getStringExtra("url");
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);//表示支持js
        settings.setBuiltInZoomControls(true);//显示放大缩小按钮
        settings.setUseWideViewPort(true);//支持双击缩放

        mWebView.setWebViewClient(new WebViewClient() {
            /**
             * 网页开始加载
             * @param view
             * @param url
             * @param favicon
             */
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                System.out.println("网页开始加载");
                pbProgress.setVisibility(View.VISIBLE);

            }

            /**
             * 网页加载结束
             * @param view
             * @param url
             */
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                System.out.println("网页开始结束");
                pbProgress.setVisibility(View.GONE);
            }

            /**
             *所有跳转的链接都毁在此方法中回调
             * @param view
             * @param url
             * @return
             */
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                System.out.println("跳转url:" + url);
                view.loadUrl(url);
                return true;
//                return super.shouldOverrideUrlLoading(view, url);

            }
        });

//        mWebView.goBack();
        mWebView.setWebChromeClient(new WebChromeClient(){
            /**
             * 进度发生变化
             * @param view
             * @param newProgress
             */
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                System.out.println("加载进度:"+newProgress);
                super.onProgressChanged(view, newProgress);
            }

            /**
             * 获取网页标题
             * @param view
             * @param title
             */
            @Override
            public void onReceivedTitle(WebView view, String title) {
                System.out.println("网页标题:"+title);
                super.onReceivedTitle(view, title);
            }
        });
        mWebView.loadUrl(url);//加载网页

    }

    @Override
    public void onClick(View v) {

    }
}
运行效果:

处理新闻详情页上面的几个按钮的监听事件

NewsDetailActivity.java
package com.xbmu.wisdombj;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ProgressBar;

public class NewsDetailActivity extends Activity implements View.OnClickListener{
    private WebView mWebView;
    private ImageButton btnBack;
    private ImageButton btnSize;
    private ImageButton btnShare;

    private ProgressBar pbProgress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);
        mWebView = (WebView) findViewById(R.id.wv_web);
        btnBack = (ImageButton) findViewById(R.id.btn_back);
        btnSize = (ImageButton) findViewById(R.id.btn_size);
        btnShare = (ImageButton) findViewById(R.id.btn_share);

        btnBack.setOnClickListener(this);
        btnSize.setOnClickListener(this);
        btnShare.setOnClickListener(this);

        pbProgress = (ProgressBar) findViewById(R.id.pb_progress);

        String url = getIntent().getStringExtra("url");
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);//表示支持js
        settings.setBuiltInZoomControls(true);//显示放大缩小按钮
        settings.setUseWideViewPort(true);//支持双击缩放

        mWebView.setWebViewClient(new WebViewClient() {
            /**
             * 网页开始加载
             * @param view
             * @param url
             * @param favicon
             */
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                System.out.println("网页开始加载");
                pbProgress.setVisibility(View.VISIBLE);

            }

            /**
             * 网页加载结束
             * @param view
             * @param url
             */
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                System.out.println("网页开始结束");
                pbProgress.setVisibility(View.GONE);
            }

            /**
             *所有跳转的链接都毁在此方法中回调
             * @param view
             * @param url
             * @return
             */
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                System.out.println("跳转url:" + url);
                view.loadUrl(url);
                return true;
//                return super.shouldOverrideUrlLoading(view, url);

            }
        });

//        mWebView.goBack();
        mWebView.setWebChromeClient(new WebChromeClient(){
            /**
             * 进度发生变化
             * @param view
             * @param newProgress
             */
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                System.out.println("加载进度:"+newProgress);
                super.onProgressChanged(view, newProgress);
            }

            /**
             * 获取网页标题
             * @param view
             * @param title
             */
            @Override
            public void onReceivedTitle(WebView view, String title) {
                System.out.println("网页标题:"+title);
                super.onReceivedTitle(view, title);
            }
        });
        mWebView.loadUrl(url);//加载网页

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_size:
                showChooseDialog();
                break;
            case R.id.btn_share:
                break;

        }
    }
    private int mCurrentChooseItem;//记录当前选中的item,点击确定前
    private int mCurrentItem = 2;//记录当前选中的item,点击确定后
    /**
     * 显示选择对话框
     */
    private void showChooseDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        String[] items = new String[] { "超大号字体", "大号字体", "正常字体", "小号字体",
                "超小号字体" };
        builder.setTitle("字体设置");
        builder.setSingleChoiceItems(items, mCurrentItem, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                System.out.println("选中:" + which);
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                WebSettings settings = mWebView.getSettings();
                switch (mCurrentChooseItem) {
                    case 0:
                        settings.setTextSize(WebSettings.TextSize.LARGEST);

                        break;
                    case 1:
                        settings.setTextSize(WebSettings.TextSize.LARGER);
                        break;
                    case 2:
                        settings.setTextSize(WebSettings.TextSize.NORMAL);
                        break;
                    case 3:
                        settings.setTextSize(WebSettings.TextSize.SMALLER);
                        break;
                    case 4:
                        settings.setTextSize(WebSettings.TextSize.SMALLEST);
                        break;

                    default:
                        break;
                }
                mCurrentItem = mCurrentChooseItem;
            }
        });
        builder.setNegativeButton("取消",null);
        builder.show();
    }
}
运行效果:
智慧北京开发第四天(下)_第1张图片















你可能感兴趣的:(智慧北京app)