获取数据 跳转轮播

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="30sp"
        android:text="欢迎来到八维"/>


</RelativeLayout>

package com.example.newss;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    private SharedPreferences sp;
    private Intent intent;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==123){
                sp.edit().putBoolean("is_first",false).commit();
                startActivity(intent);
                finish();
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sp = getSharedPreferences("flag",MODE_PRIVATE);
        intent = new Intent(MainActivity.this,TwoActivity.class);
        boolean isfirst=sp.getBoolean("is_first",true);
        if(isfirst){
            handler.sendEmptyMessageDelayed(123,5000);
        }else{
            startActivity(intent);
            finish();
        }
    }
}

package com.example.newss;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.IDN;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by 。。。。 on 2018/9/13.
 */

public class HttpUtils {
    public static String getHttpFroment(String urlstring){
        String result="";
        try {
            URL url=new URL(urlstring);
            HttpURLConnection connection= (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            int code=connection.getResponseCode();
            if(code==200){
                InputStream is=connection.getInputStream();
                result=jeixi(is);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    private static String jeixi(InputStream is) {
        String result="";
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        byte[] b=new byte[1024];
        int count=-1;
        try {
            while ((count=is.read(b,0,b.length))!=-1){
                baos.write(b,0,count);
                baos.flush();
            }
            result=baos.toString();
            baos.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}

<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">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>

</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">

   <com.bwie.xlistview.XListView
       android:id="@+id/xlistview"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"></com.bwie.xlistview.XListView>


</LinearLayout>

package com.example.newss;

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.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by 。。。。 on 2018/9/13.
 */

public class NewsAdapter extends BaseAdapter {
    private Context context;
    private List<News.ResultBean.DataBean> list;

    public NewsAdapter(Context context, List<News.ResultBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @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 int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        News.ResultBean.DataBean bean=list.get(position);
        if(bean.getType().equals("a")){
            return 0;
        }else{
            return 1;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder1 holder1=null;
        ViewHolder2 holder2=null;
        int type=getItemViewType(position);
        switch (type){
            case 0:
                if(convertView==null){
                    holder1=new ViewHolder1();
                    convertView=View.inflate(context,R.layout.item_one,null);
                    holder1.img=convertView.findViewById(R.id.img);
                    holder1.title1=convertView.findViewById(R.id.title);
                    convertView.setTag(holder1);
                }else{
                    holder1= (ViewHolder1) convertView.getTag();
                }
                ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder1.img);
                holder1.title1.setText(list.get(position).getTitle());
                break;
            case 1:
                if(convertView==null){
                    holder2=new ViewHolder2();
                    convertView=View.inflate(context,R.layout.item_two,null);
                    holder2.img1=convertView.findViewById(R.id.img1);
                    holder2.img2=convertView.findViewById(R.id.img2);
                    holder2.img3=convertView.findViewById(R.id.img3);
                    holder2.title1=convertView.findViewById(R.id.title);
                    convertView.setTag(holder2);
                }else{
                    holder2= (ViewHolder2) convertView.getTag();
                }
                ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder2.img1);
                ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s02(),holder2.img2);
                ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s03(),holder2.img3);
                holder2.title1.setText(list.get(position).getTitle());
                break;
        }
        return convertView;
    }
    class ViewHolder1{
        ImageView img;
        TextView title1;
    }
    class ViewHolder2{
        ImageView img1;
        ImageView img2;
        ImageView img3;
        TextView title1;
    }
}
package com.example.newss;

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.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by 。。。。 on 2018/9/14.
 */

class NewsTwoAdaptern extends BaseAdapter {
    private Context context;
    private List<NewsTwo.BodyBean.ResultBean> list;

    public NewsTwoAdaptern(Context context, List<NewsTwo.BodyBean.ResultBean> list) {
        this.context = context;
        this.list = list;
    }

    @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) {
        ViewHolder1 holder1=null;
        if(convertView==null){
            holder1=new ViewHolder1();
            convertView=View.inflate(context,R.layout.item_one,null);
            holder1.img=convertView.findViewById(R.id.img);
            holder1.title1=convertView.findViewById(R.id.title);
            convertView.setTag(holder1);
        }else{
            holder1= (ViewHolder1) convertView.getTag();
        }
        ImageLoader.getInstance().displayImage(list.get(position).getBarnerPic(),holder1.img);
        holder1.title1.setText(list.get(position).getBarnerName());
        return convertView;
    }
    class ViewHolder1{
        ImageView img;
        TextView title1;
    }
}

package com.example.newss;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;

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


public class OneFragment extends Fragment {
    private List<News.ResultBean.DataBean> list;
    private NewsAdapter adapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment_one, container, false);
        ListView listView=v.findViewById(R.id.listview);
        list = new ArrayList<>();
        adapter = new NewsAdapter(getActivity(),list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(),ThreeActivity.class);
                String url = list.get(position).getUrl();
                intent.putExtra("url",url);
                startActivity(intent);
            }
        });
        return v;

    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        new AsyncTask<String, Integer, String>() {
            @Override
            protected String doInBackground(String... strings) {
                String result=HttpUtils.getHttpFroment(strings[0]);
                return result;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson=new Gson();
                News news=gson.fromJson(s,News.class);
                if(news.getResult().getData()!=null){
                    list.clear();
                    list.addAll(news.getResult().getData());
                    adapter.notifyDataSetChanged();
                }
            }
        }.execute("http://172.17.8.100/mobile/exam/findNewList");
    }

}

package com.example.newss;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bwie.xlistview.XListView;
import com.google.gson.Gson;

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


public class TwoFragment extends Fragment {

    private XListView xListView;
    private List<NewsTwo.BodyBean.ResultBean> list;
    private NewsTwoAdaptern adaptern;
    private int currentPage=1;
    Handler handler=new Handler();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v=inflater.inflate(R.layout.fragment_two, container, false);
        xListView = v.findViewById(R.id.xlistview);
        list = new ArrayList<>();
        adaptern = new NewsTwoAdaptern(getActivity(), list);
        xListView.setAdapter(adaptern);
        xListView.setPullRefreshEnable(true);
        xListView.setPullLoadEnable(true);
       xListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               Intent intent=new Intent(getActivity(),ThreeActivity.class);
               String url = list.get((int) id).getUrl();
               intent.putExtra("url",url);
               startActivity(intent);
           }
       });
        xListView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                currentPage=1;
                getData(currentPage,false);
            }

            @Override
            public void onLoadMore() {
                currentPage++;
                getData(currentPage,true);
            }
        });
        return v;
    }



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getData(1,false);
    }

    private void getData(int i, final boolean b) {
        new AsyncTask<String, Integer, String>() {
            @Override
            protected String doInBackground(String... strings) {
                String result= HttpUtils.getHttpFroment(strings[0]);
                return result;
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                if(!TextUtils.isEmpty(s)){
                    Gson gson=new Gson();
                    NewsTwo newsTwo=gson.fromJson(s,NewsTwo.class);
                    if(newsTwo!=null){
                        if(newsTwo.getBody()!=null){
                            if(newsTwo.getBody().getResult()!=null){
                                if(!b) {
                                    list.clear();
                                }
                                list.addAll(newsTwo.getBody().getResult());
                                adaptern.notifyDataSetChanged();
                            }
                        }
                    }
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if(b){
                                xListView.stopLoadMore();
                            }else{
                                xListView.stopRefresh();
                                xListView.setRefreshTime("刚刚");
                            }
                        }
                    },2000);
                }
            }
        }.execute("https://api.yunxuekeji.cn/yunxue_app_api/content/getIndexBanner/"+currentPage);
    }
}

package com.example.newss;

import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.bwie.xlistview.XListView;
import com.google.gson.Gson;

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

public class TwoActivity extends AppCompatActivity {

    private List<Fragment> list;
    private int page=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_two);
        final TextView one=findViewById(R.id.one);
        final ViewPager pager=findViewById(R.id.pager);
        list = new ArrayList<>();
        OneFragment oneFragment=new OneFragment();
        TwoFragment twoFragment=new TwoFragment();
        list.add(oneFragment);
        list.add(twoFragment);
        one.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (page==0){
                    page=1;
                    pager.setCurrentItem(1);
                }else {
                    page=0;
                    pager.setCurrentItem(0);
                }
            }
        });
        pager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="300dp"></android.support.v4.view.ViewPager>
    <WebView
        android:id="@+id/wv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></WebView>
</LinearLayout>

package com.example.newss;

import android.content.Intent;
import android.nfc.tech.NfcB;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

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

public class ThreeActivity extends AppCompatActivity {

    private ViewPager pager;
    private List<View> list;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==123){
                int index=pager.getCurrentItem();
                if(index<list.size()-1){
                    index++;
                }else{
                    index=0;
                }
                handler.sendEmptyMessageDelayed(123,3000);
                pager.setCurrentItem(index);
            }
        }
    };
    private WebView wv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_three);
        pager = findViewById(R.id.pager);
        wv = findViewById(R.id.wv);
        Intent intent=getIntent();
        String urls = intent.getStringExtra("url");
        wv.loadUrl(urls);
        wv.setWebViewClient(new WebViewClient());
        list = new ArrayList<>();

        View v1=View.inflate(this,R.layout.item_image,null);
        ImageView img1=v1.findViewById(R.id.img);
        img1.setImageResource(R.mipmap.a1);
        list.add(v1);
        View v2=View.inflate(this, R.layout.item_image, null);
        ImageView img2=(ImageView) v2.findViewById(R.id.img);
        img2.setImageResource(R.mipmap.a2);
        list.add(v2);

        View v3=View.inflate(this, R.layout.item_image, null);
        ImageView img3=(ImageView) v3.findViewById(R.id.img);
        img3.setImageResource(R.mipmap.a3);
        list.add(v3);

        View v4=View.inflate(this, R.layout.item_image, null);
        ImageView img4=(ImageView) v4.findViewById(R.id.img);
        img4.setImageResource(R.mipmap.a4);
        list.add(v4);
        pager.setAdapter(new PagerAdapter() {
            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                View v=list.get(position);
                container.addView(v);
                return v;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                View v=list.get(position);
                container.removeView(v);
            }

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

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

    @Override
    protected void onResume() {
        super.onResume();
        handler.sendEmptyMessageDelayed(123,3000);
    }

    @Override
    protected void onPause() {
        super.onPause();
        handler.removeCallbacksAndMessages(null);
    }
}

封装

package com.example.newss;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

/**
 * Created by 。。。。 on 2018/9/14.
 */

public abstract class BaseActivity extends AppCompatActivity {

    protected Context context;
    protected Intent intent;
    protected Bundle bundle;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        previewAction();
        setContentView(getcontentview());
        context = this;
        intent = getIntent();
        if(intent!=null){
            bundle = intent.getExtras();
        }
        initview();
        initdata();
        setlisttener();
        setmoreaction();
    }

    private void setmoreaction() {
    }

    private void setlisttener() {
    }

    private void initdata() {
    }

    protected abstract void initview();

    protected abstract int getcontentview();

    private void previewAction() {
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值