今日头条的Fragment类

public class MyFragment extends Fragment implements XListView.IXListViewListener {
    private XListView xlv;
    private int index = 1;
    private boolean isLast;
    public Myapter2 apter;
    public Dao dao;


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

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

        xlv = (XListView) getView().findViewById(R.id.xlv);
        xlv.setPullRefreshEnable(true);
        xlv.setPullLoadEnable(true);
        xlv.setXListViewListener(this);
        setData("http://api.expoon.com/AppNews/getNewsList/type/1/p/1", index + "", 10 + "");
       xlv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
           @Override
           public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
               startActivity(new Intent(getActivity(),Main2Activity.class));

               return false;
           }
       });

    }










    public void setData(String path,String page,String limit){
        new AsyncTask(){
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                if(s!=null){

                    Gson gson=new Gson();
                    Qbean qbean = gson.fromJson(s, Qbean.class);


                    List list = qbean.getData();
                    for(Qbean.DataBean kkk:list){
                        String news_title = kkk.getNews_title();
                        String pic_url = kkk.getPic_url();
                        dao=new Dao(getActivity());
                        dao.insert(news_title,pic_url);

                    }

                    if(apter==null){
                     apter=new Myapter2(list);
                    xlv.setAdapter(apter);
                    xlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView parent, View view, int position, long id) {

                            final ImageView i2 = (ImageView) view
                                    .findViewById(R.id.i2);
                            View view2 = View
                                    .inflate(getActivity(), R.layout.pop, null);

                            final PopupWindow pop = new PopupWindow(view2, ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT
                            );
                            TextView textView = (TextView) view2.findViewById(R.id.read);
                            TextView textView2 = (TextView) view2.findViewById(R.id.shoucang);
                            TextView textView3 = (TextView) view2.findViewById(R.id.delete);

                            ImageView imageView2 = (ImageView) view2
                                    .findViewById(R.id.dismiss);
                            i2.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {

                                    int[] a = new int[2];

                                    i2.getLocationOnScreen(a);

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

                                    // pop.showAsDropDown(imageView);

                                }
                            });

                            imageView2.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    pop.dismiss();
                                }
                            });


                        }
                    });






                    }
                    else{
              apter.Loader(list,isLast);
                        apter.notifyDataSetChanged();

                    }
                }

            }

            @Override
            protected String doInBackground(String... params) {
                String path = params[0];
                try {
                    URL url=new URL(path);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(5000);

                    int code = connection.getResponseCode();
                    if(code==200){
                        InputStream is =  connection.getInputStream();
                        String json = StreamTools.readjson(is);
                        return json;
                    }


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


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





    }

    @Override
    public void onRefresh() {
   ++index;
        setData("http://api.expoon.com/AppNews/getNewsList/type/1/p/1",index+"",10+"");
        isLast=true;
        xlv.stopRefresh(true);
    }

    @Override
    public void onLoadMore() {
        ++index;
        setData("http://api.expoon.com/AppNews/getNewsList/type/1/p/1",index+"",10+"");
        isLast=false;
        xlv.stopLoadMore();

    }


    class Myapter2 extends BaseAdapter{
        public List list;
        private DisplayImageOptions options;
        public Myapter2(List list) {
            this.list = list;
            options=new DisplayImageOptions.Builder()
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .build();

        }


        public void Loader( List lists,boolean isLast){
            for (Qbean.DataBean bean:lists) {
                if(isLast){
                    list.add(bean);
                }
                else{
                    list.add(0,bean);
                }
            }

        }

        @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) {
            if(convertView==null){
                convertView=View.inflate(getActivity(),R.layout.item2,null);

            }
            TextView t1= (TextView) convertView.findViewById(R.id.t1);
            ImageView img= (ImageView) convertView.findViewById(R.id.img);

            t1.setText(list.get(position).getNews_title());
//            img.setImageResource(R.mipmap.ic_launcher);
           ImageLoader.getInstance().displayImage(list.get(position).getPic_url(),img,options);
            return convertView;
        }
    }










你可能感兴趣的:(今日头条的Fragment类)