今日头条(MyFragment,Myadapter,MenuInfo类)

//*************************************************/MenuInfo类

package com.example.my;

import java.util.List;

/**
 * Created by dell on 2017/8/16.
 */

public class MenuInfo {


    /**
     * status : 1
     * info : 获取内容成功
     * data : [{"news_id":"18538","news_title":"见证王者风采,玩转电竞周末 2017成都智博会精彩不要错过!","news_summary":"201777-9日,2017中国(成都)智慧产业国际博览会将在成都新世纪国际会展中心盛大开幕,该博览会除了展示智慧","pic_url":"http://f.expoon.com/sub/news/2017/07/03/879442_230x162_0.jpg"},{"news_id":"14740","news_title":"第十七届成都国际家具展览会\u2014\u201473日盛装呈现","news_summary":"201673日,国内三大家具展之一,第十七届成都国际家具工业展览会(以下简称\u201c成都家具展\u201d)将在世纪城新会展中心盛装","pic_url":"http://f.expoon.com/sub/news/2016/06/23/721512_230x162_0.jpg"},{"news_id":"14129","news_title":"全景看不一样的博鳌亚洲论坛","news_summary":"近日,举办的博鳌亚洲论坛2016年年会的分论坛通过全景相机拍摄。当日,主题为\u201c亚洲新未来:新活力与新愿景\u201d的博鳌亚洲论坛","pic_url":"http://f.expoon.com/sub/news/2016/03/24/417246_230x162_0.jpg"},{"news_id":"14083","news_title":"2016上海国际虚拟现实高峰论坛暨展览会","news_summary":"由中华人民共和国商务部、中华人民共和国科学技术部、中华人民共和国国家知识产权局所主办的2016上海国际虚拟现实高峰论坛暨","pic_url":"http://f.expoon.com/sub/news/2016/03/14/588974_230x162_0.jpg"},{"news_id":"14082","news_title":"2016全球首届VR&AR互动娱乐博览会","news_summary":"2016全球首届VR&AR互动娱乐博览会将在20161012\u201414日深圳会展中心盛大举办!","pic_url":"http://f.expoon.com/sub/news/2016/03/14/899861_230x162_0.jpg"},{"news_id":"14071","news_title":"幻影星空VR游戏跑步机 上海、广州展会引试玩热潮","news_summary":"广州展会上汇聚了中国顶尖的游艺设备品牌商,各种新奇好玩的游乐设备让人眼花缭乱,而在一个主打\u201c全民对战、玩出健康\u201d的幻影星","pic_url":"http://f.expoon.com/sub/news/2016/03/11/594102_230x162_0.png"},{"news_id":"14069","news_title":"HTC Vive开发者峰会深圳召开 共促VR生态发展","news_summary":"310日,HTC Vive开发者峰会于深圳威盛科技大厦隆重举行,峰会现场聚集来自全国近千名VR游戏开发者、创业者以及行","pic_url":"http://f.expoon.com/sub/news/2016/03/11/360348_230x162_0.jpg"},{"news_id":"13991","news_title":"2016中国VR开发者论坛召开,暴风魔镜推VR平台发展","news_summary":"近日,由\u201c国内虚拟现实引领者\u201d暴风魔镜、\u201c虚拟现实第一站\u201dVR界共同主办的\u201c2016中国VR开发者论坛\u201d在吉林动画学院召","pic_url":"http://f.expoon.com/sub/news/2016/03/04/973715_230x162_0.jpg"},{"news_id":"13906","news_title":"2016年虚拟现实大会5月将于北京举办","news_summary":"2016年被视为虚拟现实技术元年,新一轮热潮正在席卷全球。其产业发展迅猛,行业发展空间广阔。","pic_url":"http://f.expoon.com/sub/news/2016/02/22/627538_230x162_0.jpg"},{"news_id":"13878","news_title":"第十届中国(青岛)国际建筑节能和新型建材博览会","news_summary":"由中国建材联合会、中国贸促会建材分会、海展集团所主办的第十届中国(青岛)国际建筑节能和新型建材博览会将于201671","pic_url":"http://f.expoon.com/sub/news/2016/02/16/370742_230x162_0.jpg"},{"news_id":"13877","news_title":"第十二届中国(青岛)国际门窗幕墙及相关设备展览会","news_summary":"由中国建材联合会、中国贸促会建材分会、海展集团所主办的第十二届中国(青岛)国际门窗幕墙及相关设备展览会将于20167","pic_url":"http://f.expoon.com/sub/news/2016/02/16/599992_230x162_0.jpg"},{"news_id":"13876","news_title":"2016第七届(上海)国际高端食品饮料展览会","news_summary":"由中国饭店协会所主办的2016第七届(上海)国际高端食品饮料展览会将于2016825\u201427日在上海新国际博览中心盛大","pic_url":"http://f.expoon.com/sub/news/2016/02/16/502258_230x162_0.jpg"},{"news_id":"13875","news_title":"2016上海餐饮连锁加盟及数字化管理展览会","news_summary":"由中国饭店协会、亚洲好食材联盟所主办的2016上海餐饮连锁加盟及数字化管理展览会将于2016825\u201427日在上海新","pic_url":"http://f.expoon.com/sub/news/2016/02/16/719167_230x162_0.jpg"},{"news_id":"13872","news_title":"2016国际(杭州)文物交流博览会","news_summary":"为弘扬中国传统文化和灿烂文明,推动文化艺术的繁荣与发展,同时为文玩经营者和收藏爱好者之间搭建一个交流交易的平台,经有关部","pic_url":"http://f.expoon.com/sub/news/2016/02/03/431004_230x162_0.jpg"},{"news_id":"13871","news_title":"2016中国国际纺织机械展暨ITMA亚洲展览会展","news_summary":"据中国纺织机械协会副会长顾平介绍,中国国际纺织机械展览会与ITMA亚洲展联合以后,主办方引进了先进的国际办展理念,并与中","pic_url":"http://f.expoon.com/sub/news/2016/02/03/219921_230x162_0.jpg"},{"news_id":"13864","news_title":"2016湖南车展5月在湖南国际会展中心举行","news_summary":"中部第一车展\u201d\u2014\u20142016湖南车展全新起航。记者从车展组委会获悉,以\u201c乐享生活\u201d为主题的2016湖南车展,55日至5","pic_url":"http://f.expoon.com/sub/news/2016/02/01/498241_230x162_0.jpg"},{"news_id":"13863","news_title":"第二届大数据产业博览会五月贵阳召开","news_summary":"\u201c2016中国贵阳电子商务大会暨贵阳国际大数据产业博览会\u201d新闻发布会上获悉,第二届数博会将于2016526日至29","pic_url":"http://f.expoon.com/sub/news/2016/02/01/155596_230x162_0.jpg"},{"news_id":"13840","news_title":"IG,CHINA 2016 全球气体行业第一展首站移师上海","news_summary":"成功举办十七载,继往开来谱新篇。全球气体行业最具影响力的第一盛会\u2014\u2014中国国际气体技术、设备与应用展览会(IG, Chin","pic_url":"http://f.expoon.com/sub/news/2016/01/27/556463_230x162_0.jpg"},{"news_id":"13826","news_title":"婴儿纸尿裤市场将迎来加速跑","news_summary":"近年来,纸尿裤正经历着快速发展阶段。据预测,全球婴儿纸尿裤的市场规模将在2020年达到594亿美元。新兴市场,特别是亚洲","pic_url":"http://f.expoon.com/sub/news/2016/01/26/428779_230x162_0.jpg"},{"news_id":"13818","news_title":"内蒙古非遗年货展暨传统文化月月传在内蒙古展览馆隆重开幕","news_summary":"2016122日,由自治区文化厅主办,内蒙古展览馆和各盟市文化局承办的\u201c非遗走进现代生活\u2014\u2014全区非物质文化遗产年货展","pic_url":"http://f.expoon.com/sub/news/2016/01/25/511616_230x162_0.jpg"}]
     */

    private int status;
    private String info;
    private List<DataBean> data;

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * news_id : 18538
         * news_title : 见证王者风采,玩转电竞周末 2017成都智博会精彩不要错过!
         * news_summary : 201777-9日,2017中国(成都)智慧产业国际博览会将在成都新世纪国际会展中心盛大开幕,该博览会除了展示智慧
         * pic_url : http://f.expoon.com/sub/news/2017/07/03/879442_230x162_0.jpg
         */

        private String news_id;
        private String news_title;
        private String news_summary;
        private String pic_url;

        public String getNews_id() {
            return news_id;
        }

        public void setNews_id(String news_id) {
            this.news_id = news_id;
        }

        public String getNews_title() {
            return news_title;
        }

        public void setNews_title(String news_title) {
            this.news_title = news_title;
        }

        public String getNews_summary() {
            return news_summary;
        }

        public void setNews_summary(String news_summary) {
            this.news_summary = news_summary;
        }

        public String getPic_url() {
            return pic_url;
        }

        public void setPic_url(String pic_url) {
            this.pic_url = pic_url;
        }
    }
}
 
//************************************//Myadapter类
package com.example.my;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;

import java.util.ArrayList;

/**
 * Created by dell on 2017/8/15.
 */

public class Myadapter extends FragmentStatePagerAdapter {

    private String[] titles = {"推荐", "热点", "北京", "视屏", "社会", "娱乐", "头条", "新闻", "体育", "腾讯", "爱奇艺"};
    private ArrayList<Fragment> mFragment = new ArrayList<Fragment>();
    private FragmentManager mFragmentManager;

    public Myadapter(FragmentManager fm) {
        super(fm);
        mFragmentManager = fm;
    }

    public Myadapter(FragmentManager fm, ArrayList<Fragment> tempFragment) {
        super(fm);
        mFragmentManager = fm;
        mFragment = tempFragment;
    }


    @Override
    public Fragment getItem(int position) {
        MyFragment myFragment = new MyFragment();
        return myFragment;
    }

//    @Override
//    public void destroyItem(ViewGroup container, int position, Object object) {
        super.destroyItem(container, position, object);
//    }

    @Override
    public int getCount() {
        return titles.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}

/**********************************************************//MyFragment类
package com.example.my;

import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
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.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.google.gson.Gson;
import com.limxing.xlistview.view.XListView;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import static android.R.attr.path;

/**
 * Created by dell on 2017/8/15.
 */

public class MyFragment extends Fragment implements XListView.IXListViewListener {

    private View view;
    private XListView xlistview;
    ImageLoader imageLoader;
    private int p = 1;
    private boolean flag;
    private Myadapter1 myadapter1;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.myfrgment, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        xlistview = (XListView) view.findViewById(R.id.xlistview);
        xlistview.setPullLoadEnable(true);
        xlistview.setXListViewListener(this);

//        getDates("http://api.expoon.com/AppNews/getNewsList/type/1", p + "");
        getDates("http://api.expoon.com/AppNews/getNewsList/type/2/", p + "");


    }

    public void getDates(String path, final String page) {
        new AsyncTask<String, Void, String>() {


            private List<MenuInfo.DataBean> data;

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                if (s != null) {
                    Gson gson = new Gson();
                    MenuInfo menuInfo = gson.fromJson(s, MenuInfo.class);
                    data = menuInfo.getData();

                    if (myadapter1 == null) {
                        myadapter1 = new Myadapter1(data);
                        xlistview.setAdapter(myadapter1);

                        xlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                            private PopupWindow pop;

                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                                final ImageView image = (ImageView) view.findViewById(R.id.image);
//                            TextView text = (TextView) view.findViewById(R.id.text);
                                View view2 = View.inflate(getActivity(), R.layout.pop, null);
                                pop = new PopupWindow(view2, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                                TextView delete = (TextView) view2.findViewById(R.id.delete);
                                ImageView dismiss = (ImageView) view2.findViewById(R.id.dismiss);
                                TextView shoucang = (TextView) view2.findViewById(R.id.shoucang);
                                TextView read = (TextView) view2.findViewById(R.id.read);

                                image.setOnClickListener(new View.OnClickListener() {

                                    @Override
                                    public void onClick(View v) {

                                        int[] a = new int[2];

                                        image.getLocationOnScreen(a);

                                        pop.setFocusable(true);
                                        pop.setTouchable(true);
                                        pop.setBackgroundDrawable(new BitmapDrawable());
                                        pop.setOutsideTouchable(true);
                                        pop.showAtLocation(image, 0, a[0], a[1]);

                                        // pop.showAsDropDown(imageView);

                                    }
                                });

                                delete.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        data.remove(position - 1);
                                        myadapter1.notifyDataSetChanged();
                                        pop.dismiss();
                                    }
                                });
                                dismiss.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        pop.dismiss();
                                    }
                                });


                            }
                        });


                    } else {
                        myadapter1.LodeMore(data, flag);
                        myadapter1.notifyDataSetChanged();
                    }


//                    adapter = new MyAdapter(this, arrayList);
//                    listview.setAdapter(adapter);


                }
            }

            @Override
            protected String doInBackground(String... strings) {

                try {
                    String path = strings[0];
                    URL url = new URL(path);
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.setRequestMethod("POST");
                    urlConnection.setConnectTimeout(200);
                    urlConnection.setReadTimeout(200);
                    OutputStream outputStream = urlConnection.getOutputStream();
                    outputStream.write(("p=" + page).getBytes());
                    outputStream.flush();

                    int code = urlConnection.getResponseCode();
                    if (code == 200) {
                        InputStream is = urlConnection.getInputStream();
                        String json = StreamTook.read(is);
                        return json;
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }

                return null;
            }
        }.execute(path, page);


    }

    @Override
    public void onRefresh() {
        ++p;
        getDates("http://api.expoon.com/AppNews/getNewsList/type/2/", p + "");
        flag = true;
        xlistview.stopRefresh(true);


    }

    @Override
    public void onLoadMore() {
        ++p;
        getDates("http://api.expoon.com/AppNews/getNewsList/type/2/", p + "");
        flag = false;
        xlistview.stopLoadMore();


    }

    class Myadapter1 extends BaseAdapter {
        List<MenuInfo.DataBean> data;
        private final DisplayImageOptions options;

        public Myadapter1(List<MenuInfo.DataBean> data) {
            this.data = data;
            imageLoader = ImageLoader.getInstance();
            File file = new File(Environment.getExternalStorageDirectory(), "/Bwei");
            if (!file.exists())
                file.mkdirs();

            ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(getActivity())
                    .diskCache(new UnlimitedDiskCache(file))
                    .build();

            imageLoader.init(configuration);

            options = new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.mipmap.ic_launcher)
                    .cacheOnDisk(true)
                    .build();
        }

        public void LodeMore(List<MenuInfo.DataBean> datas, boolean flag) {
            for (MenuInfo.DataBean bean : datas) {

                if (flag) {
                    data.add(0, bean);


                } else {
                    data.add(bean);
                }


            }


        }


        @Override
        public int getCount() {

            return data.size();
        }

        @Override
        public Object getItem(int position) {

            return data.get(position);
        }

        @Override
        public long getItemId(int position) {

            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                convertView = convertView.inflate(getActivity(), R.layout.item1, null);
                viewHolder = new ViewHolder();
                viewHolder.textitem1 = convertView.findViewById(R.id.textitem1);
                viewHolder.imageitem1 = convertView.findViewById(R.id.imageitem1);
                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.textitem1.setText(data.get(position).getNews_summary());
            getimage(data.get(position).getPic_url(), viewHolder.imageitem1);

            return convertView;
        }

        class ViewHolder {
            TextView textitem1;
            ImageView imageitem1;
        }


    }

    public void getimage(String path, ImageView imageView) {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .cacheOnDisk(true)
                .cacheInMemory(true)
                .build();
        ImageLoader.getInstance().displayImage(path, imageView, options);
    }


}

 
 
 
 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值