tablayout

首先要导入依赖包

 compile 'com.jwenfeng.pulltorefresh:library:1.0.3'
compile 'com.android.support:design:23.4.0'
接下来就是布局页面了
第一个页面mianxml中

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

    tools:context="com.example.zhou.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: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="@drawable/jiazai"
            android:id="@+id/item_img1"/>
        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:src="@drawable/jiazai"
            android:layout_height="wrap_content"
            android:id="@+id/item_img2"/>
        <ImageView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:src="@drawable/jiazai"
            android:layout_height="wrap_content"
            android:id="@+id/item_img3"/>
    </LinearLayout>
</LinearLayout>
然后是头条框的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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>
最后是首页的页面设置

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
<android.support.design.widget.TabLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/tab"
    app:tabIndicatorColor="#f5e20d"
    app:tabIndicatorHeight="5dp"
    app:tabGravity="center"
    app:tabSelectedTextColor="#090df7"
    app:tabTextColor="#000000"
    app:tabMode="scrollable"
    android:fillViewport="false"
    ></android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/vp"
        ></android.support.v4.view.ViewPager>
</LinearLayout>
剩下的布局 打上三个就行了

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00f"
    >

</LinearLayout>
接下来进入到fragment 页面

public class Sy extends Fragment {
    private TabLayout tab;
    private ViewPager vp;

    private List<String> list;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View v  =View.inflate(getActivity(),R.layout.sy,null);
        tab= (TabLayout) v.findViewById(R.id.tab);
       vp = (ViewPager) v.findViewById(R.id.vp);

     list  = new ArrayList<String>();
        list.add("福利");
        list.add("Android");
        list.add("iOS");
        list.add("休息视频");
        list.add("拓展资源");
        list.add("前端");
        list.add("all");
        vp.setAdapter(new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
            @Override
            public CharSequence getPageTitle(int position) {
                return list.get(position);
            }

            @Override
            public Fragment getItem(int position) {
                MyFragment fragment_1 = new MyFragment();
                Bundle bundle=new Bundle();
                //判断赋值
                if(list.get(position).equals("福利")){
                    bundle.putString("name","福利");
                }else if(list.get(position).equals("Android")){
                    bundle.putString("name","Android");
                }else if(list.get(position).equals("iOS")){
                    bundle.putString("name","iOS");
                }else if(list.get(position).equals("休息视频")){
                    bundle.putString("name","休息视频");
                }else if(list.get(position).equals("拓展资源")){
                    bundle.putString("name","拓展资源");
                }else if(list.get(position).equals("前端")){
                    bundle.putString("name","前端");
                }else if(list.get(position).equals("all")) {
                    bundle.putString("name", "all");
                }
                fragment_1.setArguments(bundle);
                return fragment_1;
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });
        tab.setupWithViewPager(vp);
        return v;
    }
}
剩余的三个fragment只是起到联动的效果

public class Wd extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     View v  = View.inflate(getActivity(), R.layout.wd,null);
        return v;
    }
}
第三个

public class Wtt extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

      View v  =View.inflate(getActivity(), R.layout.wtt,null);
        return v;
    }
}
第四个

public class Xg extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       View v =View.inflate(getActivity(), R.layout.xg,null);
        return v;
    }
}
接下来就是大招的时间了

首先是 img页面
public class Img extends Application{
    @Override
    public void onCreate() {
        DisplayImageOptions options=new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.ic_launcher).displayer(new FadeInBitmapDisplayer(2000)).cacheOnDisc(true).cacheInMemory(true).build();
        ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this).defaultDisplayImageOptions(options).threadPoolSize(5).build();
        ImageLoader.getInstance().init(configuration);
        super.onCreate();
    }
}
项目清单要注册 name

接下来是bean包
public class JsonBean {
    /**
     * code : 200
     * msg : success
     * newslist : [{"ctime":"2017-04-19 09:00","title":"你的笑","description":"华声美女","picUrl":"http://image.hnol.net/c/2017-04/19/08/201704190811375941-2240800.jpg","url":"http://bbs.voc.com.cn/mm/meinv-7763046-0-1.html"},{"ctime":"2017-04-19 15:00","title":"靓丽[贴图]","description":"华声美女","picUrl":"http://image.hnol.net/c/2017-04/14/21/201704142139063961-4659347.jpg","url":"http://bbs.voc.com.cn/mm/meinv-7755942-0-1.html"},{"ctime":"2017-04-19 15:00","title":"曹安娜.朱唇轻启诉佳人本色[05P][贴图]","description":"华声美女","picUrl":"http://image.hnol.net/c/2017-04/14/15/201704141513576531-2497165.jpg","url":"http://bbs.voc.com.cn/mm/meinv-7755295-0-1.html"},{"ctime":"2017-04-19 15:00","title":"〖古典美女〗《硕人》[08P][贴图]","description":"华声美女","picUrl":"http://image.hnol.net/c/2017-04/18/15/201704181541229411-2497165.jpg","url":"http://bbs.voc.com.cn/mm/meinv-7761698-0-1.html"},{"ctime":"2017-04-19 15:00","title":"洋溢的青春","description":"华声美女","picUrl":"http://image.hnol.net/c/2017-04/12/21/201704122139049971-619336.jpg","url":"http://bbs.voc.com.cn/mm/meinv-7751589-0-1.html"},{"ctime":"2017-04-19 16:00","title":"[贴图]高挑气质女","description":"华声美女","picUrl":"http://image.hnol.net/c/2017-04/19/15/201704191501393991-4217076.jpg","url":"http://bbs.voc.com.cn/mm/meinv-7764026-0-1.html"},{"ctime":"2017-04-19 23:00","title":"写真","description":"华声美女","picUrl":"http://image.hnol.net/c/2017-04/19/22/201704192242151641-1876615.jpg","url":"http://bbs.voc.com.cn/mm/meinv-7765119-0-1.html"},{"ctime":"2017-04-19 23:00","title":"蛋糕","description":"华声美女","picUrl":"http://image.hnol.net/c/2017-04/19/22/20170419223505271-1876615.jpg","url":"http://bbs.voc.com.cn/mm/meinv-7765106-0-1.html"},{"ctime":"2017-04-20 00:00","title":"午後","description":"华声美女","picUrl":"http://image.hnol.net/c/2017-04/19/23/20170419230258911-1876615.jpg","url":"http://bbs.voc.com.cn/mm/meinv-7765147-0-1.html"},{"ctime":"2017-04-20 00:00","title":"小圆","description":"华声美女","picUrl":"http://image.hnol.net/c/2017-04/19/22/201704192259465311-1876615.jpg","url":"http://bbs.voc.com.cn/mm/meinv-7765144-0-1.html"}]
     */

    private int code;
    private String msg;
    private List<NewslistBean> newslist;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public List<NewslistBean> getNewslist() {
        return newslist;
    }

    public void setNewslist(List<NewslistBean> newslist) {
        this.newslist = newslist;
    }

    public static class NewslistBean {
        /**
         * ctime : 2017-04-19 09:00
         * title : 你的笑
         * description : 华声美女
         * picUrl : http://image.hnol.net/c/2017-04/19/08/201704190811375941-2240800.jpg
         * url : http://bbs.voc.com.cn/mm/meinv-7763046-0-1.html
         */

        private String ctime;
        private String title;
        private String description;
        private String picUrl;
        private String url;

        public String getCtime() {
            return ctime;
        }

        public void setCtime(String ctime) {
            this.ctime = ctime;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getPicUrl() {
            return picUrl;
        }

        public void setPicUrl(String picUrl) {
            this.picUrl = picUrl;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }
}
最最重要的配置页面myfragment设置

public class MyFragment extends Fragment{

    private ListView lv;

    private String mString;

    private String path;
    private int cont=10;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            JsonBean jsonBean= (JsonBean) msg.obj;
            list = jsonBean.getNewslist();
            myad = new Myad();
            lv.setAdapter(myad);
            super.handleMessage(msg);
        }
    };
    private Myad myad;
    private List<JsonBean.NewslistBean> list;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment,container,false);
        final PullToRefreshLayout pull= (PullToRefreshLayout) view.findViewById(R.id.pull);
        lv = (ListView) 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();
    }

    private 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();
                        JsonBean bean = gson.fromJson(json, JsonBean.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 = (ImageView) view.findViewById(R.id.item_img1);
                he.item_img2 = (ImageView) view.findViewById(R.id.item_img2);
                he.item_img3 = (ImageView) view.findViewById(R.id.item_img3);
                he.tv = (TextView) 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;
    }
}
剩下的mianactivity就是对这几个按钮进行点击设置

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);
            }
        });
    }
}
温馨提示 记得在运行前不要忘了设置网络请求,要不不会出现效果的





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值