zoukankan      html  css  js  c++  java
  • 基于百度定位及天气获取的DEMO +fragment+sharedpreference

    此工程较BaiduLocationXML相比:
    1.植入fragment,结合微信UI
    2.在原本主界面的button  textview  textview 移植到Fragment1
    3.增加网络判断,网络不通的情况下做另外处理
    4.在网络通畅的情况下,将地址信息、天气信息存入xml(sharedpreferences),
        网络不通的情况下,将原先xml信息读出展现

    fragment植入

    public class Fragment1 extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater,
                @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment1,container, false);
            locBtn = (Button) rootView.findViewById(R.id.location);
            locBtn.setVisibility(View.INVISIBLE);//隐藏button
            locInfo = (TextView) rootView.findViewById(R.id.location_info);
            weatherInfo = (TextView) rootView.findViewById(R.id.weather_info);
            initializeListeners();
            initialize();//注册广播
            return rootView;
        }
         @Override  
            public void onDestroyView() {  
               getActivity().unregisterReceiver(broadcastReceiver);  //注销广播
               super.onDestroyView();  
            }
    public class MainActivity extends FragmentActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main);
            // 初始化滑动条1/3
            initTabLine();
            // 初始化界面
            initView();//载入fragment
        }
    
    private void initView() {
            // 实例化对象
            viewPager = (ViewPager) findViewById(R.id.viewpager);
            tv1 = (TextView)findViewById(R.id.tv1);
            tv2 = (TextView) findViewById(R.id.tv2);
            tv3 = (TextView) findViewById(R.id.tv3);
            list = new ArrayList<Fragment>();
    
            // 设置数据源
            Fragment1 fragment1 = new Fragment1();
            Fragment2 fragment2 = new Fragment2();
            Fragment3 fragment3 = new Fragment3();
    
            list.add(fragment1);
            list.add(fragment2);
            list.add(fragment3);
    
            // 设置适配器
            FragmentPagerAdapter adapter = new FragmentPagerAdapter(
                    getSupportFragmentManager()) {
    
                @Override
                public int getCount() {
                    return list.size();
                }
    
                @Override
                public Fragment getItem(int arg0) {
                    return list.get(arg0);
                }
            };
    
            // 绑定适配器
            viewPager.setAdapter(adapter);
    
            // 设置滑动监听
            viewPager.setOnPageChangeListener(new OnPageChangeListener() {
    
                @Override
                public void onPageSelected(int position) {
                    // 当页面被选择时,先讲3个textview的字体颜色初始化成黑
                    tv1.setTextColor(Color.BLACK);
                    tv2.setTextColor(Color.BLACK);
                    tv3.setTextColor(Color.BLACK);
    
                    // 再改变当前选择页(position)对应的textview颜色
                    switch (position) {
                    case 0:
                        tv1.setTextColor(Color.rgb(51, 153, 0));
                        break;
                    case 1:
                        tv2.setTextColor(Color.rgb(51, 153, 0));
                        break;
                    case 2:
                        tv3.setTextColor(Color.rgb(51, 153, 0));
                        break;
                    }
                    currentPage = position;
                }
    
                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {
                    Log.i("tuzi", arg0 + "," + arg1 + "," + arg2);
    
                    // 取得该控件的实例
                    LinearLayout.LayoutParams ll = (android.widget.LinearLayout.LayoutParams) tabline
                            .getLayoutParams();
    
                    if (currentPage == 0 && arg0 == 0) { // 0->1移动(第一页到第二页)
                        ll.leftMargin = (int) (currentPage * tabLineLength + arg1
                                * tabLineLength);
                    } else if (currentPage == 1 && arg0 == 1) { // 1->2移动(第二页到第三页)
                        ll.leftMargin = (int) (currentPage * tabLineLength + arg1
                                * tabLineLength);
                    } else if (currentPage == 1 && arg0 == 0) { // 1->0移动(第二页到第一页)
                        ll.leftMargin = (int) (currentPage * tabLineLength - ((1 - arg1) * tabLineLength));
                    } else if (currentPage == 2 && arg0 == 1) { // 2->1移动(第三页到第二页)
                        ll.leftMargin = (int) (currentPage * tabLineLength - (1 - arg1)
                                * tabLineLength);
                    }
    
                    tabline.setLayoutParams(ll);
    
                }
    
                @Override
                public void onPageScrollStateChanged(int arg0) {
                    // TODO Auto-generated method stub
    
                }
            });
    
        }

    SharedPreferences:

    //保存为xml部分
    SharedPreferences perference =context.getSharedPreferences("result",context.MODE_WORLD_READABLE);//xml名为result
    editor = perference.edit(); 
    editor.putString("地址",addressStr);  
            editor.putString("天气信息",result);  
            //以上的put内容是保存在内容中,完成后利用commit提交  
            editor.commit();
    //获取SharedPreferences  
                SharedPreferences perference = getActivity().getApplicationContext().getSharedPreferences("result",getActivity().getApplicationContext().MODE_PRIVATE);  
                //取得key为name的值,如果不存在,则返回空值  
                String addr = perference.getString("地址","");  
                //取得key为age的值,如果不存在,则返回0  
                String weather = perference.getString("天气信息","");

    网络连通状态判断

    //构造函数类型
    public class NetworkStatus {
        public boolean netStatus = false;
    
        public NetworkStatus(Context context) {
            try {
                ConnectivityManager connectManager = (ConnectivityManager) context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo activeNetworkInfo = connectManager
                        .getActiveNetworkInfo();
                if (activeNetworkInfo != null) {
                    if (activeNetworkInfo.isAvailable()
                            && activeNetworkInfo.isConnected()) {
                        netStatus = true;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    //boolean类型
    public class NetworkStatus {
        public boolean netStatus = false;
        public NetworkStatus(Context context) {
       ConnectivityManager con=(ConnectivityManager)context.getSystemService   (Activity.CONNECTIVITY_SERVICE);  
        boolean wifi=con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();  
        boolean internet=con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();  
        if(internet){  
            //执行相关操作  
            netStatus=true;
            Toast.makeText(context,  
                    "当前移动网络已连接!", Toast.LENGTH_LONG)  
                    .show();  
        }else if(wifi){ 
            netStatus=true;
            Toast.makeText(context,  
                    "当前WIFI已连接", Toast.LENGTH_LONG)  
                    .show();  
        } else
        {
            Toast.makeText(context,  
                     "亲,网络连了么?", Toast.LENGTH_LONG)  
                    .show(); 
        }
    }
    }

    源码:http://download.csdn.net/detail/xiejun1026/8444569

  • 相关阅读:
    python ORM的使用
    python写入mysql
    远程连接不上centos的mysql的解决方法
    linux上mysql的安装
    缓存模块redis
    topic模式下的收发
    direct模式下的收发
    广播模式下的生产者与消费者fanout模式
    [HNOI2008]玩具装箱TOY
    [NOI2009]二叉查找树
  • 原文地址:https://www.cnblogs.com/NeilLing/p/4290081.html
Copyright © 2011-2022 走看看