主方法
package com.example.rikao_1021;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import com.example.rikao_1021.fragment.faxianfragment;
import com.example.rikao_1021.fragment.shouyeFragment;
import com.example.rikao_1021.fragment.wodefragment;
import com.example.rikao_1021.fragment.xaizaifragment;
public class MainActivity extends AppCompatActivity {
private ImageView imgTitle;
private RelativeLayout relMenu;
private DrawerLayout drawerLayout;
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.mydrawer);
imgTitle = (ImageView) findViewById(R.id.img_title);
radioGroup = (RadioGroup) findViewById(R.id.rel_navigate);
//侧滑菜单的视图
relMenu = (RelativeLayout) findViewById(R.id.rel_menu);
imgTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//关闭,侧滑菜单
drawerLayout.closeDrawer(relMenu);
}
});
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==R.id.rb_shouye){
Log.d("zzz","add index fragment ***********");
addFragment(new shouyeFragment());
}else if(checkedId==R.id.rb_faxian){
addFragment(new faxianfragment());
}else if(checkedId==R.id.rb_xiazai){
addFragment(new xaizaifragment());
}else if(checkedId==R.id.rb_wode){
addFragment(new wodefragment());
}
}
});
//默认添加"首页"
addFragment(new shouyeFragment());
}
public void addFragment(Fragment fragment){
getSupportFragmentManager().beginTransaction().replace(R.id.main_content,fragment).commit();
}
}
viewpager的适配器
package com.example.rikao_1021.Imageloder;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
/**
* author:Created by WangZhiQiang on 2017/10/21.
*/
public class ImagePager extends PagerAdapter {
Context context;
List list1;
public ImagePager(Context context, List<String> list1) {
this.context = context;
this.list1 = list1;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView=new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
ImageLoader.getInstance().displayImage(list1.get(position%list1.size()),imageView,Myadpp.getImageOptions());
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
imageloder类
package com.example.rikao_1021.Imageloder;
import android.app.Application;
import android.graphics.Bitmap;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.io.File;
/**
* author:Created by WangZhiQiang on 2017/10/21.
*/
public class Myadpp extends Application{
@Override
public void onCreate() {
super.onCreate();
File cacheFile=getExternalCacheDir();
ImageLoaderConfiguration config=new ImageLoaderConfiguration.Builder(this)
.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);
}
public static DisplayImageOptions getImageOptions(){
DisplayImageOptions optionsoptions=new DisplayImageOptions.Builder()
.cacheInMemory(true)//使用内存缓存
.cacheOnDisk(true)//使用磁盘缓存
.bitmapConfig(Bitmap.Config.RGB_565)//设置图片格式
.build();
return optionsoptions;
}
}
Fragment类(shouyefragment要用的类)
package com.example.rikao_1021.fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.example.rikao_1021.Bean.bean;
import com.example.rikao_1021.Imageloder.ImagePager;
import com.example.rikao_1021.R;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* author:Created by WangZhiQiang on 2017/10/21.
*/
public class shouye2 extends Fragment implements PullToRefreshListView.OnRefreshListener2 {
private ViewPager vp;
private LinearLayout ll;
private StringBuilder builder;
private Handler myHandler = new Handler();
private List<bean.DataBean> list = new ArrayList<>();
MyAdapter adapter;
int type = 1;
int index = 1;
PullToRefreshListView pullToRefreshListView;
private List<String> list1;
private List<ImageView> images;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
//获取当前正在显示的页面
int sss = vp.getCurrentItem();
vp.setCurrentItem(sss + 1);
//改变小圆点
setSelectedpoint((sss + 1) % list1.size());
//延迟发送消息
sendEmptyMessageDelayed(1, 2000);
}
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.shouye2, null);
vp = (ViewPager) view.findViewById(R.id.vp);
ll = (LinearLayout) view.findViewById(R.id.lin_bottom);
pullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.pull_lv);
list1 = new ArrayList<String>();
list1.add("http://pic8.nipic.com/20100701/5290458_114840036316_2.jpg");
list1.add("http://pic2.nipic.com/20090424/1468853_230119053_2.jpg");
list1.add("http://img3.3lian.com/2013/s1/20/d/57.jpg");
list1.add("http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg");
list1.add("http://a0.att.hudong.com/15/08/300218769736132194086202411_950.jpg");
initdoc();
vp.setAdapter(new ImagePager(getActivity(), list1));
vp.setCurrentItem(list1.size() * 10);
handler.sendEmptyMessageDelayed(1, 2000);
initLv();
init();
return view;
}
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
addtoTop();
myHandler.postDelayed(new Runnable() {
@Override
public void run() {
//刷新完成,必须在异步下完成
pullToRefreshListView.onRefreshComplete();
//刷新适配器
}
}, 1000);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
addtoBottom();
myHandler.postDelayed(new Runnable() {
@Override
public void run() {
//刷新完成,必须在异步下完成
pullToRefreshListView.onRefreshComplete();
//刷新适配器
}
}, 1000);
}
class Mytask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
try {
//获取url
URL url = new URL(strings[0]);
//请求网络
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
int code = urlConnection.getResponseCode();
//判断是否返回成功
if (code == 200) {
//获取网络信息
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
builder = new StringBuilder();
String s = "";
//拼接
while ((s = bf.readLine()) != null) {
builder.append(s);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
bean json = gson.fromJson(s, bean.class);
List<bean.DataBean> list2 = json.getData();
//list.addAll(list1);
if (type == 1) {
list.addAll(0, list2);
} else {
list.addAll(list2);
}
Log.d("zzz", list2.toString());
setAdapter();
}
}
public void setAdapter() {
if (adapter == null) {
adapter = new MyAdapter();
pullToRefreshListView.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
}
public void init() {
Mytask mytask = new Mytask();
mytask.execute("http://api.expoon.com/AppNews/getNewsList/type/1/p/1");
}
public void addtoTop() {
type = 1;
index++;
Mytask mytask = new Mytask();
mytask.execute("http://api.expoon.com/AppNews/getNewsList/type/1/p/" + index);
}
public void addtoBottom() {
type = 2;
index++;
Mytask mytask = new Mytask();
mytask.execute("http://api.expoon.com/AppNews/getNewsList/type/1/p/" + index);
}
public void initLv() {
//设置刷新模式 ,both代表支持上拉和下拉,pull_from_end代表上拉,pull_from_start代表下拉
pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
/*这里通过getLoadingLayoutProxy 方法来指定上拉和下拉时显示的状态的区别,第一个true 代表下来状态 ,第二个true 代表上拉的状态
如果想区分上拉和下拉状态的不同,可以分别设置*/
ILoadingLayout startLabels = pullToRefreshListView.getLoadingLayoutProxy(true, false);
startLabels.setPullLabel("下拉刷新");
startLabels.setRefreshingLabel("正在拉");
startLabels.setReleaseLabel("放开刷新");
ILoadingLayout endLabels = pullToRefreshListView.getLoadingLayoutProxy(false, true);
endLabels.setPullLabel("上拉刷新");
endLabels.setRefreshingLabel("正在载入...");
endLabels.setReleaseLabel("放开刷新...");
pullToRefreshListView.setOnRefreshListener(this);
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Viewholder vh;
if (convertView == null) {
vh = new Viewholder();
convertView = View.inflate(getActivity(), R.layout.item, null);
vh.img = (ImageView) convertView.findViewById(R.id.imageView);
vh.tv = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(vh);
} else {
vh = (Viewholder) convertView.getTag();
}
vh.tv.setText(list.get(position).getNews_title());
ImageLoader.getInstance().displayImage(list.get(position).getPic_url(), vh.img);
return convertView;
}
}
public static class Viewholder {
ImageView img;
TextView tv;
}
public void setSelectedpoint(int sss) {
for (int i = 0; i < list1.size(); i++) {
if (i == sss) {
images.get(i).setImageResource(R.drawable.point_selected);
} else {
images.get(i).setImageResource(R.drawable.point_un_selected);
}
}
}
private void initdoc() {
images = new ArrayList<>();
for (int i = 0; i < list1.size(); i++) {
ImageView imgpoint = new ImageView(getActivity());
imgpoint.setScaleType(ImageView.ScaleType.FIT_XY);
if (i == 0) {
imgpoint.setImageResource(R.drawable.point_selected);
} else {
imgpoint.setImageResource(R.drawable.point_un_selected);
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(40, 40);
params.setMargins(10, 0, 10, 0);
ll.addView(imgpoint, params);
images.add(imgpoint);
}
}
}
shouyeFragment类
package com.example.rikao_1021.fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.rikao_1021.Bean.TabModel;
import com.example.rikao_1021.R;
import java.util.ArrayList;
import java.util.List;
/**
* author:Created by WangZhiQiang on 2017/10/21.
*/
public class shouyeFragment extends Fragment{
private List lists=new ArrayList();
private ViewPager viewPager;
private TabLayout tabLayout;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.shouye, null);
viewPager = (ViewPager) view.findViewById(R.id.vp);
tabLayout = (TabLayout) view.findViewById(R.id.mytab);
//tab标题信息
intTabData();
//设置适配器 ,,得到子fragment的管理者,使用getChildFragmentManager
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
//建立关联
tabLayout.setupWithViewPager(viewPager);
//指定加载的页数 http://blog.csdn.net/qq_29134495/article/details/51548002
viewPager.setOffscreenPageLimit(lists.size());
return view;
}
private void intTabData() {
lists.add(new TabModel("推荐","xbsjxw"));
lists.add(new TabModel("课程","txs"));
lists.add(new TabModel("实战","toutiao"));
lists.add(new TabModel("职业路径","news/mobile/jbgg"));
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
//获取tab导航文本
@Override
public CharSequence getPageTitle(int position) {
return lists.get(position).getTitle();
}
@Override
public Fragment getItem(int position) {
Log.d("zzz","pager adapter ***********"+position);
Bundle bundle=new Bundle();
bundle.putString("dataType",lists.get(position).getType());
bundle.putString("pageIndex","1");
shouye2 s2=new shouye2();
s2.setArguments(bundle);
return s2;
}
@Override
public int getCount() {
return lists.size();
}
}
}
其他的fragment类
package com.example.rikao_1021.fragment;
import android.support.v4.app.Fragment;
/**
* author:Created by WangZhiQiang on 2017/10/21.
*/
public class faxianfragment extends Fragment{
}
其他的fragment类
package com.example.rikao_1021.fragment;
import android.support.v4.app.Fragment;
/**
* author:Created by WangZhiQiang on 2017/10/21.
*/
public class wodefragment extends Fragment{
}
其他的fragment类
package com.example.rikao_1021.fragment;
import android.support.v4.app.Fragment;
/**
* author:Created by WangZhiQiang on 2017/10/21.
*/
public class xaizaifragment extends Fragment{
}
Bean类
package com.example.rikao_1021.Bean;
import java.util.List;
/**
* author:Created by WangZhiQiang on 2017/10/21.
*/
public class bean {
private int status;
private String info;
private List<DataBean> data;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
private String news_id;
private String news_title;
private String news_summary;
private String pic_url;
public String getNews_id() {
return news_id;
}
public void setNews_id(String news_id) {
this.news_id = news_id;
}
public String getNews_title() {
return news_title;
}
public void setNews_title(String news_title) {
this.news_title = news_title;
}
public String getNews_summary() {
return news_summary;
}
public void setNews_summary(String news_summary) {
this.news_summary = news_summary;
}
public String getPic_url() {
return pic_url;
}
public void setPic_url(String pic_url) {
this.pic_url = pic_url;
}
}
}
Bean类(tabModel)
package com.example.rikao_1021.Bean;
/**
* author:Created by WangZhiQiang on 2017/10/21.
*/
public class TabModel {
private String title;
private String type;
public TabModel(String title, String type) {
this.title = title;
this.type = type;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "TabModel{" +
"title='" + title + '\'' +
", type='" + type + '\'' +
'}';
}
}
**主布局**
<?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.rikao_1021.MainActivity">
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mydrawer">
<!--主内容区域-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/rel_navigate"
android:layout_alignParentBottom="true">
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="首页"
android:button="@null"
android:gravity="center"
android:id="@+id/rb_shouye"
android:padding="3dp"
android:background="@drawable/rb_selector"
android:checked="true"/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="发现"
android:button="@null"
android:padding="3dp"
android:gravity="center"
android:id="@+id/rb_faxian"
android:background="@drawable/rb_selector"/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="下载"
android:button="@null"
android:gravity="center"
android:padding="3dp"
android:id="@+id/rb_xiazai"
android:background="@drawable/rb_selector"/>
<RadioButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="我的"
android:padding="3dp"
android:button="@null"
android:gravity="center"
android:id="@+id/rb_wode"
android:background="@drawable/rb_selector"/>
</RadioGroup>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/rel_navigate"
android:id="@+id/main_content"></FrameLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="260dp"
android:layout_height="match_parent"
android:id="@+id/rel_menu"
android:layout_gravity="start"
android:background="#550000ff">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:id="@+id/img_title"
android:layout_marginBottom="50dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是侧边栏"
android:layout_below="@+id/img_title"/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
**shouye的布局**
<?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" >
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="40dp"
app:tabGravity="center"
app:tabIndicatorColor="@color/colorAccent"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/colorPrimaryDark"
app:tabTextColor="@color/colorPrimary"
android:id="@+id/mytab"></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:layout_below="@id/mytab"></android.support.v4.view.ViewPager>
</RelativeLayout>
**shouye2的布局**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/vp"
android:layout_alignParentTop="true"
></android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/lin_bottom"
android:layout_alignBottom="@+id/vp"
android:gravity="center"
android:layout_marginBottom="7dp"
></LinearLayout>
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pull_lv"
android:layout_below="@+id/lin_bottom"
ptr:ptrHeaderTextColor="#FFFFFF"
ptr:ptrHeaderBackground="#383838"
ptr:ptrAnimationStyle="flip"
ptr:ptrDrawable="@drawable/default_ptr_flip"
></com.handmark.pulltorefresh.library.PullToRefreshListView>
</RelativeLayout>
**item布局**
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="28dp"
android:layout_marginTop="13dp"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView"
android:layout_marginStart="38dp"
android:layout_marginTop="12dp"
android:layout_toEndOf="@+id/imageView"
android:text="TextView" />
</RelativeLayout>