技术点描述:
1.MVC架构,分包明确、抽取Activity基类和Fragment基类
2.异步 AsyncTask+ HttpUrlConnection + 单例模式 + 接口回调封装网络框架工具类(get、文本、图片)
3.TableLayout + Fragment + ViewPager封装UI框架
4.Json数据解析
5.PullToRefresh下拉刷新、上拉加载
6.WebView加载互联网页面、WebSettings及WebViewClient的使用
1.androidmanifest.xml添加权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2. 简单 封装 BaseFragment
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
initdata();
initview();
}
public abstract int getLayoutId();
public abstract void initdata();
public abstract void initview();
}
3. 简单 封装 BaseFragment
public abstract class BaseFragment extends Fragment {
private View initid;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initdata();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
initid = getLayoutId(inflater, container);
return initid;
}
public abstract View getLayoutId(LayoutInflater inflater, ViewGroup container);
public abstract void initdata();
}
4. 封装网络请求 HttpURLConnection
/*
*Name:fanyanlong
*Time:15:31
*Name:Android_zx_week01
* 单例模式
*/
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUtils {
private ConnectivityManager connectivityManager;
private NetworkInfo activeNetworkInfo;
private URL url;
private HttpURLConnection httpURLConnection;
private InputStream inputStream;
private ByteArrayOutputStream byteArrayOutputStream;
private byte[] bytes;
//懒汉式
//静态变量
private static HttpUtils httpUtils;
//私有化构造方法
private HttpUtils(){
}
// 外部访问方法
public static synchronized HttpUtils getInstance(){
if (httpUtils==null){
httpUtils = new HttpUtils();
}
return httpUtils;
}
//网络状态判断
public boolean isnetwork(Context context){
//获取管理类
connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//获取网络状态
activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
if(activeNetworkInfo!=null){
return activeNetworkInfo.isAvailable();
}
return false;
}
//网络请求 (不可以放主线程) 出现ANR异常
public String getData(String path) throws IOException {
//初始化统一资源定位符
url = new URL(path);
//打开网络请求
httpURLConnection = (HttpURLConnection) url.openConnection();
//设置请求方式
httpURLConnection.setRequestMethod("GET");
//请求超时设置
httpURLConnection.setConnectTimeout(1000);
//读取超时设置
httpURLConnection.setReadTimeout(1000);
//判断请求
if(httpURLConnection.getResponseCode()==200){
//初始化输入流
inputStream = httpURLConnection.getInputStream();
//转换流
byteArrayOutputStream = new ByteArrayOutputStream();
bytes = new byte[1024];
int len;
while ((len=inputStream.read(bytes))!=-1){
byteArrayOutputStream.write(bytes,0,len);
}
//关闭
inputStream.close();
}
return byteArrayOutputStream.toString();
}
5.主页面 MainActivity 实现
public class MainActivity extends BaseActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
private ArrayList<Fragment> fragments;
private ArrayList<String> strings;
@Override
public int getLayoutId() {
return R.layout.activity_main;
}
@Override
public void initdata() {
}
@Override
public void initview() {
tabLayout = findViewById(R.id.table_one);
viewPager = findViewById(R.id.viewpager_one);
fragments = new ArrayList<>();
fragments.add(new TjFragment());
fragments.add(new RdFragment());
fragments.add(new BwzxFragment());
fragments.add(new BjFragment());
fragments.add(new DmFragment());
fragments.add(new YlFragment());
strings = new ArrayList<>();
strings.add("推荐");
strings.add("热点");
strings.add("最新");
strings.add("北京");
strings.add("动漫");
strings.add("娱乐");
viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return strings.get(position);
}
});
//tablayout 绑定viewpage 滑动
tabLayout.setupWithViewPager(viewPager);
}
}
6.主页面 TjFragment 实现
public class TjFragment extends BaseFragment {
private View inflate;
private String data;
int page=2;
private Gson gson;
private ListBean listBean;
private List<ListBean.ResultBean.NewsBean> news;
public List<ListBean.ResultBean.BannerBean> bannerlist;
XBanner xbanner;
private MyListView listView;
private PullToRefreshScrollView pullToRefreshScrollView;
public static final String TAG="TjFragment";
private Bundle bundle;
private ArrayList<ListBean.ResultBean.NewsBean> newsBeans = new ArrayList<>();
@Override
public View getLayoutId(LayoutInflater inflater, ViewGroup container) {
inflate = inflater.inflate(R.layout.fragment_tj, null);
xbanner = inflate.findViewById(R.id.fragment_xbanner);
pullToRefreshScrollView = inflate.findViewById(R.id.fragment_pulltoscview);
listView = inflate.findViewById(R.id.fragment_listview);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
private String url1;
private Intent intent;
private String url;
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
intent = new Intent(getActivity(), TjActivity.class);
bundle = new Bundle();
url1 = news.get(i).getUrl();
if (url1!=null){
intent.putExtra("webpath",url1);
getActivity().startActivity(intent);
}else {
Log.d(TAG, "webpath 为空");
}
}
});
pullToRefreshScrollView.setMode(PullToRefreshBase.Mode.BOTH);//设置模式
//添加点击事件
pullToRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
//下拉刷新
@Override
public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
page=2;
newsBeans.clear();
new myAysnc().execute();
pullToRefreshScrollView.onRefreshComplete();//关闭刷新
}
//上拉加载
@Override
public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
page++;
new myAysnc().execute();
pullToRefreshScrollView.onRefreshComplete();//关闭刷新
}
});
return inflate;
}
@Override
public void initdata() {
if (HttpUtil.getInstance().getNet(getActivity())) {
Toast.makeText(getActivity(), "恭喜你请求成功!", Toast.LENGTH_SHORT).show();
new myAysnc().execute();
} else {
Toast.makeText(getActivity(), "请求失败啦!", Toast.LENGTH_SHORT).show();
}
}
class myAysnc extends AsyncTask<Integer, Integer, String> {
String path = "http://blog.zhaoliang5156.cn/api/news/bannernews"+page+".json";
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("SS", "onPostExecute: "+s);
gson = new Gson();
listBean = gson.fromJson(s, ListBean.class);
bannerlist = listBean.getResult().getBanner();
news = listBean.getResult().getNews();
xbanner.removeAllViews();
xbanner.setData(bannerlist,null);
newsBeans.addAll(news);
xbanner.setmAdapter(new XBanner.XBannerAdapter() {
@Override
public void loadBanner(XBanner banner, View view, int position) {
Glide.with(getActivity()).load(bannerlist.get(position).getImage()).into((ImageView) view);
}
});
//刷新适配器
ListAdapter listAdapter = new ListAdapter(newsBeans, getActivity());
listView.setAdapter(listAdapter);
}
@Override
protected String doInBackground(Integer... integers) {
try {
Log.d(TAG, "dopath===: "+path);
data = HttpUtil.getInstance().getData(path);
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
}
7.主页面 Activity 布局实现
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
layout="@layout/lun"
></include>
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
app:tabTextColor="@color/colorPrimaryDark"
app:tabSelectedTextColor="@color/colorPrimary"
android:layout_height="wrap_content"
android:id="@+id/table_one"
></com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewpager_one"
></androidx.viewpager.widget.ViewPager>
</LinearLayout>
8.主页面 TjFragment 布局实现
<?xml version="1.0" encoding="utf-8"?>
<com.handmark.pulltorefresh.library.PullToRefreshScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_pulltoscview"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".frgment.TjFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<com.stx.xhb.xbanner.XBanner
android:id="@+id/fragment_xbanner"
android:layout_width="match_parent"
android:layout_height="220dp">
</com.stx.xhb.xbanner.XBanner>
<com.bw.xushanshan.MyListView
android:id="@+id/fragment_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"></com.bw.xushanshan.MyListView>
</LinearLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
想要源码评论区回复我,给你私发链接
Kotlin基础篇【1】Android Studio 3.0 Kotlin环境配置
Kotlin基础篇【2】初识Kotlin的意义
Kotlin基础篇【3】Kotlin基础语法
=======Kotlin基础篇【4】Kotlin基础语法即将更新=======
网友评论