题目要求:
1, (25分) 使用TabLayout实现页面的导航切换
2, (25分) 实现ViewPager + Fragment ,切换页面, 2个Fragment;
3, (25分) 第一个Fragment中, 获取网络数据,展示到RecyclerView
地址: http://app.vmoiver.com/apiv3/post/getPostInCate?cateid=0&p=1
解析字段: title , image
4, (25分) 第二个Fragment中, 通过webView展示http://baidu.com网页信息
效果图
代码实例:
导入jar包
xUtils 和 gson 的jar包
添加依赖:
compile 'com.android.support:design:26.0.0-alpha1'
compile 'com.android.support:recyclerview-v7:25.3.1'
一, 页面:
main_activity.html
xml version="1.0" encoding="utf-8"?>fragment_my_fragment01.xmlxmlns: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" android:orientation="vertical" tools:context="bw.com.day18_test.MainActivity"> android:layout_width="match_parent" android:layout_height="60dp" android:id="@+id/tb_id" /> android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/vp_id" />
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"
tools:context="bw.com.day18_test.MyFragment01">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rv_id"/>
fragment_my_fragment02.xml
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"
tools:context="bw.com.day18_test.MyFragment02">
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/web_view_id"/>
二, 代码
MyApp.java
public class MyApp extends Application { @Override public void onCreate() { super.onCreate(); //初始化xUtils x.Ext.init(this); x.Ext.setDebug(true); } }
注意: 必须要在清单文件中,
添加权限 : 网络权限, 读写SD卡的权限
MainActivity.java
public class MainActivity extends AppCompatActivity { //导航 private TabLayout mTb; private Listtitles; //内容 private ViewPager mVp; private List data; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //构建标题 titles = new ArrayList<>(); titles.add("第一页"); titles.add("第二页"); //构建数据源 data = new ArrayList<>(); data.add(new MyFragment01()); data.add(new MyFragment02()); //构建适配器 adapter = new MyAdapter(getSupportFragmentManager()); mVp.setAdapter(adapter); //将tabLayout 和ViewPager 绑定到一起 mTb.setupWithViewPager(mVp); } private void initView() { mTb = (TabLayout) findViewById(R.id.tb_id); mVp = (ViewPager) findViewById(R.id.vp_id); } //TODO 自定义ViewPager 的适配器 class MyAdapter extends FragmentPagerAdapter { public MyAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return data.get(position); } @Override public int getCount() { return data.size(); } @Override public CharSequence getPageTitle(int position) { return titles.get(position); } } }
MyFragment01.java
/** * 网络数据请求,解析 --- RecyclerView */ public class MyFragment01 extends Fragment { private RecyclerView mRv;//控件 private ListdataBean; private MyRecycleViewAdapter adapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_my_fragment01, container, false); mRv = (RecyclerView) view.findViewById(R.id.rv_id); //设置RecycleView显示的布局 LinearLayoutManager manager = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false); mRv.setLayoutManager(manager); //TODO 通过xUtils 获取数据 RequestParams params = new RequestParams("http://app.vmoiver.com/apiv3/post/getPostInCate?cateid=0&p=1"); x.http().get(params, new Callback.CommonCallback () { @Override public void onSuccess(String result) { //TODO 得到数据源 VMoive vMoive = new Gson().fromJson(result,VMoive.class); dataBean = vMoive.getData(); //TODO 初始化适配器 adapter = new MyRecycleViewAdapter(getContext(),dataBean); mRv.setAdapter(adapter); } @Override public void onError(Throwable throwable, boolean b) {} @Override public void onCancelled(CancelledException e) {} @Override public void onFinished() { } }); return view; } }
MyFragment02.java
public class MyFragment02 extends Fragment { private WebView mVb; private String url = "http://baidu.com"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_my_fragment02, container, false); mVb = (WebView) view.findViewById(R.id.web_view_id); //TODO 加载地址 mVb.loadUrl(url); //TODO 设置在当前App中显示 mVb.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { //系统默认会打开系统浏览器去打开网页,为了要显示在自己的webview中必须设置这个属性 view.loadUrl(url); return super.shouldOverrideUrlLoading(view, request); } }); //TODO 设置支持js WebSettings webSettings = mVb.getSettings(); webSettings.setJavaScriptEnabled(true); return view; } }
MyRecycleViewAdapter.java
public class MyRecycleViewAdapter extends RecyclerView.Adapter{ private Context context; private List data; public MyRecycleViewAdapter(Context context, List data) { this.context = context; this.data = data; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { //TODO 绑定页面 View view = LayoutInflater.from(context).inflate(R.layout.rv_item,parent,false); ViewHolder viewHolder = new ViewHolder(view); return viewHolder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { //赋值 holder.tv.setText(data.get(position).getTitle()); ImageOptions options = new ImageOptions.Builder() .setUseMemCache(true) .setFailureDrawableId(R.mipmap.ic_launcher) .setLoadingDrawableId(R.mipmap.ic_launcher) .build(); x.image().bind(holder.iv,data.get(position).getImage(),options); } @Override public int getItemCount() { return data.size(); } class ViewHolder extends RecyclerView.ViewHolder { ImageView iv; TextView tv; public ViewHolder(View itemView) { super(itemView); this.iv = (ImageView) itemView.findViewById(R.id.iv_id); this.tv = (TextView) itemView.findViewById(R.id.tv_id); } } }
VMovie.java -- gsonFormat 生成的实体类