练习

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_37457363/article/details/78066455

布局


content布局


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.app.enzo.xingweijin1509c20170922.com.app.bawie.xingweijin1509c20170922.MainActivity"
    tools:showIn="@layout/app_bar_main">


    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rg"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        >
        <RadioButton
            android:layout_width="0dp"
            android:gravity="center"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="15sp"
            android:text="资讯"
            android:id="@+id/rb1"
            android:button="@null"
            />
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1"
            android:textSize="15sp"
            android:text="行情"
            android:id="@+id/rb2"
            android:button="@null"
            />
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1"
            android:textSize="15sp"
            android:text="自选"
            android:id="@+id/rb3"
            android:button="@null"
            />
        <RadioButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1"
            android:textSize="15sp"
            android:text="首页"
            android:id="@+id/rb4"
            android:button="@null"
            />
    </RadioGroup>


    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/vp1"
        android:layout_above="@id/rg"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true">
    </android.support.v4.view.ViewPager>


</RelativeLayout>





fragment   布局



<?xml version="1.0" encoding="utf-8"?>
<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/lv">
        </ListView>


    </com.jwenfeng.library.pulltorefresh.PullToRefreshLayout>


</LinearLayout>



item  布局



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">


    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"
        android:id="@+id/img"/>


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/title"
        android:text="标题"
        android:textSize="20sp"
        android:layout_toRightOf="@id/img"
        />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/time"
        android:text="时间"
        android:layout_alignParentRight="true"
        android:layout_alignBottom="@id/img"
        />
</RelativeLayout>



fragment1111   布局



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:text="第一个页面"
        android:layout_centerInParent="true"
        />
</RelativeLayout>



fragment2222   布局



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:text="第二个页面"
        android:layout_centerInParent="true"
        />
</RelativeLayout>


fragment3333布局



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:text="第三个页面"
        android:layout_centerInParent="true"
        />
</RelativeLayout>




fragment4 444布局


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="scrollable"
        android:id="@+id/tab">
    </android.support.design.widget.TabLayout>


        <android.support.v4.view.ViewPager
            android:layout_below="@id/tab"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/vp2"
            >
        </android.support.v4.view.ViewPager>


</RelativeLayout>



Androidmanifest    设置



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.enzo.xingweijin1509c20170922">


    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:name="com.app.bawie.xingweijin1509c20170922.MyApplic"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.app.bawie.xingweijin1509c20170922.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>



MainActivity   代码




package com.app.bawie.xingweijin1509c20170922;


import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;


import com.app.bawie.Fragment.Fragment3;
import com.app.bawie.Fragment.Fragment4;
import com.app.bawie.Fragment.Fragment1;
import com.app.bawie.Fragment.Fragment2;
import com.app.enzo.xingweijin1509c20170922.R;


import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    //初始化控件
    ViewPager vp1;
    List<Fragment> list = new ArrayList<Fragment>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        vp1 = (ViewPager) findViewById(R.id.vp1);
        RadioGroup rg = (RadioGroup) findViewById(R.id.rg);


        //设置viewpager
        list.add(new Fragment1());
        list.add(new Fragment2());
        list.add(new Fragment3());
        list.add(new Fragment4());
        vp1.setAdapter(new Adapter(getSupportFragmentManager()));


        //设置radiogroup的监听事件
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i)
                {
                    case R.id.rb1:vp1.setCurrentItem(0);break;
                    case R.id.rb2:vp1.setCurrentItem(1);break;
                    case R.id.rb3:vp1.setCurrentItem(2);break;
                    case R.id.rb4:vp1.setCurrentItem(3);break;
                }
            }
        });


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();


        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();


        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }


        return super.onOptionsItemSelected(item);
    }


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();


        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {


        } else if (id == R.id.nav_slideshow) {


        } else if (id == R.id.nav_manage) {


        }


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
    class Adapter extends FragmentPagerAdapter{


        public Adapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }


        @Override
        public int getCount() {
            return list.size();
        }
    }
}


MyAdapter  代码




package com.app.bawie.xingweijin1509c20170922;


import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioGroup;


import com.app.bawie.Fragment.Fragment3;
import com.app.bawie.Fragment.Fragment4;
import com.app.bawie.Fragment.Fragment1;
import com.app.bawie.Fragment.Fragment2;
import com.app.enzo.xingweijin1509c20170922.R;


import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    //初始化控件
    ViewPager vp1;
    List<Fragment> list = new ArrayList<Fragment>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        vp1 = (ViewPager) findViewById(R.id.vp1);
        RadioGroup rg = (RadioGroup) findViewById(R.id.rg);


        //设置viewpager
        list.add(new Fragment1());
        list.add(new Fragment2());
        list.add(new Fragment3());
        list.add(new Fragment4());
        vp1.setAdapter(new Adapter(getSupportFragmentManager()));


        //设置radiogroup的监听事件
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i)
                {
                    case R.id.rb1:vp1.setCurrentItem(0);break;
                    case R.id.rb2:vp1.setCurrentItem(1);break;
                    case R.id.rb3:vp1.setCurrentItem(2);break;
                    case R.id.rb4:vp1.setCurrentItem(3);break;
                }
            }
        });


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();


        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();


        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }


        return super.onOptionsItemSelected(item);
    }


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();


        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {


        } else if (id == R.id.nav_slideshow) {


        } else if (id == R.id.nav_manage) {


        }


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
    class Adapter extends FragmentPagerAdapter{


        public Adapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }


        @Override
        public int getCount() {
            return list.size();
        }
    }
}


Applic  代码




package com.app.bawie.xingweijin1509c20170922;


import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Environment;


import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;


import java.io.File;


import static android.R.attr.path;




/**
 * autour: 樊彦龙
 * date: 2017/9/6 10:40
 * update: 2017/9/6
 */


public class MyApplic extends Application{


    @Override
    public void onCreate() {
        super.onCreate();
        //初始化IamgeLoader
        //获取sd卡根目录路径
        File files = new File("/sdcard/Rimg");
        initImageLoader(getApplicationContext(),files);


    }
    public static void initImageLoader(Context context,File file) {
        // This configuration tuning is custom. You can tune every option, you may tune some of them,
        // or you can create default configuration by
        //  ImageLoaderConfiguration.createDefault(this);
        // method.
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        config.writeDebugLogs(); // Remove for release app
        config .diskCache(new UnlimitedDiskCache(file));//UnlimitedDiskCache 限制这个图片的缓存路径
        config .diskCacheFileCount(50);//配置sdcard缓存文件的数量
        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config.build());
    }














    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }


    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }
}


Bean   代码



package com.app.bawie.Bean;


/**
 * Created by ENZO on 2017/9/22.
 */


public class bean {


    private String img;
    private String title;
    private String otime;


    public bean(String img, String title, String otime) {
        this.img = img;
        this.title = title;
        this.otime = otime;
    }


    public String getImg() {
        return img;
    }


    public void setImg(String img) {
        this.img = img;
    }


    public String getTitle() {
        return title;
    }


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


    public String getOtime() {
        return otime;
    }


    public void setOtime(String otime) {
        this.otime = otime;
    }

}


Fragment1.java   代码




package com.app.bawie.Fragment;


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 com.app.enzo.xingweijin1509c20170922.R;


/**
 * Created by ENZO on 2017/9/22.
 */


public class Fragment1 extends Fragment{


    //返回视图
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.menu_fragment1,null);
    }
}


Fragment2222.java代码



package com.app.bawie.Fragment;


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 com.app.enzo.xingweijin1509c20170922.R;


/**
 * Created by ENZO on 2017/9/22.
 */


public class Fragment2 extends Fragment{
    //返回视图
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.menu_fragment2,null);
    }
}




fragment333.java   代码



package com.app.bawie.Fragment;


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 com.app.enzo.xingweijin1509c20170922.R;


/**
 * Created by ENZO on 2017/9/22.
 */


public class Fragment3 extends Fragment{
    //返回视图
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.menu_fragment3,null);
    }
}



fragment44444.java代码



package com.app.bawie.Fragment;


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.app.enzo.xingweijin1509c20170922.R;


import java.util.ArrayList;
import java.util.List;


/**
 * Created by ENZO on 2017/9/22.
 */


public class Fragment4 extends Fragment{


    //初始化控件
    TabLayout tab;
    ViewPager vp2;
    List<String> list = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //返回视图
        View v = View.inflate(getActivity(),R.layout.menu_fragment4,null);
        tab = v.findViewById(R.id.tab);
        vp2 = v.findViewById(R.id.vp2);
        return v;
    }


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


        //判断网络是否可用
        boolean networkAvailable = isNetworkAvailable(getActivity());
        if(!networkAvailable)
        {
            Toast.makeText(getActivity(),"无网络,请设置",Toast.LENGTH_SHORT).show();
        }
        else
        {


            //为标题栏添加文字
            list.add("数据新闻");
            list.add("快讯");
            list.add("头条");
            list.add("精编公告");
            list.add("美股");
            list.add("港股");
            list.add("基金");
            list.add("理财");




            //设置适配器
            vp2.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
                @Override
                public CharSequence getPageTitle(int position) {
                    return list.get(position);
                }


                @Override
                public Fragment getItem(int position) {
                    NewsFragment newsFragment = new NewsFragment();
                    Bundle bundle = new Bundle();
                    if (list.get(position).equals("数据新闻"))
                    {
                        bundle.putString("type","xbsjxw");
                    }else if (list.get(position).equals("快讯"))
                    {
                        bundle.putString("type","txs");
                    }else if (list.get(position).equals("头条"))
                    {
                        bundle.putString("type","toutiao");
                    }else if (list.get(position).equals("精编公告"))
                    {
                        bundle.putString("type","news/mobile/jbgg");
                    }else if (list.get(position).equals("美股"))
                    {
                        bundle.putString("type","news/mobile/mgxw");
                    }else if (list.get(position).equals("港股"))
                    {
                        bundle.putString("type","news/mobile/ggxw");
                    }else if (list.get(position).equals("基金"))
                    {
                        bundle.putString("type","news/mobile/jjxw");
                    }else if (list.get(position).equals("理财"))
                    {
                        bundle.putString("type","news/mobile/lcxw");
                    }
                    newsFragment.setArguments(bundle);
                    return newsFragment;
                }


                @Override
                public int getCount() {
                    return list.size();
                }
            });


            //联动效果
            tab.setupWithViewPager(vp2);
        }


    }




    //判断网络是否可用
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected())
            {
                // 当前网络是连接的
                if (info.getState() == NetworkInfo.State.CONNECTED)
                {
                    // 当前所连接的网络可用
                    return true;
                }
            }
        }
        return false;
    }
}




NewsFragment.java  代码






package com.app.bawie.Fragment;


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.app.enzo.xingweijin1509c20170922.R;


import java.util.ArrayList;
import java.util.List;


/**
 * Created by ENZO on 2017/9/22.
 */


public class Fragment4 extends Fragment{


    //初始化控件
    TabLayout tab;
    ViewPager vp2;
    List<String> list = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //返回视图
        View v = View.inflate(getActivity(),R.layout.menu_fragment4,null);
        tab = v.findViewById(R.id.tab);
        vp2 = v.findViewById(R.id.vp2);
        return v;
    }


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


        //判断网络是否可用
        boolean networkAvailable = isNetworkAvailable(getActivity());
        if(!networkAvailable)
        {
            Toast.makeText(getActivity(),"无网络,请设置",Toast.LENGTH_SHORT).show();
        }
        else
        {


            //为标题栏添加文字
            list.add("数据新闻");
            list.add("快讯");
            list.add("头条");
            list.add("精编公告");
            list.add("美股");
            list.add("港股");
            list.add("基金");
            list.add("理财");




            //设置适配器
            vp2.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
                @Override
                public CharSequence getPageTitle(int position) {
                    return list.get(position);
                }


                @Override
                public Fragment getItem(int position) {
                    NewsFragment newsFragment = new NewsFragment();
                    Bundle bundle = new Bundle();
                    if (list.get(position).equals("数据新闻"))
                    {
                        bundle.putString("type","xbsjxw");
                    }else if (list.get(position).equals("快讯"))
                    {
                        bundle.putString("type","txs");
                    }else if (list.get(position).equals("头条"))
                    {
                        bundle.putString("type","toutiao");
                    }else if (list.get(position).equals("精编公告"))
                    {
                        bundle.putString("type","news/mobile/jbgg");
                    }else if (list.get(position).equals("美股"))
                    {
                        bundle.putString("type","news/mobile/mgxw");
                    }else if (list.get(position).equals("港股"))
                    {
                        bundle.putString("type","news/mobile/ggxw");
                    }else if (list.get(position).equals("基金"))
                    {
                        bundle.putString("type","news/mobile/jjxw");
                    }else if (list.get(position).equals("理财"))
                    {
                        bundle.putString("type","news/mobile/lcxw");
                    }
                    newsFragment.setArguments(bundle);
                    return newsFragment;
                }


                @Override
                public int getCount() {
                    return list.size();
                }
            });


            //联动效果
            tab.setupWithViewPager(vp2);
        }


    }




    //判断网络是否可用
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected())
            {
                // 当前网络是连接的
                if (info.getState() == NetworkInfo.State.CONNECTED)
                {
                    // 当前所连接的网络可用
                    return true;
                }
            }
        }
        return false;
    }
}

展开阅读全文

没有更多推荐了,返回首页