}
package com.along.test.monthtest;
import android.content.Context;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.youth.banner.loader.ImageLoader;
/**
* Created by Alongstorm on 2017/10/25,0025.
*/
public class GlideImageLoader extends ImageLoader {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(context).load(path).into(imageView);
}
}
package com.along.test.monthtest;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by Alongstorm on 2017/10/25,0025.
*/
public class LoggingInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request=chain.request();
long t1=System.nanoTime();
Response response=chain.proceed(request);
long t2=System.nanoTime();
return response;
}
}
package com.along.test.monthtest;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import com.along.test.monthtest.adapter.MyShowAdapter;
import com.along.test.monthtest.bean.ShowBean;
import com.along.test.monthtest.model.IShowModel;
import com.along.test.monthtest.presenter.ShowPresenter;
import com.along.test.monthtest.view.IShow;
import java.util.List;
public class ShowActivity extends Activity implements IShow {
List
RecyclerView rv;
ShowPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
rv=findViewById(R.id.rv_show);
Intent intent=getIntent();
String id = intent.getStringExtra("id");
Toast.makeText(this,id,Toast.LENGTH_SHORT).show();
presenter=new ShowPresenter(this,this);
presenter.showView(id);
}
@Override
public void showCard(List
list=lists;
MyShowAdapter adapter=new MyShowAdapter(list,this);
LinearLayoutManager layoutManager=new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rv.setLayoutManager(layoutManager);
rv.setAdapter(adapter);
}
}
package com.along.test.monthtest;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import com.along.test.monthtest.fragmeng.Frag_One;
import com.along.test.monthtest.fragmeng.Frag_Three;
import com.along.test.monthtest.fragmeng.Frag_Two;
import java.util.ArrayList;
import java.util.List;
public class ZhuyeActivity extends FragmentActivity {
ViewPager vp;
List
RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zhuye);
vp= (ViewPager) findViewById(R.id.vp);
rg=findViewById(R.id.rg);
initView();
vp.setAdapter(new MyVPAdapter(getSupportFragmentManager()));
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
rg.check(R.id.rb1);
break;
case 1:
rg.check(R.id.rb2);
break;
case 2:
rg.check(R.id.rb3);
break;
default:
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.rb1:
vp.setCurrentItem(0);
break;
case R.id.rb2:
vp.setCurrentItem(1);
break;
case R.id.rb3:
vp.setCurrentItem(2);
break;
default:
break;
}
}
});
}
private void initView() {
list=new ArrayList<>();
list.add(new Frag_One());
list.add(new Frag_Two());
list.add(new Frag_Three());
}
class MyVPAdapter extends FragmentPagerAdapter{
public MyVPAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
}
}
package com.along.test.monthtest.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.along.test.monthtest.R;
import com.along.test.monthtest.bean.ShowBean;
import java.util.List;
/**
* Created by Alongstorm on 2017/10/25,0025.
*/
public class MyShowAdapter extends RecyclerView.Adapter
//定义三种常量 表示三种条目类型
public static final int TYPE_ONE = 1;
public static final int TYPE_TWO = 2;
List
Context context;
public MyShowAdapter(List
this.data = data;
this.context = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view;
if (viewType==TYPE_ONE){
view=View.inflate(context, R.layout.item_one,null);
return new MyOneViewHolder(view);
}else{
view=View.inflate(context,R.layout.item_two,null);
return new MyTwoViewHolder(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
//int type=getItemViewType(position);
if (holder instanceof MyOneViewHolder){
((MyOneViewHolder) holder).tva.setText(data.get(position).getTitle());
Glide.with(context)
.load(data.get(position).getImages().get(0))
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(((MyOneViewHolder) holder).iva);
}else if (holder instanceof MyTwoViewHolder){
((MyTwoViewHolder) holder).tvb.setText(data.get(position).getTitle());
Glide.with(context)
.load(data.get(position).getImages().get(0))
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(((MyTwoViewHolder) holder).ivb);
}
}
@Override
public int getItemCount() {
return data==null?0:data.size();
}
@Override
public int getItemViewType(int position) {
if (position%2==0){
return TYPE_ONE;
}else{
return TYPE_TWO;
}
}
class MyOneViewHolder extends RecyclerView.ViewHolder{
TextView tva;
ImageView iva;
public MyOneViewHolder(View itemView) {
super(itemView);
tva=itemView.findViewById(R.id.tva);
iva=itemView.findViewById(R.id.iva);
}
}
class MyTwoViewHolder extends RecyclerView.ViewHolder{
TextView tvb;
ImageView ivb;
public MyTwoViewHolder(View itemView) {
super(itemView);
tvb=itemView.findViewById(R.id.tvb);
ivb=itemView.findViewById(R.id.ivb);
}
}
}
package com.along.test.monthtest.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.along.test.monthtest.R;
import com.along.test.monthtest.bean.ZuixinBean;
import java.util.List;
/**
* Created by Alongstorm on 2017/10/25,0025.
*/
public class MyXLVAdapter extends RecyclerView.Adapter
List
Context context;
public MyXLVAdapter(List
this.datas = datas;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=View.inflate(context, R.layout.item,null);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tv.setText(datas.get(position).getTitle());
Glide.with(context)
.load(datas.get(position).getImages().get(0))
.placeholder(R.mipmap.ic_launcher)
.into(holder.iv);
}
@Override
public int getItemCount() {
if (datas==null){
return 0;
}else{
return datas.size();
}
//return bean==null?0:bean.getStories().size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView tv;
ImageView iv;
public MyViewHolder(View itemView) {
super(itemView);
tv=itemView.findViewById(R.id.tv_item);
iv=itemView.findViewById(R.id.iv_item);
}
}
}
package com.along.test.monthtest.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.along.test.monthtest.R;
import com.along.test.monthtest.bean.ZtrbBean;
import com.along.test.monthtest.myview.TuoYuan;
import java.util.List;
/**
* Created by Alongstorm on 2017/10/25,0025.
*/
public class MyZRVAdapter extends RecyclerView.Adapter
Context context;
List
public MyZRVAdapter(Context context, List
this.context = context;
this.datas = datas;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=View.inflate(context, R.layout.item_grid,null);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
holder.tvv.setText(datas.get(position).getName());
Glide.with(context)
.load(datas.get(position).getThumbnail())
.placeholder(R.mipmap.ic_launcher)
.into(holder.ivv);
//为RecyclerView设置单机条目的事件 长按事件忽略了
if(mOnItemClickLitener!=null){
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos=holder.getLayoutPosition();
mOnItemClickLitener.OnitemClick(holder.itemView,pos);
}
});
}
}
@Override
public int getItemCount() {
return datas==null?0:datas.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView tvv;
ImageView ivv;
//TuoYuan ivv;
public MyViewHolder(View itemView) {
super(itemView);
tvv=itemView.findViewById(R.id.tvv);
ivv=itemView.findViewById(R.id.ivv);
}
}
public interface OnItemClickListener{
void OnitemClick(View view,int position);
}
private OnItemClickListener mOnItemClickLitener;
public void setOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
this.mOnItemClickLitener = mOnItemClickLitener;
}
}
package com.along.test.monthtest.fragerji;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.along.test.monthtest.GlideImageLoader;
import com.along.test.monthtest.adapter.MyXLVAdapter;
import com.along.test.monthtest.R;
import com.along.test.monthtest.bean.ZuixinBean;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.youth.banner.Banner;
import com.youth.banner.Transformer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by Alongstorm on 2017/10/25,0025.
*/
public class Frag_zuixin extends Fragment {
OkHttpClient okHttpClient;
int p=1;
XRecyclerView xrv;
MyXLVAdapter adapter;
Banner mBanner;
List
ZuixinBean bean;
List
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=View.inflate(getActivity(), R.layout.zuixin,null);
xrv=view.findViewById(R.id.xrv);
initData();
showView();
xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable(){
public void run() {
datas.clear();
initDataMore(p);
adapter.notifyDataSetChanged();
xrv.refreshComplete();
p++;
}
}, 1000);
}
@Override
public void onLoadMore() {
new Handler().postDelayed(new Runnable(){
public void run() {
initDataMore(p);
adapter.notifyDataSetChanged();
xrv.loadMoreComplete();
p++;
}
}, 1000);
}
});
return view;
}
private void initDataMore(int path){
okHttpClient= new OkHttpClient.Builder().build();
final Request request=new Request.Builder()
.url("http://news-at.zhihu.com/api/4/news/before/2013111"+path).addHeader("User-Agent","").build();
Call call=okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String res=response.body().string();
Log.i("---", "onResponse: "+res);
Gson gson=new Gson();
bean = gson.fromJson(res, ZuixinBean.class);
datas.addAll(bean.getStories());
Log.i("++++", "onResponse: "+bean.toString());
}
});
}
private void initData() {
okHttpClient= new OkHttpClient.Builder().build();
//使用拦截器
//okHttpClient.interceptors().add(new LoggingInterceptor());
final Request request=new Request.Builder()
.url("http://news-at.zhihu.com/api/4/news/latest").addHeader("User-Agent","").build();
Call call=okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String res=response.body().string();
Log.i("+++", "onResponse: "+res);
Gson gson=new Gson();
bean = gson.fromJson(res, ZuixinBean.class);
for (int i = 0; i < bean.getTop_stories().size(); i++) {
listImg.add(bean.getTop_stories().get(i).getImage());
}
datas.addAll(bean.getStories());
Log.i("++++", "onResponse: "+bean.toString());
}
});
}
private void showView() {
LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
xrv.setLayoutManager(layoutManager);
xrv.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
xrv.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
xrv.setArrowImageView(R.mipmap.iconfont_downgrey);
adapter=new MyXLVAdapter(datas,getActivity());
Log.i("+++++", "showView: "+datas.toString());
xrv.setAdapter(adapter);
xrv.setLoadingMoreEnabled(true);
xrv.setPullRefreshEnabled(true);
View head=View.inflate(getActivity(),R.layout.recyclerview_header,null);
mBanner=head.findViewById(R.id.banner);
mBanner.setImages(listImg)
.setDelayTime(1000)
.setBannerAnimation(Transformer.Tablet)//设置轮播动画
.setImageLoader(new GlideImageLoader())
.start();
xrv.addHeaderView(head);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
getActivity().getMenuInflater().inflate(R.menu.menu_main,menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
package com.along.test.monthtest.fragerji;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.along.test.monthtest.adapter.MyZRVAdapter;
import com.along.test.monthtest.R;
import com.along.test.monthtest.ShowActivity;
import com.along.test.monthtest.bean.ZtrbBean;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by Alongstorm on 2017/10/25,0025.
*/
public class Frag_ztrb extends Fragment {
RecyclerView rcv;
OkHttpClient okHttpClient;
ZtrbBean bean;
MyZRVAdapter ad;
List
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=View.inflate(getActivity(), R.layout.zutiribao,null);
rcv=view.findViewById(R.id.rcv);
initData();
Log.i("+++4", "showView: "+datas.size());
// showView();
return view;
}
private void showView() {
Log.i("+++3", "showView: "+datas.size());
rcv.setLayoutManager(new GridLayoutManager(getActivity(),2));
ad=new MyZRVAdapter(getActivity(),datas);
ad.notifyDataSetChanged();
rcv.setAdapter(ad);
}
private void initData() {
datas=new ArrayList<>();
okHttpClient= new OkHttpClient.Builder().build();
final Request request=new Request.Builder()
.url("http://news-at.zhihu.com/api/4/themes").addHeader("User-Agent","").build();
Call call=okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String res=response.body().string();
Log.i("---2", "onResponse: "+res);
Gson gson=new Gson();
bean = gson.fromJson(res, ZtrbBean.class);
datas.addAll(bean.getOthers());
Log.i("++++2", "onResponse: "+bean.toString());
Log.i("+++2", "onResponse: "+datas.size());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
showView();
ad.setOnItemClickLitener(new MyZRVAdapter.OnItemClickListener() {
@Override
public void OnitemClick(View view, int position) {
//Toast.makeText(getActivity(),datas.get(position).getId()+"",Toast.LENGTH_SHORT).show();
Intent in=new Intent(getActivity(), ShowActivity.class);
in.putExtra("id",datas.get(position).getId()+"");
startActivity(in);
}
});
}
});
//showView();
}
});
}
}
package com.along.test.monthtest.model;
import com.along.test.monthtest.util.LoadDataUtils;
/**
* Created by Alongstorm on 2017/10/25,0025.
*/
public class ShowModel implements IShowModel {
@Override
public String loadData(String path) {
String res= LoadDataUtils.getData(path);
return res;
}
}
package com.along.test.monthtest.myview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by Alongstorm on 2017/10/25,0025.
*/
public class Yuan extends View {
private Paint mPaint;
public Yuan(Context context) {
super(context);
init();
}
public Yuan(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Yuan(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
mPaint = new Paint();
mPaint.setAntiAlias(true); //抗锯齿
mPaint.setColor(Color.BLUE);//画笔颜色
mPaint.setStyle(Paint.Style.FILL); //画笔风格
mPaint.setTextSize(36); //绘制文字大小,单位px
mPaint.setStrokeWidth(5); //画笔粗细
}
//重写该方法,在这里绘图
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(100, 100, 50, mPaint);
}
}
package com.along.test.monthtest.presenter;
import android.content.Context;
import com.along.test.monthtest.bean.ShowBean;
import com.along.test.monthtest.model.ShowModel;
import com.along.test.monthtest.util.OkHttpUtils;
import com.along.test.monthtest.util.OnUiCallback;
import com.along.test.monthtest.view.IShow;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* Created by Alongstorm on 2017/10/25,0025.
*/
public class ShowPresenter implements IShowPresenter {
Context context;
IShow view;
ShowModel model;
List
public ShowPresenter(IShow view, Context context) {
this.view = view;
this.context = context;
model=new ShowModel();
}
@Override
public void showView(String id) {
OkHttpUtils.getInstance().doGet("http://news-at.zhihu.com/api/4/theme/"+id, new OnUiCallback() {
@Override
public void onFailed(Call call, IOException e) {
}
@Override
public void onSuccess(String result) throws IOException {
String res=result;
Gson gson=new Gson();
ShowBean showBean = gson.fromJson(res, ShowBean.class);
list.addAll(showBean.getStories());
view.showCard(list);
}
});
}
}
package com.along.test.monthtest.util;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 姓名:McoWu
* 时间:2017/9/27 16:19.
* 本类作用:根据接口请求数据
*/
public class LoadDataUtils {
public static String getData(String path) {
String json="";
URL url = null;
try {
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
if (code == 200) {
InputStream isr = conn.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int len=-1;
while((len=isr.read(buff))!=-1){
bos.write(buff,0,len);
}
json= new String(bos.toByteArray());
}
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
}
package com.along.test.monthtest.util;
import android.os.Handler;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
/**
* Created by Alongstorm on 2017/10/13,0013.
*/
public class OkHttpUtils {
private Handler handler = new Handler();
public Handler getHandler(){
return handler;
}
//单例
private static OkHttpUtils okHttpUtils = new OkHttpUtils();
private OkHttpUtils(){};
public static OkHttpUtils getInstance(){
return okHttpUtils;
}
private OkHttpClient client;
private void initOkHttpClient(){
if(client == null){
client = new OkHttpClient.Builder().build();
}
}
//公用的get请求方法 完成的功能不确定
public void doGet(String url, Callback callback){
initOkHttpClient();
Request request = new Request.Builder().url(url).build();
Call call = client.newCall(request);
call.enqueue(callback);
}
}
package com.along.test.monthtest.util;
import android.os.Handler;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* Created by yfeng on 2017/10/12.
*/
public abstract class OnUiCallback implements Callback{
private Handler handler = OkHttpUtils.getInstance().getHandler();
public abstract void onFailed(Call call, IOException e);
public abstract void onSuccess(String result) throws IOException;
@Override
public void onFailure(final Call call, final IOException e) {
//该方式 存在问题 网络请求也跑到了主线程 待解决
//该方法就是把 线程post到handler所在的线程
handler.post(new Runnable() {
@Override
public void run() {
onFailed(call, e);
}
});
}
@Override
public void onResponse(final Call call, final Response response) throws IOException {
final String result = response.body().string();
//该方式 存在问题 网络请求也跑到了主线程 待解决
handler.post(new Runnable() {
@Override
public void run() {
try {
onSuccess(result);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
package com.along.test.monthtest.fragmeng;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.along.test.monthtest.R;
import com.along.test.monthtest.fragerji.Frag_remen;
import com.along.test.monthtest.fragerji.Frag_zhuanlan;
import com.along.test.monthtest.fragerji.Frag_ztrb;
import com.along.test.monthtest.fragerji.Frag_zuixin;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Alongstorm on 2017/10/25,0025.
*/
public class Frag_One extends Fragment {
TabLayout tab;
ViewPager vp;
List
List
List
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=View.inflate(getActivity(), R.layout.frag_one,null);
tab=view.findViewById(R.id.tab);
vp=view.findViewById(R.id.vp_main);
initTab();
initVP();
vp.setAdapter(new MyAdapter(getActivity().getSupportFragmentManager()));
tab.setupWithViewPager(vp);
return view;
}
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return list.get(position);
}
@Override
public int getCount() {
return list.size();
}
@Override
public CharSequence getPageTitle(int position) {
return listStr.get(position);
}
}
private void initVP() {
list=new ArrayList<>();
list.add(new Frag_zuixin());
list.add(new Frag_zhuanlan());
list.add(new Frag_remen());
list.add(new Frag_ztrb());
}
private void initTab() {
listStr=new ArrayList<>();
listTv=new ArrayList<>();
listStr.add("最新日报");
listStr.add("专栏");
listStr.add("热门");
listStr.add("主题日报");
for (int i = 0; i < listStr.size(); i++) {
TextView tv=new TextView(getActivity());
tv.setText(listStr.get(i));
listTv.add(tv);
}
tab.setTabMode(TabLayout.MODE_SCROLLABLE);
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:design:26.0.0-alpha1'
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.okio:okio:1.13.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.youth.banner:banner:1.4.9'
compile 'com.android.support:cardview-v7:26.0.0-alpha1'
}