import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import news1510a.bawei.com.a14_zhoukao_test.fragment.FragmentHome;
import news1510a.bawei.com.a14_zhoukao_test.fragment.FragmentMarket;
import news1510a.bawei.com.a14_zhoukao_test.fragment.FragmentMore;
import news1510a.bawei.com.a14_zhoukao_test.fragment.FragmentNotify;
import news1510a.bawei.com.a14_zhoukao_test.fragment.FragmentXiangFa;
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.radio_group);
//默认显示
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,new FragmentHome()).commit();
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i){
case R.id.radio_01:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,new FragmentHome()).commit();
break;
case R.id.radio_02:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,new FragmentXiangFa()).commit();
break;
case R.id.radio_03:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,new FragmentMarket()).commit();
break;
case R.id.radio_04:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,new FragmentNotify()).commit();
break;
case R.id.radio_05:
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,new FragmentMore()).commit();
break;
default:
break;
}
}
});
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_above="@+id/radio_group"
android:id="@+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<RadioGroup
android:id="@+id/radio_group"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="40dp">
<RadioButton
android:id="@+id/radio_01"
android:padding="10dp"
android:button="@null"
android:gravity="center"
android:background="@drawable/radio_selector"
android:checked="true"
android:text="首页"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/radio_02"
android:padding="10dp"
android:button="@null"
android:gravity="center"
android:background="@drawable/radio_selector"
android:text="想法"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/radio_03"
android:padding="10dp"
android:button="@null"
android:gravity="center"
android:background="@drawable/radio_selector"
android:text="市场"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/radio_04"
android:padding="10dp"
android:button="@null"
android:gravity="center"
android:background="@drawable/radio_selector"
android:text="通知"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<RadioButton
android:id="@+id/radio_05"
android:padding="10dp"
android:button="@null"
android:gravity="center"
android:background="@drawable/radio_selector"
android:text="更多"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
</RadioGroup>
</RelativeLayout>
++++++++++++++++++++++++++++++++++++++++++++++++++++++
空白frment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#888888"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
+++++++++++++++++++++++++++++++++++++++++++++++++++++
首页frment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="羞羞的铁拳"
/>
<android.support.design.widget.TabLayout
android:id="@+id/tab"
app:tabGravity="center"
app:tabIndicatorColor="@color/colorAccent"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/colorPrimaryDark"
app:tabTextColor="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="40dp">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</LinearLayout>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
热门frment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<news1510a.bawei.com.a14_zhoukao_test.view.XListView
android:id="@+id/x_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</news1510a.bawei.com.a14_zhoukao_test.view.XListView>
</LinearLayout>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
子界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<news1510a.bawei.com.a14_zhoukao_test.view.XListView
android:id="@+id/x_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</news1510a.bawei.com.a14_zhoukao_test.view.XListView>
</LinearLayout>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
子界面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_title"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image_view"
android:layout_width="100dp"
android:layout_height="100dp" />
</LinearLayout>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
radio_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--根据选中和未选中的状态,去设置背景颜色的图片-->
<item android:state_checked="true" android:drawable="@color/colorAccent"/>
<item android:state_checked="false" android:drawable="@color/colorPrimary"/>
<item android:drawable="@color/colorPrimary"/>
</selector>
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
package news1510a.bawei.com.a14_zhoukao_test.adapter;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
import news1510a.bawei.com.a14_zhoukao_test.R;
import news1510a.bawei.com.a14_zhoukao_test.bean.DataDataBean;
import news1510a.bawei.com.a14_zhoukao_test.util.ImageLoaderUtil;
/**
* @author Dash
* @date 2017/10/16
* @description:
*/
public class MyAdapter extends BaseAdapter {
Context context;
List<DataDataBean.ResultsBean> list;
private int TEXT_ONLY = 0;
private int IMAGE_TEXT = 1;
public MyAdapter(Context context, List<DataDataBean.ResultsBean> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (list.get(position).getImages() != null){
return IMAGE_TEXT;
}
return TEXT_ONLY;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (getItemViewType(i) == TEXT_ONLY){
TextHolder holder;
if (view == null){
view = View.inflate(context, R.layout.item_text_layout,null);
holder = new TextHolder();
holder.textView = view.findViewById(R.id.text_title);
view.setTag(holder);
}else {
holder = (TextHolder) view.getTag();
}
holder.textView.setText(list.get(i).getDesc());
}else if (getItemViewType(i)==IMAGE_TEXT){
ImageHolder holder;
if (view == null){
view = View.inflate(context, R.layout.item_iamge_layout,null);
holder = new ImageHolder();
holder.textView = view.findViewById(R.id.text_title);
holder.imageView = view.findViewById(R.id.image_view);
view.setTag(holder);
}else {
holder = (ImageHolder) view.getTag();
}
holder.textView.setText(list.get(i).getDesc());
//图片
Log.i("----",list.get(i).getImages().get(0)+"?imageView2/0/w/100");
ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0)+"?imageView2/0/w/100",holder.imageView, ImageLoaderUtil.getRoundOption());
}
return view;
}
private class TextHolder{
TextView textView;
}
private class ImageHolder{
TextView textView;
ImageView imageView;
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package news1510a.bawei.com.a14_zhoukao_test.application;
import android.app.Application;
import news1510a.bawei.com.a14_zhoukao_test.util.ImageLoaderUtil;
/**
* @author Dash
* @date 2017/10/10
* @description:
*/
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//在应用创建的时候,,,对imageLoader进行全局配置
ImageLoaderUtil.init(this);//写一个工具类对imageLoader进行初始化...应用也可以作为上下文
}
}
+++++++++++++++++++++++++++
package news1510a.bawei.com.a14_zhoukao_test.inter;
/**
* @author Dash
* @date 2017/10/16
* @description:
*/
public interface JsonStringCallaBack {
public void getJsonString(String json);
}
+++++++++++++++++++++++++++++++++++++++++++++++++++
package news1510a.bawei.com.a14_zhoukao_test.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.CircleBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import java.io.File;
import news1510a.bawei.com.a14_zhoukao_test.R;
/**
* @author Dash
* @date 2017/10/10
* @description:
*/
public class ImageLoaderUtil {
/**
* 对imageLoader进行配置的方法
* @param context 上下文
*/
public static void init(Context context) {
//File cacheDir = StorageUtils.getCacheDirectory(context); //指定默认的硬盘(sd卡)的缓存文件夹路径
/**
* 可以自己创建一个sd下面的文件夹作为硬盘缓存的路径
*/
File file = new File(Environment.getExternalStorageDirectory(),"image");
if (! file.exists()){
file.mkdirs();
}
//1.创建一个ImageLoaderConfiguration,,,图片加载器的配置对象
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.threadPoolSize(3) // default 线程池内加载的数量
.threadPriority(Thread.NORM_PRIORITY - 2) // default 设置当前线程的优先级
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
//1 TB = 1024GB 1G=1024MB 1M = 1024KB 1KB = 1024BYTE
//LruMemoryCache,,lru最近最少使用算法...内部维护的是LinkedHashMap,,当一张图片最近很少使用的时候会从mao集合里面移除
.memoryCache(new LruMemoryCache(2 * 1024 * 1024)) //指定内存缓存的大小,,2M,,可以通过自己的内存缓存实现
.memoryCacheSize(2 * 1024 * 1024) // 内存缓存的最大值
.memoryCacheSizePercentage(13) // default
//指定硬盘/磁盘缓存的路径
.diskCache(new UnlimitedDiskCache(file)) // default 可以自定义缓存路径
.diskCacheSize(50 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值
.diskCacheFileCount(100) // 可以缓存的文件数量
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs() // 打印debug log
.build(); //开始构建
//2.对上面配置对象的初始化
ImageLoader.getInstance().init(config);
}
/**
* 默认的展示图片的选项
*
* @return
*/
public static DisplayImageOptions getDefaultOption() {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher) // 设置图片下载期间显示的图片
.showImageForEmptyUri(R.mipmap.ic_launcher) // 设置图片Uri为空或是错误的时候显示的图片
.showImageOnFail(R.mipmap.ic_launcher) // 设置图片加载或解码过程中发生错误显示的图片
.resetViewBeforeLoading(true) // default 设置图片在加载前是否重置、复位
.cacheInMemory(true) // default 设置下载的图片是否缓存在内存中
.cacheOnDisk(true) // default 设置下载的图片是否缓存在SD卡中
.considerExifParams(true) // default
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED) // default 设置图片以如何的编码方式显示
.bitmapConfig(Bitmap.Config.RGB_565) // default 设置图片的解码类型
.displayer(new SimpleBitmapDisplayer()) // default 还可以设置圆角图片new RoundedBitmapDisplayer(20)
.build();
return options;
}
/**
* 展示圆角的选项
* @return
*/
public static DisplayImageOptions getRoundOption() {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher) // 设置图片下载期间显示的图片
.showImageForEmptyUri(R.mipmap.ic_launcher) // 设置图片Uri为空或是错误的时候显示的图片
.showImageOnFail(R.mipmap.ic_launcher) // 设置图片加载或解码过程中发生错误显示的图片
.resetViewBeforeLoading(true) // default 设置图片在加载前是否重置、复位
.cacheInMemory(true) // default 设置下载的图片是否缓存在内存中
.cacheOnDisk(true) // default 设置下载的图片是否缓存在SD卡中
.considerExifParams(true) // default
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED) // default 设置图片以如何的编码方式显示
.bitmapConfig(Bitmap.Config.RGB_565) // default 设置图片的解码类型
.displayer(new RoundedBitmapDisplayer(20)) // default 还可以设置圆角图片new RoundedBitmapDisplayer(20)
.build();
return options;
}
/**
* 展示圆形的选项
* @return
*/
public static DisplayImageOptions getCircleOption() {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.ic_launcher) // 设置图片下载期间显示的图片
.showImageForEmptyUri(R.mipmap.ic_launcher) // 设置图片Uri为空或是错误的时候显示的图片
.showImageOnFail(R.mipmap.ic_launcher) // 设置图片加载或解码过程中发生错误显示的图片
.resetViewBeforeLoading(true) // default 设置图片在加载前是否重置、复位
.cacheInMemory(true) // default 设置下载的图片是否缓存在内存中
.cacheOnDisk(true) // default 设置下载的图片是否缓存在SD卡中
.considerExifParams(true) // default
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED) // default 设置图片以如何的编码方式显示
.bitmapConfig(Bitmap.Config.RGB_565) // default 设置图片的解码类型
.displayer(new CircleBitmapDisplayer()) // default 还可以设置圆角图片new RoundedBitmapDisplayer(20)
.build();
return options;
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package news1510a.bawei.com.a14_zhoukao_test.util;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import news1510a.bawei.com.a14_zhoukao_test.inter.JsonStringCallaBack;
/**
* @author Dash
* @date 2017/10/16
* @description:
*/
public class NetDataUtil {
/**
* 获取数据之前先判断网络状态
* @param path
* @param callaBack
*/
public static void getData(Context context,final String path, final JsonStringCallaBack callaBack) {
if (NetWorkUtil.isConn(context)){
Toast.makeText(context,"网络可用",Toast.LENGTH_SHORT).show();
AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
int responseCode = connection.getResponseCode();
if (responseCode == 200){
InputStream inputStream = connection.getInputStream();
String json = streamToString(inputStream,"utf-8");
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String json) {
//异步拿回这个json串,交给具体解析的那个类
callaBack.getJsonString(json);
}
};
asyncTask.execute();
}else {
NetWorkUtil.showNoNetWorkDlg(context);
}
}
private static String streamToString(InputStream inputStream, String charset) {
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,charset);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String s = null;
StringBuilder builder = new StringBuilder();
while ((s = bufferedReader.readLine()) != null){
builder.append(s);
}
bufferedReader.close();
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package news1510a.bawei.com.a14_zhoukao_test.util;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import news1510a.bawei.com.a14_zhoukao_test.R;
/**
* @author Dash
* @date 2017/9/27
* @description:
*
* 如果要使用网络状态的判断,需要权限....<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
*/
public class NetWorkUtil {
/**
* 这个方法是判断网络状态是否可用的
* @param context
* @return
*/
public static boolean isConn(Context context){
boolean bisConnFlag=false;
//1.获取网络连接的管理对象
ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
//2.通过管理者对象拿到网络的信息
NetworkInfo network = conManager.getActiveNetworkInfo();
if(network!=null){
//3.网络状态是否可用的返回值
bisConnFlag=network.isAvailable();
}
return bisConnFlag;
}
/**
* 如果没有网络 弹出dialog对话框,,,是否进入设置网络的页面
* @param context
*/
public static void showNoNetWorkDlg(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.mipmap.ic_launcher) //
.setTitle("警告") //
.setMessage("当前无网络,是否去设置?").setPositiveButton("设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 跳转到系统的网络设置界面
Intent intent = null;
// 先判断当前系统版本
if(android.os.Build.VERSION.SDK_INT > 10){ // 3.0以上
intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
}else{
intent = new Intent();
intent.setClassName("com.android.settings", "com.android.settings.WirelessSettings");
}
context.startActivity(intent);
}
}).setNegativeButton("取消", null).show();
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++
package news1510a.bawei.com.a14_zhoukao_test.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 news1510a.bawei.com.a14_zhoukao_test.R;
/**
* @author Dash
* @date 2017/10/16
* @description:
*/
public class FragmentDongTai extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dongtai_layout,container,false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package news1510a.bawei.com.a14_zhoukao_test.fragment;
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 java.util.ArrayList;
import java.util.List;
import news1510a.bawei.com.a14_zhoukao_test.R;
/**
* @author Dash
* @date 2017/10/16
* @description:
*/
public class FragmentHome extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;
private List<String> list;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home_layout,container,false);
tabLayout = view.findViewById(R.id.tab);
viewPager = view.findViewById(R.id.view_pager);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
list = new ArrayList<>();
list.add("动态");
list.add("热门");
list.add("发现");
//设置适配器
viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
@Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
@Override
public Fragment getItem(int position) {
Fragment fragment =null;
switch (position){
case 0:
fragment = new FragmentDongTai();
break;
case 1:
fragment = new FragmentReMen();
break;
case 2:
fragment = new FragmentFaXian();
break;
default:
break;
}
return fragment;
}
@Override
public int getCount() {
return list.size();
}
});
//关联
tabLayout.setupWithViewPager(viewPager);
}
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package news1510a.bawei.com.a14_zhoukao_test.fragment;
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 java.util.ArrayList;
import java.util.List;
import news1510a.bawei.com.a14_zhoukao_test.R;
/**
* @author Dash
* @date 2017/10/16
* @description:
*/
public class FragmentHome extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;
private List<String> list;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home_layout,container,false);
tabLayout = view.findViewById(R.id.tab);
viewPager = view.findViewById(R.id.view_pager);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
list = new ArrayList<>();
list.add("动态");
list.add("热门");
list.add("发现");
//设置适配器
viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
@Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
@Override
public Fragment getItem(int position) {
Fragment fragment =null;
switch (position){
case 0:
fragment = new FragmentDongTai();
break;
case 1:
fragment = new FragmentReMen();
break;
case 2:
fragment = new FragmentFaXian();
break;
default:
break;
}
return fragment;
}
@Override
public int getCount() {
return list.size();
}
});
//关联
tabLayout.setupWithViewPager(viewPager);
}
}
+++++++++++++++++++++++++++++++++++++++++++++++