近期需求需要listview嵌套一个webview,虽然网上有很多例子了,但是自己还是作下笔记吧

其实就是listview里面添加一个带webview的头部,然后处理下滑动事件

首先首页布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.com.listwebview.demo.MainActivity">

    <ListView android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="#595958"/>
</FrameLayout>
View Code

MainActivity

package cn.com.listwebview.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private Adapter adapter = null;
    private List<String> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listview);
        for(int i = 0;i < 20; i++){
            list.add(""+i);
        }
        adapter = new Adapter(MainActivity.this,R.layout.item_line,list);
        View view = LayoutInflater.from(this).inflate(R.layout.item_head,null);
        WebView webView = (WebView)view.findViewById(R.id.webView);

        webView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP)
                    listView.requestDisallowInterceptTouchEvent(false);
                else
                    listView.requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        webView.loadUrl("http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0519/2892.html");
        listView.setAdapter(adapter);
        listView.addHeaderView(view,null,false);
    }
}
View Code
public class Adapter extends ArrayAdapter<String> {

    private List<String> list;
    private int resource;
    private Context context;
    private LayoutInflater inflater;

    public Adapter(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
        this.context = context;
        this.resource = resource;
        this.list = objects;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        final String val = getItem(position);
        if (convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(resource, null);
            holder.tv_val = (TextView) convertView.findViewById(R.id.tv_val);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_val.setText(val);
        return convertView;
    }

    static class ViewHolder{
        TextView tv_val;
    }

}
View Code

head布局

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

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="400dp" />

</LinearLayout>
View Code

 

最后不要忘记了权限,不然无法显示,这个很容易会忘记的

<uses-permission android:name="android.permission.INTERNET" />
posted on 2015-02-03 13:45  翻滚的咸鱼  阅读(1274)  评论(0编辑  收藏  举报