ViewPager与pilltoRefresh(上下拉加载)与webview

//主方法

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.moniyuekao_20171220_2.Adapter.MyAdapter;
import com.example.moniyuekao_20171220_2.Adapter.MyFragAdapter;
import com.example.moniyuekao_20171220_2.Utils.JieXiUtils;
import com.example.moniyuekao_20171220_2.bean.Bean;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;

import java.util.List;

public class MainActivity extends AppCompatActivity{

    private String path="http://v.juhe.cn/toutiao/index?type=top&key=dbedecbcd1899c9785b95cc2d17131c5";
    //private String path="http://v.juhe.cn/toutiao/index?type=top&key=f628e84fadf97a32dfc6323c7d89e15b";
    private ViewPager vp;
    private PullToRefreshListView plv;
    private PullToRefreshScrollView psv;
    private List<Bean.ResultBean.DataBean> data;
    private MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp=(ViewPager)findViewById(R.id.vp);
        plv=(PullToRefreshListView)findViewById(R.id.plv);
        //plv=(ListView)findViewById(R.id.plv);
        //psv=(PullToRefreshScrollView)findViewById(R.id.psv);
        new AsyncTask<String,String,String>(){

            @Override
            protected String doInBackground(String... params) {
                String getstr = JieXiUtils.getstr(path);
                return getstr;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson=new Gson();
                Bean bean = gson.fromJson(s, Bean.class);
                data = bean.getResult().getData();
                adapter=new MyAdapter(MainActivity.this,data);
                plv.setAdapter(adapter);
                //webview点击加载网页
                plv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent in=new Intent(MainActivity.this,Main2Activity.class);
                        in.putExtra("url",data.get(position).getUrl());
                        startActivity(in);
                    }
                });
                //下拉加载
                plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
                    @Override
                    public void onRefresh(PullToRefreshBase<ListView> pullToRefreshBase) {
                        new Thread(){
                            @Override
                            public void run() {
                                super.run();
                                String getstrs = JieXiUtils.getstr(path);
                                Gson gson=new Gson();
                                Bean bean = gson.fromJson(getstrs, Bean.class);
                                final List<Bean.ResultBean.DataBean> todata = bean.getResult().getData();
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        data.addAll(0,todata);
                                        if(adapter!=null){
                                            adapter.notifyDataSetChanged();
                                            plv.onRefreshComplete();
                                        }
                                    }
                                });
                            }
                        }.start();

                    }
                });
                //上拉刷新
//                plv.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
//                    @Override
//                    public void onLastItemVisible() {
//                        
//                    }
//                });
                vp.setAdapter(new MyFragAdapter(MainActivity.this,data));
                vp.setCurrentItem(100000);
                initData();
            }

            public Handler handle=new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    int item = vp.getCurrentItem();
                    item++;
                    vp.setCurrentItem(item);
                    initData();
                }
            };
            private void initData() {
                handle.sendEmptyMessageDelayed(1,2000);
            }
        }.execute();
    }
}
 
//主方法布局
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:id="@+id/psv"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:id="@+id/vp"></android.support.v4.view.ViewPager>
     <com.handmark.pulltorefresh.library.PullToRefreshListView
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:id="@+id/plv"></com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>
    </RelativeLayout>
<!--<com.example.day20171218_yuekaomoni_1.MyListView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:id="@+id/plv"></com.example.day20171218_yuekaomoni_1.MyListView>-->
<!--<com.handmark.pulltorefresh.library.PullToRefreshScrollView-->
<!--xmlns:android="http://schemas.android.com/apk/res/android"-->
<!--xmlns:app="http://schemas.android.com/apk/res-auto"-->
<!--android:layout_width="match_parent"-->
<!--android:id="@+id/psv"-->
<!--android:layout_height="match_parent">-->

//第二个页面(也就是webview页面)
//方法

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;

/**
 * Created by lenovo on 2017/12/20.
 */

public class Main2Activity extends AppCompatActivity{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Intent intent = getIntent();
        String url = intent.getStringExtra("url");
        WebView wv = (WebView) findViewById(R.id.wv);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.loadUrl(url);
    }
}
//布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
        android:id="@+id/wv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"></WebView>

</LinearLayout>
//ViewPage适配器
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.moniyuekao_20171220_2.bean.Bean;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by lenovo on 2017/12/20.
 */

public class MyFragAdapter extends PagerAdapter{

    private Context con;
    private List<Bean.ResultBean.DataBean> list;

    public MyFragAdapter(Context con,List<Bean.ResultBean.DataBean> list) {
        this.con = con;
        this.list = list;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView img=new ImageView(con);
        ImageLoader.getInstance().displayImage(list.get(position%list.size()).getThumbnail_pic_s(),img);
        container.addView(img);
        return img;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
//ListView适配器
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.moniyuekao_20171220_2.R;
import com.example.moniyuekao_20171220_2.bean.Bean;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by lenovo on 2017/12/20.
 */

public class MyAdapter extends BaseAdapter{

    private Context con;
    private List<Bean.ResultBean.DataBean> list;
    public MyAdapter(Context con,List<Bean.ResultBean.DataBean> list) {
        this.con = con;
        this.list = list;
    }

    @Override
    public int getItemViewType(int position) {
        if(list.get(position).getThumbnail_pic_s03()!=""&&list.get(position).getThumbnail_pic_s02()!=""){
                     return 0;
        }else if(list.get(position).getThumbnail_pic_s02()!=""){
                     return 1;
        }else {
                    return 2;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        int type = getItemViewType(position);
        switch (type){
            case 0:
                if(convertView==null){
                    convertView=View.inflate(con, R.layout.layout_xx1,null);
                    holder=new ViewHolder();
                    holder.img1=(ImageView)convertView.findViewById(R.id.img_1_1);
                    holder.img2=(ImageView)convertView.findViewById(R.id.img_1_2);
                    holder.img3=(ImageView)convertView.findViewById(R.id.img_1_3);
                    holder.name=(TextView)convertView.findViewById(R.id.text1);
                    convertView.setTag(holder);
                }else {
                    holder=(ViewHolder)convertView.getTag();
                }
                    holder.name.setText(list.get(position).getTitle());
                ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder.img1);
                ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s02(),holder.img2);
                ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s03(),holder.img3);
                break;
            case 1:
                if(convertView==null){
                    convertView=View.inflate(con, R.layout.layout_xx2,null);
                    holder=new ViewHolder();
                    holder.img1=(ImageView)convertView.findViewById(R.id.img_2_1);
                    holder.img2=(ImageView)convertView.findViewById(R.id.img_2_2);
                    holder.name=(TextView)convertView.findViewById(R.id.text2);
                    convertView.setTag(holder);
                }else {
                    holder=(ViewHolder)convertView.getTag();
                }
                holder.name.setText(list.get(position).getTitle());
                ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder.img1);
                ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s02(),holder.img2);
                break;
            case 2:
                if(convertView==null){
                    convertView=View.inflate(con, R.layout.layout_xx1,null);
                    holder=new ViewHolder();
                    holder.img1=(ImageView)convertView.findViewById(R.id.img_3);
                    holder.name=(TextView)convertView.findViewById(R.id.text3);
                    convertView.setTag(holder);
                }else {
                    holder=(ViewHolder)convertView.getTag();
                }
                holder.name.setText(list.get(position).getTitle());
                ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder.img1);
                break;
        }
        return convertView;
    }
    public class ViewHolder{
        private TextView name;
        private ImageView img1;
        private ImageView img2;
        private ImageView img3;
    }
}

//多条目
//条目一
//布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text1"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/img_1_1"/>
        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/img_1_2"/>
        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/img_1_3"/>
    </LinearLayout>

</LinearLayout>
//条目二布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text2"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/img_2_1"/>
        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:id="@+id/img_2_2"/>
    </LinearLayout>
</LinearLayout>
//条目三布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text3"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/img_3"/>
</LinearLayout>
//获取gson串
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by lenovo on 2017/12/20.
 */

public class JieXiUtils {
    private static ByteArrayOutputStream baos;

    public static String getstr(String path){
        try {
            URL url=new URL(path);
            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setConnectTimeout(3000);
            int code = urlConnection.getResponseCode();
            if(code==200){
                InputStream stream = urlConnection.getInputStream();
                baos=new ByteArrayOutputStream();
                int len=0;
                byte[] arr=new byte[1024*50];
                while ((len=stream.read(arr))!=-1){
                    baos.write(arr,0,len);
                }
                return baos.toString();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
//配置文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.moniyuekao_20171220_2">

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:name=".Application.ImageApplication"
        android:allowBackup="true" android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true" android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Main2Activity"></activity>
    </application>

</manifest>
//依赖
compile 'com.github.userswlwork:pull-to-refresh:1.0.0'
//上下拉
//补充一下
//下面的是一个可以全局加载的代码内容(自定义了一个listview,在主布局文件里将listview改为包名加类名,
//将总布局改为com.handmark.pulltorefresh.library.PullToRefreshScrollView,其他不变即可)
public class MyListView extends ListView{
    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int i = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);

        super.onMeasure(widthMeasureSpec, i);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值