//main方法
import android.support.annotation.IdRes; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.RadioGroup; import com.example.week02.Fragment.GengDuoFragment; import com.example.week02.Fragment.ShiChangFragment; import com.example.week02.Fragment.ShouYeFragment; import com.example.week02.Fragment.TongZhiFragment; import com.example.week02.Fragment.XiangFaFragment; 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); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { switch (checkedId){ case R.id.radio_01: addFragment(new ShouYeFragment()); break; case R.id.radio_02: addFragment(new XiangFaFragment()); break; case R.id.radio_03: addFragment(new ShiChangFragment()); break; case R.id.radio_04: addFragment(new TongZhiFragment()); break; case R.id.radio_05: addFragment(new GengDuoFragment()); break; default: break; } } }); //添加首页 页面 addFragment(new ShouYeFragment()); } public void addFragment(Fragment f){ FragmentManager manager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = manager.beginTransaction(); fragmentTransaction.replace(R.id.frame_layout,f); fragmentTransaction.commit(); } }
//适配器
public class MyLvAdapter extends BaseAdapter{ private List<Result.ResultsBean> lists; private Context context; //定义视图类型 private final int TYPE_TITLE=0; private final int TYPE_PIC=1; public MyLvAdapter(List<Result.ResultsBean> lists, Context context) { this.lists = lists; this.context = context; } @Override public int getCount() { return lists.size(); } //返回视图类型的数量 @Override public int getViewTypeCount() { return 2; } //根据当前的条目的下标,返回此条目对应的视图类型 @Override public int getItemViewType(int position) { //根据数据去判断返回的类型 Result.ResultsBean entity=lists.get(position); List<String> imgurl=entity.getImages(); if(imgurl==null){ return TYPE_TITLE; }else{ return TYPE_PIC; } } @Override public View getView(int position, View convertView, ViewGroup parent) { //根据position得到视图类型 int type=getItemViewType(position); if(type==TYPE_TITLE){ ViewHolderTitle holderTitle; if(convertView==null){ holderTitle=new ViewHolderTitle(); convertView=View.inflate(context, R.layout.item_pic,null); holderTitle.textView=(TextView) convertView.findViewById(R.id.tv_title); convertView.setTag(holderTitle); }else{ holderTitle=(ViewHolderTitle) convertView.getTag(); } holderTitle.textView.setText(lists.get(position).getDesc()); }else if(type==TYPE_PIC){ ViewHolderPic holderPic; if(convertView==null){ holderPic=new ViewHolderPic(); convertView=View.inflate(context, R.layout.item_pic,null); holderPic.textView=(TextView) convertView.findViewById(R.id.tv_title); holderPic.imageView=(ImageView) convertView.findViewById(R.id.img); convertView.setTag(holderPic); }else{ holderPic=(ViewHolderPic) convertView.getTag(); } holderPic.textView.setText(lists.get(position).getDesc()); //加载图片 ImageLoader.getInstance().displayImage( lists.get(position).getImages().get(0), holderPic.imageView, ImageloaderUtil.getImageOptions()); } return convertView; } @Override public long getItemId(int position) { return position; } @Override public Object getItem(int position) { return lists.get(position); } class ViewHolderTitle{ TextView textView; } class ViewHolderPic{ TextView textView; ImageView imageView; } }
//适配器
public class MyPagerAdapter extends FragmentPagerAdapter { //选项卡文字列表 private List<String> tabs; public MyPagerAdapter(FragmentManager fm, List<String> tabs) { super(fm); this.tabs = tabs; } //用于返回选项卡的文本 @Override public CharSequence getPageTitle(int position) { return tabs.get(position); } @Override public Fragment getItem(int position) { // if(position==0){ // return new DongTaiFragment(); // }else if() Fragment f=null; switch (position){ case 0: f=new DongTaiFragment();//动态页面 break; case 1: f=new ContentFramgnet();//热门页面 break; case 2: f=new FindFragment();//发现页面 break; } return f; } @Override public int getCount() { return tabs.size(); } }
//工具包
public class ImageloaderUtil { /** * ImageLoader的配置 * @param context */ public static void initConfig(Context context) { //配置 // File cacheFile=context.getExternalCacheDir(); File cacheFile= new File(Environment.getExternalStorageDirectory()+"/"+"image"); ImageLoaderConfiguration config=new ImageLoaderConfiguration.Builder(context) .memoryCacheExtraOptions(480, 800)//缓存图片最大的长和宽 .threadPoolSize(2)//线程池的数量 .threadPriority(4) .memoryCacheSize(2*1024*1024)//设置内存缓存区大小 .diskCacheSize(20*1024*1024)//设置sd卡缓存区大小 .diskCache(new UnlimitedDiskCache(cacheFile))//自定义缓存目录 .writeDebugLogs()//打印日志内容 .diskCacheFileNameGenerator(new Md5FileNameGenerator())//给缓存的文件名进行md5加密处理 .build(); ImageLoader.getInstance().init(config); } /** * 获取图片设置类 * @return */ public static DisplayImageOptions getImageOptions(){ DisplayImageOptions optionsoptions=new DisplayImageOptions.Builder() .cacheInMemory(true)//使用内存缓存 .cacheOnDisk(true)//使用磁盘缓存 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片格式 .displayer(new RoundedBitmapDisplayer(10))//设置圆角,参数代表弧度 .build(); return optionsoptions; } }
//工具包
public class MyTask extends AsyncTask<String,Void,String> { //申请一个接口类对象 private Icallbacks icallbacks; //将无参构造设置成私有的,使之在外部不能够调用 private MyTask(){} //定义有参构造方法 public MyTask(Icallbacks icallbacks) { this.icallbacks = icallbacks; } @Override protected String doInBackground(String... params) { String str=""; try { //使用HttpUrlConnection URL url=new URL(params[0]); HttpURLConnection connection=(HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); if(connection.getResponseCode()==200){ InputStream inputStream=connection.getInputStream(); //调用工具类中的静态方法 str=StreamToString.streamToStr(inputStream,"utf-8"); } } catch (MalformedURLException e) { e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); } return str; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); //解析,封装到bean,更新ui组件 icallbacks.updateUiByjson(s); } //定义一个接口 public interface Icallbacks{ /** * 根据回传的json字符串,解析并更新页面组件 * @param jsonstr */ void updateUiByjson(String jsonstr); } }
//工具包
public class NetStateUtil { /* * 判断网络连接是否已开 * true 已打开 false 未打开 * */ public static boolean isConn(Context context){ boolean bisConnFlag=false; ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo network = conManager.getActiveNetworkInfo(); if(network!=null){ bisConnFlag=conManager.getActiveNetworkInfo().isAvailable(); } return bisConnFlag; } /** * 当判断当前手机没有网络时选择是否打开网络设置 * @param context */ public static void showNoNetWorkDlg(final Context context) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setIcon(R.mipmap.ic_launcher) // .setTitle(R.string.app_name) // .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(); } }
//工具包
public class StreamToString { public static String streamToStr(InputStream inputStream,String chartSet){ StringBuilder builder=new StringBuilder(); try { BufferedReader br=new BufferedReader(new InputStreamReader(inputStream,chartSet)); String con; while ((con=br.readLine())!=null){ builder.append(con); } br.close(); return builder.toString(); } catch (Exception e) { e.printStackTrace(); } return ""; } }
//调用工具包
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); ImageloaderUtil.initConfig(this); } }
//Bean包解析数据
public class Result { /** * error : false * results : [{"_id":"59de2f22421aa90fe50c015c","createdAt":"2017-10-11T22:48:02.721Z","desc":"用 Kotlin 实现的基于物理的动画","images":["http://img.gank.io/58925abb-3e11-4d6e-9e44-a4567c03d03f"],"publishedAt":"2017-10-17T13:10:43.731Z","source":"web","type":"Android","url":"https://github.com/sagar-viradiya/AndroidPhysicsAnimation","used":true,"who":" Thunder Bouble"},{"_id":"59e46c6a421aa90fe50c0174","createdAt":"2017-10-16T16:23:06.637Z","desc":"Android 通用圆角布局,快速实现圆角需求。","images":["http://img.gank.io/4d9b99ba-cc97-4ef8-b834-477ad8a97100"],"publishedAt":"2017-10-17T13:10:43.731Z","source":"web","type":"Android","url":"https://github.com/GcsSloop/rclayout","used":true,"who":"sloop"},{"_id":"59e46c89421aa90fe7253598","createdAt":"2017-10-16T16:23:37.313Z","desc":"Android 加密工具包。","publishedAt":"2017-10-17T13:10:43.731Z","source":"web","type":"Android","url":"https://github.com/GcsSloop/encrypt","used":true,"who":"sloop"},{"_id":"59e55ecd421aa90fe725359c","createdAt":"2017-10-17T09:37:17.526Z","desc":"一个用 Kotlin 写的轻量级 URL 路由器框架","publishedAt":"2017-10-17T13:10:43.731Z","source":"web","type":"Android","url":"https://github.com/twocity/linker","used":true,"who":"ZhangTitanjum"},{"_id":"59e5740f421aa90fe725359f","createdAt":"2017-10-17T11:07:59.933Z","desc":"Android面试指南:我们需要怎样的工程师,我们需要成为怎样的工程师?","publishedAt":"2017-10-17T13:10:43.731Z","source":"web","type":"Android","url":"https://mp.weixin.qq.com/s?__biz=MzU4MjAzNTAwMA==&mid=2247483781&idx=2&sn=c5ef46cea309df058d2b168fada6dec0&chksm=fdbf32d2cac8bbc4b7f373c76bfc4d2447717634fdbc5afae565cca92551bfd152b329d5e103#rd","used":true,"who":null},{"_id":"59df3eac421aa90fef20347c","createdAt":"2017-10-12T18:06:36.692Z","desc":"文章中详实说明利用Cmake构造Ndk库需要注意的问题,以及部分Cmake命令的解读。","publishedAt":"2017-10-16T12:19:20.311Z","source":"web","type":"Android","url":"http://blog.csdn.net/qq_34902522/article/details/78144127","used":true,"who":null},{"_id":"59e1b47b421aa90fef203481","createdAt":"2017-10-14T14:53:47.998Z","desc":"详细介绍java垃圾回收的过程及相关算法","publishedAt":"2017-10-16T12:19:20.311Z","source":"chrome","type":"Android","url":"https://mp.weixin.qq.com/s?__biz=MzIwODI3MTc2Ng==&mid=2649647405&idx=1&sn=46a48cedd1540a994cd316ca164e005a&chksm=8f1f69d4b868e0c25245676ac55dc8d1fcad3dfa4bc63a2e0835b0a1155f1d2320a45a7c7078#rd","used":true,"who":"技术特工队"},{"_id":"59e4092c421aa90fe50c016d","createdAt":"2017-10-16T09:19:40.793Z","desc":"几条小经验帮你美化你的GitHub开源项目","publishedAt":"2017-10-16T12:19:20.311Z","source":"web","type":"Android","url":"https://mp.weixin.qq.com/s?__biz=MzIwMzYwMTk1NA==&mid=2247487344&idx=1&sn=744a9ebc0425fb3fa17c3f62eb59e421","used":true,"who":"陈宇明"},{"_id":"59dc7149421aa94e07d18490","createdAt":"2017-10-10T15:05:45.902Z","desc":"使用 Kotlin 实现的一个 Dribbble 客户端","images":["http://img.gank.io/05d6552f-97ba-4d52-ad33-3caeba5cb327","http://img.gank.io/84594f1b-d10e-42a3-afc1-c7d2bf9ac0cf"],"publishedAt":"2017-10-11T12:40:42.545Z","source":"web","type":"Android","url":"https://github.com/armcha/Ribble","used":true,"who":" Thunder Bouble"},{"_id":"59dcca81421aa94e0053bddf","createdAt":"2017-10-10T21:26:25.797Z","desc":"最近在给某某银行做项目的时,涉及到了数据埋点,性能监控等问题,那我们起先想到的有两种方案,方案之一就是借助第三方,比如友盟、Bugly等,由于项目是部署在银行的网络框架之内的,所以该方案不可行。","publishedAt":"2017-10-11T12:40:42.545Z","source":"web","type":"Android","url":"https://mp.weixin.qq.com/s?__biz=MzIyMjQ0MTU0NA==&mid=2247484445&idx=1&sn=8eef04a7932b58ef0012643db228fb32&chksm=e82c3d3adf5bb42c88333160a88c7b05fb5f45798434afa956fe1f1a58c7713ef121c7ea0af4&scene=0&key=7460e137ddd94f92f668e812cfc0aef8fde2bdf7943c7409875cce12a3baed3526f31e4a707ed86896ee8ddbbf761bb2f09b2d7406c3b9016589495240d835d967a2141231c43d084635a7df11647fb0&ascene=0&uin=MjMzMzgwOTEwMQ%3D%3D&devicetype=iMac+MacBookPro12%2C1+OSX+OSX+10.10.5+build(14F27)&version=11020201&pass_ticket=54ym37fDoXgDZm7nzjGt6KNDR9%2F9ZIU8%2Bo5kNcGEXqi8GKijls6et5TXcXxbERi%2F","used":true,"who":"Tamic (码小白)"}] */ private boolean error; private List<ResultsBean> results; public boolean isError() { return error; } public void setError(boolean error) { this.error = error; } public List<ResultsBean> getResults() { return results; } public void setResults(List<ResultsBean> results) { this.results = results; } public static class ResultsBean { /** * _id : 59de2f22421aa90fe50c015c * createdAt : 2017-10-11T22:48:02.721Z * desc : 用 Kotlin 实现的基于物理的动画 * images : ["http://img.gank.io/58925abb-3e11-4d6e-9e44-a4567c03d03f"] * publishedAt : 2017-10-17T13:10:43.731Z * source : web * type : Android * url : https://github.com/sagar-viradiya/AndroidPhysicsAnimation * used : true * who : Thunder Bouble */ private String _id; private String createdAt; private String desc; private String publishedAt; private String source; private String type; private String url; private boolean used; private String who; private List<String> images; public String get_id() { return _id; } public void set_id(String _id) { this._id = _id; } public String getCreatedAt() { return createdAt; } public void setCreatedAt(String createdAt) { this.createdAt = createdAt; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String getPublishedAt() { return publishedAt; } public void setPublishedAt(String publishedAt) { this.publishedAt = publishedAt; } public String getSource() { return source; } public void setSource(String source) { this.source = source; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public boolean isUsed() { return used; } public void setUsed(boolean used) { this.used = used; } public String getWho() { return who; } public void setWho(String who) { this.who = who; } public List<String> getImages() { return images; } public void setImages(List<String> images) { this.images = images; } } }
//Fragment
public class ContentFramgnet extends Fragment { private View v; private int pageIndex=1; private MyLvAdapter lvAdapter; private List<Result.ResultsBean> lists=new ArrayList<>(); private XListView xlv; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { v = View.inflate(getActivity(), R.layout.content,null); xlv = (XListView) v.findViewById(R.id.xlv); //进行设置 xlv.setPullRefreshEnable(true); xlv.setPullLoadEnable(true); //设置监听 xlv.setXListViewListener(new XListView.IXListViewListener() { @Override public void onRefresh() { pageIndex=1; MyTask mytask=new MyTask(new MyTask.Icallbacks() { @Override public void updateUiByjson(String jsonstr) { Gson gson=new Gson(); Result result=gson.fromJson(jsonstr, Result.class); //要显示的数据-添加到最前面 lists.addAll(0,result.getResults()); //设置适配器 setAdapter(); //关闭视图 new Handler().postDelayed(new Runnable() { @Override public void run() { xlv.stopRefresh(); } },1000); } }); mytask.execute("http://gank.io/api/data/Android/10/"+pageIndex); } @Override public void onLoadMore() { pageIndex++; MyTask mytask=new MyTask(new MyTask.Icallbacks() { @Override public void updateUiByjson(String jsonstr) { Gson gson=new Gson(); Result result=gson.fromJson(jsonstr, Result.class); //要显示的数据 lists.addAll(result.getResults()); //设置适配器 setAdapter(); //关闭视图 new Handler().postDelayed(new Runnable() { @Override public void run() { xlv.stopLoadMore(); } },1000); } }); mytask.execute("http://gank.io/api/data/Android/10/"+pageIndex); } }); return v; } public void setAdapter(){ if(lvAdapter==null){ lvAdapter=new MyLvAdapter(lists,getActivity()); xlv.setAdapter(lvAdapter); }else{ lvAdapter.notifyDataSetChanged(); } } private void requestNetData() { MyTask mytask=new MyTask(new MyTask.Icallbacks() { @Override public void updateUiByjson(String jsonstr) { Gson gson=new Gson(); Result result=gson.fromJson(jsonstr, Result.class); //要显示的数据 lists.addAll(result.getResults()); //设置适配器 setAdapter(); } }); mytask.execute("http://gank.io/api/data/Android/10/"+pageIndex); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); requestNetData(); } }
//Fragment
public class DongTaiFragment extends Fragment { private XListView xlv; private int pageIndex=1; private MyLvAdapter lvAdapter; private List<Result.ResultsBean> lists=new ArrayList<>(); @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View inflate = inflater.inflate(R.layout.don_layout, null); xlv = (XListView) inflate.findViewById(R.id.xlv); xlv = (XListView) inflate.findViewById(R.id.xlv); //进行设置 xlv.setPullRefreshEnable(true); xlv.setPullLoadEnable(true); //设置监听 xlv.setXListViewListener(new XListView.IXListViewListener() { @Override public void onRefresh() { pageIndex = 1; MyTask mytask = new MyTask(new MyTask.Icallbacks() { @Override public void updateUiByjson(String jsonstr) { Gson gson = new Gson(); Result result = gson.fromJson(jsonstr, Result.class); //要显示的数据-添加到最前面 lists.addAll(0, result.getResults()); //设置适配器 setAdapter(); //关闭视图 new Handler().postDelayed(new Runnable() { @Override public void run() { xlv.stopRefresh(); } }, 1000); } }); mytask.execute("http://gank.io/api/data/Android/10/" + pageIndex); } @Override public void onLoadMore() { pageIndex++; MyTask mytask = new MyTask(new MyTask.Icallbacks() { @Override public void updateUiByjson(String jsonstr) { Gson gson = new Gson(); Result result = gson.fromJson(jsonstr, Result.class); //要显示的数据 lists.addAll(result.getResults()); //设置适配器 setAdapter(); //关闭视图 new Handler().postDelayed(new Runnable() { @Override public void run() { xlv.stopLoadMore(); } }, 1000); } }); mytask.execute("http://gank.io/api/data/Android/10/" + pageIndex); } }); return inflate; } public void setAdapter() { if (lvAdapter == null) { lvAdapter = new MyLvAdapter(lists, getActivity()); xlv.setAdapter(lvAdapter); } else { lvAdapter.notifyDataSetChanged(); } } private void requestNetData() { MyTask mytask = new MyTask(new MyTask.Icallbacks() { @Override public void updateUiByjson(String jsonstr) { Gson gson = new Gson(); Result result = gson.fromJson(jsonstr, Result.class); //要显示的数据 lists.addAll(result.getResults()); //设置适配器 setAdapter(); } }); mytask.execute("http://gank.io/api/data/Android/10/" + pageIndex); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); requestNetData(); } }
//Fragment
public class FindFragment extends Fragment{ private XListView xlv; private int pageIndex=1; private MyLvAdapter lvAdapter; private List<Result.ResultsBean> lists=new ArrayList<>(); @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View inflate = inflater.inflate(R.layout.fa_layout, null); xlv = (XListView) inflate.findViewById(R.id.xlv); //进行设置 xlv.setPullRefreshEnable(true); xlv.setPullLoadEnable(true); //设置监听 xlv.setXListViewListener(new XListView.IXListViewListener() { @Override public void onRefresh() { MyTask mytask=new MyTask(new MyTask.Icallbacks() { @Override public void updateUiByjson(String jsonstr) { Gson gson=new Gson(); Result result=gson.fromJson(jsonstr, Result.class); //要显示的数据-添加到最前面 lists.addAll(0,result.getResults()); //设置适配器 setAdapter(); //关闭视图 new Handler().postDelayed(new Runnable() { @Override public void run() { xlv.stopRefresh(); } },1000); } }); mytask.execute("http://gank.io/api/data/Android/10/"+pageIndex); } @Override public void onLoadMore() { pageIndex++; MyTask mytask=new MyTask(new MyTask.Icallbacks() { @Override public void updateUiByjson(String jsonstr) { Gson gson=new Gson(); Result result=gson.fromJson(jsonstr, Result.class); //要显示的数据 lists.addAll(result.getResults()); //设置适配器 setAdapter(); //关闭视图 new Handler().postDelayed(new Runnable() { @Override public void run() { xlv.stopLoadMore(); } },1000); } }); mytask.execute("http://gank.io/api/data/Android/10/"+pageIndex); } }); return inflate; } public void setAdapter(){ if(lvAdapter==null){ lvAdapter=new MyLvAdapter(lists,getActivity()); xlv.setAdapter(lvAdapter); }else{ lvAdapter.notifyDataSetChanged(); } } private void requestNetData() { MyTask mytask=new MyTask(new MyTask.Icallbacks() { @Override public void updateUiByjson(String jsonstr) { Gson gson=new Gson(); Result result=gson.fromJson(jsonstr, Result.class); //要显示的数据 lists.addAll(result.getResults()); //设置适配器 setAdapter(); } }); mytask.execute("http://gank.io/api/data/Android/10/"+pageIndex); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); requestNetData(); } }
//Fragment
public class GengDuoFragment extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View inflate = inflater.inflate(R.layout.layout_fragment_normal, null); TextView textView = (TextView) inflate.findViewById(R.id.tvv); textView.setText("更多"); return inflate; } }
//Fragment
public class ShiChangFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View inflate = inflater.inflate(R.layout.layout_fragment_normal, null); TextView textView = (TextView) inflate.findViewById(R.id.tvv); textView.setText("市场"); return inflate; } }
//Fragment
public class ShouYeFragment extends Fragment{ private View inflate; private TabLayout tab; private ViewPager viewpager; private List<String> tabs; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { inflate = inflater.inflate(R.layout.shouye_layout, null); initView(); return inflate; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); tabs=new ArrayList<String>(); tabs.add("动态"); tabs.add("热门"); tabs.add("发现"); //设置viewpager的适配器 viewpager.setAdapter(new MyPagerAdapter(getChildFragmentManager(),tabs)); //进行关联 tab.setupWithViewPager(viewpager); } private void initView() { tab = (TabLayout) inflate.findViewById(R.id.tab); viewpager = (ViewPager) inflate.findViewById(R.id.vp); } }
//Fragment
public class TongZhiFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View inflate = inflater.inflate(R.layout.layout_fragment_normal, null); TextView textView = (TextView) inflate.findViewById(R.id.tvv); textView.setText("通知"); return inflate; } }
//Fragment
public class XiangFaFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View inflate = inflater.inflate(R.layout.layout_fragment_normal, null); TextView textView = (TextView) inflate.findViewById(R.id.tvv); textView.setText("想法"); return inflate; } }
//选中时的颜色
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!--选中时时红色--> <item android:state_checked="true" android:drawable="@android:color/holo_red_dark"></item> <item android:drawable="@android:color/white"></item> </selector>
//activity-main
<?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" tools:context="com.example.week02.MainActivity"> <RadioGroup android:layout_width="match_parent" android:layout_height="40dp" android:layout_alignParentBottom="true" android:orientation="horizontal" android:id="@+id/radio_group" > <RadioButton android:layout_width="0dp" android:layout_height="match_parent" android:text="首页" android:padding="10dp" android:gravity="center" android:button="@null" android:id="@+id/radio_01" android:background="@drawable/radiobutton_selector" android:layout_weight="1" android:checked="true" /> <RadioButton android:layout_width="0dp" android:layout_height="match_parent" android:text="想法" android:padding="10dp" android:gravity="center" android:button="@null" android:id="@+id/radio_02" android:background="@drawable/radiobutton_selector" android:layout_weight="1" /> <RadioButton android:layout_width="0dp" android:layout_height="match_parent" android:text="市场" android:padding="10dp" android:gravity="center" android:button="@null" android:id="@+id/radio_03" android:background="@drawable/radiobutton_selector" android:layout_weight="1" /> <RadioButton android:layout_width="0dp" android:layout_height="match_parent" android:text="通知" android:padding="10dp" android:gravity="center" android:button="@null" android:id="@+id/radio_04" android:background="@drawable/radiobutton_selector" android:layout_weight="1" /> <RadioButton android:layout_width="0dp" android:layout_height="match_parent" android:text="更多" android:padding="10dp" android:gravity="center" android:button="@null" android:id="@+id/radio_05" android:background="@drawable/radiobutton_selector" android:layout_weight="1" /> </RadioGroup> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/radio_group" android:id="@+id/frame_layout" ></FrameLayout> </RelativeLayout>
//content
<?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"> <!--添加xlistview的组件--> <com.example.week02.view.XListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/xlv" ></com.example.week02.view.XListView> </LinearLayout>
//don_Layout
<?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"> <!--添加xlistview的组件--> <com.example.week02.view.XListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/xlv" ></com.example.week02.view.XListView> </LinearLayout>
//fa-layout
<?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"> <!--添加xlistview的组件--> <com.example.week02.view.XListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/xlv" ></com.example.week02.view.XListView> </LinearLayout>
//item
<?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"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tv_title" /> </LinearLayout>
//item_pic
<?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" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_title" /> <ImageView android:layout_width="150dp" android:layout_height="150dp" android:scaleType="fitXY" android:id="@+id/img" android:layout_gravity="center" /> </LinearLayout>
//layout_fragment_normal
<?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" android:orientation="vertical" android:gravity="center" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tvv" android:textSize="22sp" /> </LinearLayout>
//shouye_layout
<?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" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:focusableInTouchMode="true" android:focusable="true"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="羞羞哒铁拳 开心麻花" /> <!--tabLayout 修改 app:tabGravity="fill" app:tabMode="fixed",使选项卡可以平铺整个屏幕 --> <android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="40dp" app:tabGravity="fill" app:tabIndicatorColor="@color/colorAccent" app:tabMode="fixed" app:tabSelectedTextColor="@color/colorPrimaryDark" app:tabTextColor="@color/colorPrimary" android:id="@+id/tab" ></android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/vp" ></android.support.v4.view.ViewPager> </LinearLayout>
//要加的权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
android:name=".MyApplication"
//还有导入XListView-Android-master文件里面的文件