您的位置:首页 > 其它

Fragment复用Demo

2016-12-19 20:43 344 查看
import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.ArrayList;

import org.json.JSONArray;

import org.json.JSONObject;

import android.app.Activity;

import android.graphics.Color;

import android.os.Bundle;

import android.os.Handler;

import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentActivity;

import android.support.v4.view.ViewPager;

import android.support.v4.view.ViewPager.OnPageChangeListener;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.TextView;

public class MainActivity extends FragmentActivity implements OnClickListener {

    private ViewPager viewpager;

    private String type;

    private ArrayList<News> list1;

    private ArrayList<News> list2;

    private ArrayList<News> list3;

    private ArrayList<News> list4;

    private ArrayList<News> list5;

    private TextView vpimg;

    private TextView listmr;

    private TextView listxl;

    private TextView listxy;

    private TextView listjg;

    Handler handler = new Handler(){

        public void handleMessage(android.os.Message msg) {

            viewpager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(),listf));

        };

    };

    private ArrayList<Fragment> listf;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        // 初始化数据

        initView();

        //网络请求

        getDataFromNet();

        //添加数据

        initData();

        viewpager.setOnPageChangeListener(new OnPageChangeListener() {

            

            @Override

            public void onPageSelected(int arg0) {

                switch (arg0) {

                case 0:

                    vpimg.setBackgroundColor(Color.BLUE);

                    listmr.setBackgroundColor(Color.WHITE);

                    listxl.setBackgroundColor(Color.WHITE);

                    listxy.setBackgroundColor(Color.WHITE);

                    listjg.setBackgroundColor(Color.WHITE);

                    break;

                case 1:

                    listmr.setBackgroundColor(Color.BLUE);

                    vpimg.setBackgroundColor(Color.WHITE);

                    listxl.setBackgroundColor(Color.WHITE);

                    listxy.setBackgroundColor(Color.WHITE);

                    listjg.setBackgroundColor(Color.WHITE);

                    break;

                case 2:

                    listxl.setBackgroundColor(Color.BLUE);

                    listmr.setBackgroundColor(Color.WHITE);

                    vpimg.setBackgroundColor(Color.WHITE);

                    listxy.setBackgroundColor(Color.WHITE);

                    listjg.setBackgroundColor(Color.WHITE);

                    break;

                case 3:

                    listxy.setBackgroundColor(Color.BLUE);

                    listmr.setBackgroundColor(Color.WHITE);

                    listxl.setBackgroundColor(Color.WHITE);

                    vpimg.setBackgroundColor(Color.WHITE);

                    listjg.setBackgroundColor(Color.WHITE);

                    break;

                case 4:

                    listjg.setBackgroundColor(Color.BLUE);

                    listmr.setBackgroundColor(Color.WHITE);

                    listxl.setBackgroundColor(Color.WHITE);

                    listxy.setBackgroundColor(Color.WHITE);

                    vpimg.setBackgroundColor(Color.WHITE);

                    break;

                    

                default:

                    break;

                }

            }

            

            @Override

            public void onPageScrolled(int arg0, float arg1, int arg2) {

                // TODO Auto-generated method stub

                

            }

            

            @Override

            public void onPageScrollStateChanged(int arg0) {

                // TODO Auto-generated method stub

                

            }

        });

    }

    //添加数据

    private void initData() {

        listf = new ArrayList<Fragment>();

        listf.add(Fragment1.newInstance(list1));

        listf.add(Fragment1.newInstance(list2));

        listf.add(Fragment1.newInstance(list3));

        listf.add(Fragment1.newInstance(list4));

        listf.add(Fragment1.newInstance(list5));

        

    }

    //网络请求

    private void getDataFromNet() {

        list1 = new ArrayList<News>();

        list2 = new ArrayList<News>();

        list3 = new ArrayList<News>();

        list4 = new ArrayList<News>();

        list5 = new ArrayList<News>();

        

        new Thread(){

            public void run() {

                try {

                    URL url = new URL("http://172.17.29.120/localuser/ljy/shoptest/commodity.json");

                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

                    conn.setConnectTimeout(3000);

                    conn.setReadTimeout(2000);
a818

                    if (conn.getResponseCode()==HttpURLConnection.HTTP_OK) {

                        InputStream inputStream = conn.getInputStream();

                        String json = input2String(inputStream);

                        parseJson(json);

                        handler.sendEmptyMessage(0);

                        

                    }

                } catch (Exception e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

                }

            };

        }.start();

    }

     protected void parseJson(String json) {

     try {

     JSONArray jsonArray = new JSONArray(json);

     for (int i = 0; i < jsonArray.length(); i++) {

    

     JSONObject jsonObject = jsonArray.getJSONObject(i);

     type = jsonObject.getString("type");

     JSONArray item = jsonObject.getJSONArray("item");

     for (int j = 0; j < item.length(); j++) {

     News news = new News();

     JSONObject jsonObject2 = item.getJSONObject(j);

     news.img = jsonObject2.getString("img");

     news.info = jsonObject2.getString("info");

     news.title = jsonObject2.getString("title");

     if (type.equals("vpimg")) {

     list1.add(news);

     } else if (type.equals("listmr")) {

     list2.add(news);

     } else if (type.equals("listxl")) {

     list3.add(news);

     } else if (type.equals("listxy")) {

     list4.add(news);

     } else if (type.equals("listjg")) {

     list5.add(news);

     }

     }

    

     }

     } catch (Exception e) {

     // TODO Auto-generated catch block

     e.printStackTrace();

     }

     }

    protected String input2String(InputStream is) throws IOException {

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        int len = 0;

        byte[] buffer = new byte[1024];

        while ((len = is.read(buffer))!=-1) {

            outputStream.write(buffer, 0, len);

        }

        return outputStream.toString("gbk");

    }

    // 初始化数据

    private void initView() {

        vpimg = (TextView) findViewById(R.id.vpimg);

        listmr = (TextView) findViewById(R.id.listmr);

        listxl = (TextView) findViewById(R.id.listxl);

        listxy = (TextView) findViewById(R.id.listxy);

        listjg = (TextView) findViewById(R.id.listjg);

        viewpager = (ViewPager) findViewById(R.id.viewpager);

        vpimg.setOnClickListener(this);

        listmr.setOnClickListener(this);

        listxl.setOnClickListener(this);

        listxy.setOnClickListener(this);

        listjg.setOnClickListener(this);

        

        vpimg.setBackgroundColor(Color.BLUE);

    }

    @Override

    public void onClick(View v) {

        switch (v.getId()) {

        case R.id.vpimg:

            viewpager.setCurrentItem(0);

            break;

        case R.id.listmr:

            viewpager.setCurrentItem(1);

            break;

        case R.id.listxl:

            viewpager.setCurrentItem(2);

            break;

        case R.id.listxy:

            viewpager.setCurrentItem(3);

            break;

        case R.id.listjg:

            viewpager.setCurrentItem(4);

            break;

        default:

            break;

        }

    }

}

MyFragment类

import java.util.ArrayList;

import com.nostra13.universalimageloader.core.ImageLoader;

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.BaseAdapter;

import android.widget.ImageView;

import android.widget.ListView;

import android.widget.TextView;

public class Fragment1 extends Fragment{

    private ListView listview;

    private ArrayList<News> list1;

    @Override

    public View onCreateView(LayoutInflater inflater,

            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment1, null);

    }

    @Override

    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);

        listview = (ListView) getView().findViewById(R.id.listview);

        

        Bundle bundle = getArguments();

        list1 = (ArrayList<News>) bundle.getSerializable("list");

        listview.setAdapter(new BaseAdapter() {

            

            @Override

            public View getView(int position, View convertView, ViewGroup parent) {

                if (convertView ==null) {

                    convertView = View.inflate(getActivity(), R.layout.item, null);

                }

                ImageView img = (ImageView) convertView.findViewById(R.id.img);

                TextView title = (TextView) convertView.findViewById(R.id.title);

                TextView into = (TextView) convertView.findViewById(R.id.into);

                title.setText(list1.get(position).title);

                into.setText(list1.get(position).info);

                ImageLoader imageLoader = ImageLoader.getInstance();

                imageLoader.displayImage(list1.get(position).img, img);

                return convertView;

            }

            

            @Override

            public long getItemId(int position) {

                // TODO Auto-generated method stub

                return 0;

            }

            

            @Override

            public Object getItem(int position) {

                // TODO Auto-generated method stub

                return null;

            }

            

            @Override

            public int getCount() {

                // TODO Auto-generated method stub

                return list1.size();

            }

        });

        

    }

    

    public static Fragment newInstance(ArrayList<News> list){

        Fragment1 fragment1 = null;

        if (fragment1==null) {

            fragment1 = new Fragment1();

        }

        Bundle bundle = new Bundle();

        bundle.putSerializable("list", list);

        fragment1.setArguments(bundle);

        return fragment1;

        

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: