今日头条

来源:互联网 发布:通达oa开放那些端口 编辑:程序博客网 时间:2024/04/29 02:36

MainActivity

import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

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

import fragments.Sy;
import fragments.Wd;
import fragments.Wtt;
import fragments.Xg;

public class MainActivity extends AppCompatActivity {

    private TextView shouye;
    private TextView xigua;
    private TextView wtt;
    private TextView wode;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        shouye = (TextView) findViewById(R.id.shouye);
        xigua = (TextView) findViewById(R.id.xigua);
        wtt = (TextView) findViewById(R.id.wtt);
        wode = (TextView) findViewById(R.id.wode);
//展示第一个视图
        FragmentManager manager=getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.fl,new Sy());
        transaction.commit();
        shouye.setTextColor(Color.RED);
        xigua.setTextColor(Color.GRAY);
        wtt.setTextColor(Color.GRAY);
        wode.setTextColor(Color.GRAY);
        //点击事件
        shouye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager manager=getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.fl,new Sy());
                transaction.commit();
                shouye.setTextColor(Color.RED);
                xigua.setTextColor(Color.GRAY);
                wtt.setTextColor(Color.GRAY);
                wode.setTextColor(Color.GRAY);
            }
        });
        xigua.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager manager=getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.fl,new Xg());
                transaction.commit();
                shouye.setTextColor(Color.GRAY);
                xigua.setTextColor(Color.RED);
                wtt.setTextColor(Color.GRAY);
                wode.setTextColor(Color.GRAY);
            }
        });
        wtt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager manager=getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.fl,new Wtt());
                transaction.commit();
                shouye.setTextColor(Color.GRAY);
                xigua.setTextColor(Color.GRAY);
                wtt.setTextColor(Color.RED);
                wode.setTextColor(Color.GRAY);
            }
        });
        wode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager manager=getSupportFragmentManager();
                FragmentTransaction transaction = manager.beginTransaction();
                transaction.replace(R.id.fl,new Wd());
                transaction.commit();
                shouye.setTextColor(Color.GRAY);
                xigua.setTextColor(Color.GRAY);
                wtt.setTextColor(Color.GRAY);
                wode.setTextColor(Color.RED);
            }
        });
    }
}
MyFragment

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.List;

/**
 * Created by Administrator on 2017/9/18.
 */

public class MyFragment extends Fragment{


    private ListView lv;
    private List<Newslist> list;
    private String mString;
    private JsonRootBean bean;
    private String path;
    private int cont=10;
    private Handler handler = new Handler(){



        @Override
        public void handleMessage(Message msg) {
            bean = (JsonRootBean) msg.obj;
            list = bean.getNewslist();

            myad = new Myad();
            lv.setAdapter(myad);
        }
    };
    private Myad myad;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.news_layout,container,false);
        final PullToRefreshLayout pull=view.findViewById(R.id.pull);
        lv = view.findViewById(R.id.pull_lv);

        pull.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        cont++;
                        path = "http://api.tianapi.com/meinv/?key=6dd96d7344c784e7d5a6f6dfd118cde3&num=" + cont;
                        getData();
                        myad.notifyDataSetChanged();
                        pull.finishRefresh();
                    }
                },2000);
            }

            @Override
            public void loadMore() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        cont++;
                        path = "http://api.tianapi.com/meinv/?key=6dd96d7344c784e7d5a6f6dfd118cde3&num=" + cont;
                        getData();
                        myad.notifyDataSetChanged();
                        pull.finishLoadMore();
                    }
                },2000);
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle =getArguments();
        mString = bundle.getString("name","福利");
        getData();
    }

    public void getData() {
        new Thread(){


            @Override
            public void run() {
                   if (path==null) {
                       path = "http://api.tianapi.com/meinv/?key=6dd96d7344c784e7d5a6f6dfd118cde3&num=" + cont;
                   }
                try {
                    URL url = new URL(path);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(5000);
                    if (connection.getResponseCode() == 200) {
                        InputStream stream = connection.getInputStream();
                        String json = zhuan(stream, "utf-8");
                        Log.i("TAG",json);
                        Gson gson = new Gson();
                        bean = gson.fromJson(json, JsonRootBean.class);
                        Message message = Message.obtain();
                        message.what = 0;
                        message.obj = bean;
                        handler.sendMessage(message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    }.start();
}

    private String zhuan(InputStream stream, String s) {
        try {
            InputStreamReader in=new InputStreamReader(stream,s);
            BufferedReader re=new BufferedReader(in);
            String ss=null;
            StringBuilder builder=new StringBuilder();
            while((ss=re.readLine())!=null){
                builder.append(ss);
            }
            return builder.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    class Myad extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

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

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            He he = null;
            if (view == null) {
                view = View.inflate(getActivity(), R.layout.item, null);
                he = new He();
                he.item_img1 = view.findViewById(R.id.item_img1);
                he.item_img2 = view.findViewById(R.id.item_img2);
                he.item_img3 = view.findViewById(R.id.item_img3);
                he.tv = view.findViewById(R.id.tv);
                view.setTag(he);
            } else {
                he = (He) view.getTag();
            }
            he.tv.setText(list.get(i).getTitle());
            ImageLoader.getInstance().displayImage(list.get(i).getPicUrl(),he.item_img1);
            ImageLoader.getInstance().displayImage(list.get(i).getPicUrl(),he.item_img2);
            return view;
        }

        class He {
            ImageView item_img1,item_img2,item_img3;
            TextView tv;
        }
    }
    }

javaBean :Newslist

img

activity_main

<?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" tools:context="exam3.myapplication.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ll"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_alignParentBottom="true"

        >
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="首页"
            android:id="@+id/shouye"
            android:textColor="#f00"
            android:gravity="center"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="西瓜视频"
            android:id="@+id/xigua"
            android:gravity="center"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="微头条"
            android:id="@+id/wtt"
            android:gravity="center"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="我的"
            android:id="@+id/wode"
            android:gravity="center"/>
    </LinearLayout>
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/ll"
        android:id="@+id/fl">

    </FrameLayout>

</RelativeLayout>

item

<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="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv"/>
    <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:src="@mipmap/ic_launcher"
            android:id="@+id/item_img1"/>
        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:src="@mipmap/ic_launcher"
            android:layout_height="wrap_content"
            android:id="@+id/item_img2"/>
        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:src="@mipmap/ic_launcher"
            android:layout_height="wrap_content"
            android:id="@+id/item_img3"/>
    </LinearLayout>

</LinearLayout>

news_layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.jwenfeng.library.pulltorefresh.PullToRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/pull">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/pull_lv"></ListView>
    </com.jwenfeng.library.pulltorefresh.PullToRefreshLayout>

</LinearLayout>