1.BaseFragment 类
public abstract class BaseFragment extends Fragment {
//加载布局
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(initLayout(),container,false);
return view;
}
//初始化布局
protected abstract int initLayout();
//加载控件
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initView();
}
//初始化控件
protected abstract void initView();
//加载数据
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initData();
}
//初始化控件
protected abstract void initData();
//findViewVyId
protected <T extends View> T fvd(int resId){
return (T) getView().findViewById(resId);
}
}
2.BaseFragmentActivity 类
public abstract class BaseFragmentActivity extends FragmentActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//布局
setContentView(initLayout());
//控件
initView();
//数据
initData();
}
protected abstract int initLayout();
protected abstract void initView();
protected abstract void initData();
//findViewById
protected <T extends View> T fvd(int resId){
return (T) findViewById(resId);
}
}
3.主页
public class MainActivity extends BaseFragmentActivity {
private String[] titles = {"首页","wode","信息"};
private List<Fragment> data = new ArrayList<Fragment>();
private ViewPager pager;
private TabLayout tab;
private DrawerLayout draw;
//布局
@Override
protected int initLayout() {
return R.layout.activity_main;
}
//控件
@Override
protected void initView() {
pager = fvd(R.id.pager);
tab = fvd(R.id.tab);
draw = fvd(R.id.draw);
}
//数据
@Override
protected void initData() {
//侧拉
draw.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
data.add(new TabFrag1());
data.add(new TabFrag2());
data.add(new TabFrag3());
//适配器
TabAdter tabAdter = new TabAdter(getSupportFragmentManager(), data, titles);
pager.setAdapter(tabAdter);
//关联
tab.setupWithViewPager(pager);
}
}
4.主页布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
android:id="@+id/draw"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
></android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
></android.support.design.widget.TabLayout>
</LinearLayout>
<LinearLayout
android:layout_width="400dp"
android:layout_height="match_parent"
android:background="#ccc"
android:orientation="vertical"
android:layout_gravity="left"
>
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:id="@+id/name"
android:text="个人"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/guan"
android:text="关闭"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
5.Frag1
/**
* @Auther: 付明锟
* @Date: 2019/3/2 09:05:52
* @Description:
*/
public class TabFrag1 extends BaseFragment {
private List<JsonBean.DataBean> data = new ArrayList<JsonBean.DataBean>();
private String url = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
private int pager=1;
private PullToRefreshListView pull_list;
private PullAdter pullAdter;
//布局
@Override
protected int initLayout() {
return R.layout.tabfrag1;
}
//控件
@Override
protected void initView() {
pull_list = fvd(R.id.pull_list);
}
//数据
@Override
protected void initData() {
//设置支持上下拉
pull_list.setMode(PullToRefreshBase.Mode.BOTH);
//设置刷新时可以滚动
pull_list.setScrollingWhileRefreshingEnabled(true);
//适配器
pullAdter = new PullAdter(data, getActivity());
pull_list.setAdapter(pullAdter);
//加载数据
myData(pager);
//监听
pull_list.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
data.clear();
myData(pager);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
pager++;
myData(pager);
}
});
}
private void myData(int p) {
String murl = url+p;
Http.MyTaAxsk(murl, new Http.CallBack() {
@Override
public void getData(String str) {
//解析
jiexi(str);
}
});
}
//解析
private void jiexi(String str) {
Gson gson = new Gson();
JsonBean jsonBean = gson.fromJson(str, JsonBean.class);
List<JsonBean.DataBean> data2 = jsonBean.getData();
data.addAll(data2);
//刷新适配器
pullAdter.notifyDataSetChanged();
//关闭
pull_list.onRefreshComplete();
}
}
6.轮播和Grlid
public class TabFrag3 extends BaseFragment implements MyAsTack.CallBack {
private String url = "http://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1";
private List<JsonBean2.ResultsBean> data = new ArrayList<JsonBean2.ResultsBean>();
private List<String> data2 = new ArrayList<String>();
private Banner banner;
private GridView grid;
@Override
protected int initLayout() {
return R.layout.tabfrag3;
}
@Override
protected void initView() {
banner = fvd(R.id.banner);
grid = fvd(R.id.grid);
}
//数据
@Override
protected void initData() {
//轮播
data2.add("http://f.expoon.com/sub/news/2016/01/21/887844_230x162_0.jpg");
data2.add("http://f.expoon.com/sub/news/2016/01/21/580828_230x162_0.jpg");
data2.add("http://f.expoon.com/sub/news/2016/01/21/745921_230x162_0.jpg");
data2.add("http://f.expoon.com/sub/news/2016/01/21/158040_230x162_0.jpg");
banner.setImageLoader(new ImageLoader() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(getActivity()).load(path).into(imageView);
}
});
banner.setDelayTime(2000);
banner.setImages(data2);
banner.start();
MyAsTack tack = new MyAsTack();
tack.setCallBack(this);
tack.execute(url);
}
@Override
public String getData(String ss) {
Gson gson = new Gson();
JsonBean2 jsonBean2 = gson.fromJson(ss, JsonBean2.class);
List<JsonBean2.ResultsBean> results = jsonBean2.getResults();
data.addAll(results);
//适配器
ListAdter listAdter = new ListAdter(data, getActivity());
grid.setAdapter(listAdter);
return null;
}
}
7.Http
public class Http {
//网络判断
public static boolean isABoolean(Context context){
if (context!=null){
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info!=null){
return info.isAvailable();
}
}
return false;
}
//网络请求
public static String reputHttpGet(String s){
try {
URL url = new URL(s);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int code = connection.getResponseCode();
if (code == HttpURLConnection.HTTP_OK){
InputStream stream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream,"utf-8"));
StringBuilder builder = new StringBuilder();
String str = "";
while ((str=reader.readLine())!=null){
builder.append(str);
}
//Log.i("tag", "reputHttpGet: "+builder.toString());
return builder.toString();
}
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//接口
public interface CallBack{
void getData(String str);
}
//异步
public static void MyTaAxsk(String url,final CallBack callBack){
new AsyncTask<String,Void,String>() {
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
callBack.getData(s);
}
@Override
protected String doInBackground(String... strings) {
return Http.reputHttpGet(strings[0]);
}
}.execute(url);
}
}
8.Tab适配器
public class TabAdter extends FragmentPagerAdapter {
private List<Fragment> data;
private String[] titles;
public TabAdter(FragmentManager fm, List<Fragment> data, String[] titles) {
super(fm);
this.data = data;
this.titles = titles;
}
@Override
public Fragment getItem(int i) {
return data.get(i);
}
@Override
public int getCount() {
return data.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
}
9.多条目
public class ListAdter extends BaseAdapter {
private List<JsonBean2.ResultsBean> data;
private Context context;
public ListAdter(List<JsonBean2.ResultsBean> data, Context context) {
this.data = data;
this.context = context;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return position%2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
switch (getItemViewType(position)){
case 0:
Holder holder;
if (convertView == null){
convertView = View.inflate(context,R.layout.item1,null);
holder = new Holder();
holder.image1 = convertView.findViewById(R.id.image1);
holder.name1 = convertView.findViewById(R.id.name1);
convertView.setTag(holder);
}else{
holder = (Holder) convertView.getTag();
}
holder.name1.setText(data.get(position).getType());
Glide.with(context).load(data.get(position).getUrl()).into(holder.image1);
break;
case 1:
Holder2 holder2;
if (convertView == null){
convertView = View.inflate(context,R.layout.item2,null);
holder2 = new Holder2();
holder2.image2 = convertView.findViewById(R.id.image2);
convertView.setTag(holder2);
}else{
holder2 = (Holder2) convertView.getTag();
}
Glide.with(context).load(data.get(position).getUrl()).into(holder2.image2);
break;
}
return convertView;
}
class Holder{
ImageView image1;
TextView name1;
}
class Holder2{
ImageView image2;
}
}