布局
activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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"
android:orientation="vertical"
android:id="@+id/drawerLayout"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/bottomViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
android:id="@+id/bottomTabLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
app:tabGravity="fill">
</android.support.design.widget.TabLayout>
</LinearLayout>
<FrameLayout
android:id="@+id/leftMenu"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
></FrameLayout>
</android.support.v4.widget.DrawerLayout>
left_fragment
<?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:background="#5f9f9f"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/menu">
</ListView>
</LinearLayout>
left_image_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_height="200dp">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"
android:id="@+id/imageView"/>
</LinearLayout>
left_text_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:text="11111"
android:textSize="20dp"
android:gravity="center"
android:padding="20dp"/>
</LinearLayout>
home_fragment
<?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="match_parent"
android:textSize="20dp"
android:text="首页"/>
</LinearLayout>
movie_fragment
<?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.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/movietab"
app:tabGravity="fill">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/movieVP">
</android.support.v4.view.ViewPager>
</LinearLayout>
movie_jj_fragment
<?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">
<com.youth.banner.Banner
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/banner">
</com.youth.banner.Banner>
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pullList"
android:layout_weight="1">
</com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>
list_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="5dp"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:paddingBottom="50dp"
android:text="111111"/>
<TextView
android:id="@+id/neirong"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="22222"/>
</LinearLayout>
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
工具类
App
import android.app.Application;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration imageLoaderConfiguration=ImageLoaderConfiguration.createDefault(this);
ImageLoader.getInstance().init(imageLoaderConfiguration);
}
}
BaseActivity
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
public abstract class BaseActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(LayoutResId());
initView(savedInstanceState);
initData();
}
protected abstract void initData();
protected abstract void initView(Bundle savedInstanceState);
protected abstract int LayoutResId();
}
BaseFragment
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public abstract class BaseFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(getLayoutResId(),container,false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
initView(view);
initData();
}
protected abstract void initData();
protected abstract void initView(View view);
protected abstract int getLayoutResId();
}
NetUtil
import android.os.AsyncTask;
import com.google.common.io.CharStreams;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class NetUtil {
//接口
public interface CallBack<T>{
void getdata(T t);
}
public static void yibu(String lujing,final Class clazz,final CallBack callBack){
new AsyncTask<String, Void, Object>() {
@Override
protected Object doInBackground(String... strings) {
return NetUtil.jiexi(strings[0],clazz);
}
@Override
protected void onPostExecute(Object o) {
callBack.getdata(o);
}
}.execute(lujing);
}
private static <T> T jiexi(String lujing, Class clazz) {
T t = (T) new Gson().fromJson(NetUtil.geturl(lujing), clazz);
return t;
}
public static String geturl(String lujing) {
String result="";
try {
URL url=new URL(lujing);
HttpURLConnection urlConnection= (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(3000);
urlConnection.setConnectTimeout(3000);
int code = urlConnection.getResponseCode();
if (code==200){
InputStream inputStream=urlConnection.getInputStream();
InputStreamReader streamReader=new InputStreamReader(inputStream,"UTF-8");
result = CharStreams.toString(streamReader);
return result.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
MainActivity
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import com.bwie.asus.mn3.adapter.BottomVPAdapter;
import com.bwie.asus.mn3.fragment.LeftFragment;
import com.bwie.asus.mn3.utils.BaseActivity;
public class MainActivity extends BaseActivity {
private DrawerLayout drawerLayout;
private ViewPager bottomVP;
private TabLayout bottomTab;
private ActionBarDrawerToggle mToogle;
@Override
protected void initData() {
}
@Override
protected void initView(Bundle savedInstanceState) {
//找控件
drawerLayout = findViewById(R.id.drawerLayout);
bottomVP = findViewById(R.id.bottomViewPager);
bottomTab = findViewById(R.id.bottomTabLayout);
//给左上角图标的左边加上一个返回的图标
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//ActionBarDrawerToggle 动作栏抽屉切换开关
mToogle = new ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close);
//监听侧边栏的开关
drawerLayout.addDrawerListener(mToogle);
//同步
mToogle.syncState();
if (savedInstanceState==null){
getSupportFragmentManager().beginTransaction()
.add(R.id.leftMenu,new LeftFragment())
.commit();
}
//适配器
bottomVP.setAdapter(new BottomVPAdapter(getSupportFragmentManager()));
bottomTab.setupWithViewPager(bottomVP);
}
public void showPage(int position){
bottomVP.setCurrentItem(position);
drawerLayout.closeDrawer(Gravity.START);
}
//点击
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToogle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected int LayoutResId() {
return R.layout.activity_main;
}
}
BottomVPAdapter
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.bwie.asus.mn3.fragment.HomeFragment;
import com.bwie.asus.mn3.fragment.MineFragment;
import com.bwie.asus.mn3.fragment.MovieFragment;
public class BottomVPAdapter extends FragmentPagerAdapter {
private String[] botnames=new String[]{"首页","影视","我的"};
public BottomVPAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new HomeFragment();
case 1:
return new MovieFragment();
case 2:
return new MineFragment();
}
return null;
}
@Override
public int getCount() {
return botnames.length;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return botnames[position];
}
}
LeftFragment
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.bwie.asus.mn3.MainActivity;
import com.bwie.asus.mn3.R;
import com.bwie.asus.mn3.adapter.LeftAdapter;
import static android.app.Activity.RESULT_OK;
public class LeftFragment extends Fragment {
private ListView listView;
private LeftAdapter leftAdapter;
private int PICK_IMAGE=100;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.left_fragment,container,false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = view.findViewById(R.id.menu);
leftAdapter = new LeftAdapter(getActivity());
listView.setAdapter(leftAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position==0){
Intent intent=new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent,PICK_IMAGE);
}
((MainActivity)getActivity()).showPage(position-1);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==PICK_IMAGE){
if (resultCode==RESULT_OK){
Uri uri=data.getData();
Intent intent=new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri,"image/*");
intent.putExtra("CROP",true);
intent.putExtra("aspectX",1);
intent.putExtra("aspectY",1);
intent.putExtra("outputX",100);
intent.putExtra("outputY",100);
intent.putExtra("return-data",true);
startActivityForResult(intent,200);
}
return;
}
if (requestCode==200){
if (resultCode==RESULT_OK){
Bitmap bitmap=data.getParcelableExtra(String.valueOf("data"));
leftAdapter.setIconBitmap(bitmap);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
LeftAdapter
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bwie.asus.mn3.R;
public class LeftAdapter extends BaseAdapter {
private String[] menusname=new String[]{"首页","影视","我的"};
private final int ITEM_COUNT=2;
private final int IMAGE=0;
private final int TEXT=1;
private Context mContext;
private Bitmap iconBitmap;
public LeftAdapter(Context mContext) {
this.mContext = mContext;
}
public void setIconBitmap(Bitmap iconBitmap) {
this.iconBitmap = iconBitmap;
notifyDataSetChanged();
}
@Override
public int getCount() {
return menusname.length+1;
}
@Override
public int getViewTypeCount() {
return ITEM_COUNT;
}
@Override
public int getItemViewType(int position) {
return position==0?IMAGE:TEXT;
}
@Override
public String getItem(int position) {
if (position==0){
return null;
}
return menusname[position-1];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if (convertView==null){
convertView= LayoutInflater.from(mContext)
.inflate(getItemViewType(position)==IMAGE?R.layout.left_image_item:
R.layout.left_text_item,
parent,false);
holder=new ViewHolder(convertView);
}else {
holder = (ViewHolder) convertView.getTag();
}
if (getItemViewType(position)==TEXT){
holder.bindData(getItem(position));
}else {
if (iconBitmap !=null){
holder.imageView.setImageBitmap(iconBitmap);
}
}
return convertView;
}
class ViewHolder{
ImageView imageView;
TextView textView;
public ViewHolder(View itemView){
imageView=itemView.findViewById(R.id.imageView);
textView=itemView.findViewById(R.id.textView);
itemView.setTag(this);
}
public void bindData(String text){
textView.setText(text);
};
}
}
MovieFragment
package com.bwie.asus.mn3.fragment;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;
import com.bwie.asus.mn3.R;
import com.bwie.asus.mn3.adapter.MovieAdapter;
import com.bwie.asus.mn3.utils.BaseFragment;
public class MovieFragment extends BaseFragment {
private TabLayout movieTab;
private ViewPager movieVP;
@Override
protected void initData() {
}
@Override
protected void initView(View view) {
movieTab = view.findViewById(R.id.movietab);
movieVP = view.findViewById(R.id.movieVP);
movieVP.setAdapter(new MovieAdapter(getChildFragmentManager()));
movieTab.setupWithViewPager(movieVP);
}
@Override
protected int getLayoutResId() {
return R.layout.movie_fragment;
}
}
MovieAdapter
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import com.bwie.asus.mn3.fragment.MovieJJFragment;
import com.bwie.asus.mn3.fragment.MovieZZFragment;
public class MovieAdapter extends FragmentPagerAdapter {
private String[] movienames=new String[]{"即将上映","正在热播"};
public MovieAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new MovieJJFragment();
case 1:
return new MovieZZFragment();
}
return null;
}
@Override
public int getCount() {
return movienames.length;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return movienames[position];
}
}
MovieJJFragment
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import com.bwie.asus.mn3.R;
import com.bwie.asus.mn3.adapter.NewsAdapter;
import com.bwie.asus.mn3.bean.Bunner;
import com.bwie.asus.mn3.bean.NewsList;
import com.bwie.asus.mn3.utils.BaseFragment;
import com.bwie.asus.mn3.utils.NetUtil;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoaderInterface;
import java.util.ArrayList;
import java.util.List;
public class MovieJJFragment extends BaseFragment {
private Banner banner;
private PullToRefreshListView pull;
private int num=1;
private String path="http://www.xieast.com/api/news/news.php?page=";
private NewsAdapter adapter;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
adapter.notifyDataSetChanged();
pull.onRefreshComplete();
}
};
private List<NewsList.DataBean> data=new ArrayList<>();
@Override
protected void initData() {
NetUtil.yibu(
"https://api.tianapi.com/wxnew/?key=c4aa776e0a51d57d6750511e2baa46b6&num=6&page=1",
Bunner.class,
new NetUtil.CallBack<Bunner>() {
@Override
public void getdata(Bunner bunner) {
banner.setImages(bunner.getNewslist());
banner.setBannerTitles(getTitle(bunner));
banner.start();
}
}
);
}
private List<String> getTitle(Bunner ban) {
List<String> list=new ArrayList<>();
for (Bunner.NewslistBean sss:ban.getNewslist()){
list.add(sss.getTitle());
}
return list;
}
@Override
protected void initView(View view) {
banner = view.findViewById(R.id.banner);
banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);
banner.setImageLoader(new ImageLoaderInterface<ImageView>() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Bunner.NewslistBean bean= (Bunner.NewslistBean) path;
ImageLoader.getInstance().displayImage(bean.getPicUrl(),imageView);
}
@Override
public ImageView createImageView(Context context) {
ImageView imageView=new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
return imageView;
}
});
initData();
pull = view.findViewById(R.id.pullList);
pull.setMode(PullToRefreshBase.Mode.BOTH);
adapter = new NewsAdapter(getActivity(), (ArrayList<NewsList.DataBean>) data);
pull.setAdapter(adapter);
pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
num=1;
getData(num);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
num++;
getData(num);
}
});
num=1;
getData(num);
}
private void getData(final int num) {
new Thread(){
@Override
public void run() {
super.run();
NetUtil netUtil=new NetUtil();
String getpath = netUtil.geturl(path+num);
Gson gson=new Gson();
NewsList newsList = gson.fromJson(getpath, NewsList.class);
if (num==1){
data.clear();
}
data.addAll(newsList.getData());
Message message=new Message();
message.what=1;
message.obj= adapter;
handler.sendMessage(message);
}
}.start();
}
@Override
protected int getLayoutResId() {
return R.layout.movie_jj_fragment;
}
}
NewsAdapter
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bwie.asus.mn3.R;
import com.bwie.asus.mn3.bean.NewsList;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
public class NewsAdapter extends BaseAdapter {
private Context context;
private ArrayList<NewsList.DataBean> list;
public NewsAdapter(Context context, ArrayList<NewsList.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if (convertView==null){
holder=new ViewHolder();
convertView = View.inflate(context, R.layout.list_item, null);
holder.neirong=convertView.findViewById(R.id.neirong);
holder.name=convertView.findViewById(R.id.name);
holder.image=convertView.findViewById(R.id.image);
convertView.setTag(holder);
}else {
holder= (ViewHolder) convertView.getTag();
}
holder.name.setText(list.get(position).getTitle());
holder.neirong.setText(list.get(position).getAuthor_name());
DisplayImageOptions options=new DisplayImageOptions.Builder()
.bitmapConfig(Bitmap.Config.RGB_565).cacheOnDisk(true)
.build();
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder.image,options);
return convertView;
}
class ViewHolder{
TextView name,neirong;
ImageView image;
}
}