dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.youth.banner:banner:1.4.9'
compile files('libs/glide-3.7.0.jar')
compile files('libs/gson-2.2.4.jar')
compile files('libs/okhttp-3.9.0.jar')
compile files('libs/okio-1.13.0.jar')
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
compile 'com.android.support:design:26.0.0-alpha1'
public class MainActivity extends AppCompatActivity {
private MyRadius radius;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showRadius();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
}, 3000);
}
private void showRadius() {
radius = (MyRadius) findViewById(R.id.myradius);
// ValueAnimator animator = ValueAnimator.ofInt(0,500);
// animator.setDuration(3000);
//
// animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
// @Override
// public void onAnimationUpdate(ValueAnimator animation) {
// int curValue = (int)animation.getAnimatedValue();
// radius.layout(curValue,curValue,curValue+radius.getWidth(),curValue+radius.getHeight());
// }
// });
// animator.start();
//radius.setVisibility(View.VISIBLE);
int endX = radius.getMeasuredWidth() - getResources().getDisplayMetrics().widthPixels;
//设置X轴平移量
int endY = radius.getMeasuredHeight() * 2 - getResources().getDisplayMetrics().heightPixels;
//设置Y轴平移量
PropertyValuesHolder transXHolder = PropertyValuesHolder.ofFloat("translationX", 0, -endX);
PropertyValuesHolder transYHolder = PropertyValuesHolder.ofFloat("translationY", 0, -endY);
// PropertyValuesHolder scaleXHolder=PropertyValuesHolder.ofFloat("scaleX",1,0);
// PropertyValuesHolder scaleYHolder=PropertyValuesHolder.ofFloat("scaleY",1,0);
ObjectAnimator objectAnimator=ObjectAnimator.ofPropertyValuesHolder(radius,transXHolder,transYHolder);
objectAnimator.setDuration(3000);
objectAnimator.start();//播放完后,图片会回到原来的位置
}
}
public class Main2Activity extends AppCompatActivity {
private ViewPager vp;
private List list_frag;
private MyFTAdapter myFTAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initFragment();
}
private void initFragment() {
vp = (ViewPager) findViewById(R.id.vp);
list_frag = new ArrayList<>();
list_frag.add(new Fragment_One());
list_frag.add(new Fragment_Two());
list_frag.add(new Fragment_Three());
myFTAdapter = new MyFTAdapter(getSupportFragmentManager(), list_frag);
vp.setAdapter(myFTAdapter);
}
}
public class MyRadius extends View {
private Paint paint;
public MyRadius(Context context) {
super(context);
init();
}
public MyRadius(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public MyRadius(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void init(){
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(1);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(20, 20, 20, paint);
}
}
/**
* Created by Administrator on 2017/10/13.
*/
public class OkHttp {
/**
* 静态实例
*/
private static OkHttp sOkHttpManager;
/**
* okhttpclient实例
*/
private OkHttpClient mClient;
/**
* 因为我们请求数据一般都是子线程中请求,在这里我们使用了handler
*/
private Handler mHandler;
/**
* 构造方法
*/
private OkHttp() {
mClient = new OkHttpClient();
/**
* 在这里直接设置连接超时.读取超时,写入超时
*/
mClient.newBuilder().connectTimeout(10, TimeUnit.SECONDS);
mClient.newBuilder().readTimeout(10, TimeUnit.SECONDS);
mClient.newBuilder().writeTimeout(10, TimeUnit.SECONDS);
/**
* 如果是用的3.0之前的版本 使用以下直接设置连接超时.读取超时,写入超时
*/
//client.setConnectTimeout(10, TimeUnit.SECONDS);
//client.setWriteTimeout(10, TimeUnit.SECONDS);
//client.setReadTimeout(30, TimeUnit.SECONDS);
/**
* 初始化handler
*/
mHandler = new Handler(Looper.getMainLooper());
}
/**
* 单例模式 获取OkHttp实例
*
* @return
*/
public static OkHttp getInstance() {
if (sOkHttpManager == null) {
sOkHttpManager = new OkHttp();
}
return sOkHttpManager;
}
//-------------------------同步的方式请求数据--------------------------
/**
* 对外提供的get方法,同步的方式
*
* @param url 传入的地址
* @return
*/
public static Response getSync(String url) {
//通过获取到的实例来调用内部方法
return sOkHttpManager.inner_getSync(url);
}
/**
* GET方式请求的内部逻辑处理方式,同步的方式
*
* @param url
* @return
*/
private Response inner_getSync(String url) {
Request request = new Request.Builder().url(url).build();
Response response = null;
try {
//同步请求返回的是response对象
response = mClient.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
/**
* 对外提供的同步获取String的方法
*
* @param url
* @return
*/
public static String getSyncString(String url) {
return sOkHttpManager.inner_getSyncString(url);
}
/**
* 同步方法
*/
private String inner_getSyncString(String url) {
String result = null;
try {
/**
* 把取得到的结果转为字符串,这里最好用string()
*/
result = inner_getSync(url).body().string();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
//-------------------------异步的方式请求数据--------------------------
public static void getAsync(String url, DataCallBack callBack) {
getInstance().inner_getAsync(url, callBack);
}
/**
* 内部逻辑请求的方法
*
* @param url
* @param callBack
* @return
*/
private void inner_getAsync(String url, final DataCallBack callBack) {
final Request request = new Request.Builder().url(url).build();
mClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
deliverDataFailure(request, e, callBack);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String result = null;
try {
result = response.body().string();
} catch (IOException e) {
deliverDataFailure(request, e, callBack);
}
deliverDataSuccess(result, callBack);
}
});
}
/**
* 分发失败的时候调用
*
* @param request
* @param e
* @param callBack
*/
private void deliverDataFailure(final Request request, final IOException e, final DataCallBack callBack) {
/**
* 在这里使用异步处理
*/
mHandler.post(new Runnable() {
@Override
public void run() {
if (callBack != null) {
callBack.requestFailure(request, e);
}
}
});
}
/**
* 分发成功的时候调用
*
* @param result
* @param callBack
*/
private void deliverDataSuccess(final String result, final DataCallBack callBack) {
/**
* 在这里使用异步线程处理
*/
mHandler.post(new Runnable() {
@Override
public void run() {
if (callBack != null) {
try {
callBack.requestSuccess(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
/**
* 数据回调接口
*/
public interface DataCallBack {
void requestFailure(Request request, IOException e);
void requestSuccess(String result) throws Exception;
}
//-------------------------提交表单--------------------------
public static void postAsync(String url, Map params, DataCallBack callBack) {
getInstance().inner_postAsync(url, params, callBack);
}
private void inner_postAsync(String url, Map params, final DataCallBack callBack) {
RequestBody requestBody = null;
if (params == null) {
params = new HashMap<>();
}
/**
* 如果是3.0之前版本的,构建表单数据是下面的一句
*/
//FormEncodingBuilder builder = new FormEncodingBuilder();
/**
* 3.0之后版本
*/
FormBody.Builder builder = new FormBody.Builder();
/**
* 在这对添加的参数进行遍历,map遍历有四种方式,如果想要了解的可以网上查找
*/
for (Map.Entry map : params.entrySet()) {
String key = map.getKey().toString();
String value = null;
/**
* 判断值是否是空的
*/
if (map.getValue() == null) {
value = "";
} else {
value = map.getValue();
}
/**
* 把key和value添加到formbody中
*/
builder.add(key, value);
}
requestBody = builder.build();
//结果返回
// 请求对象
final Request request = new Request.Builder().url(url).post(requestBody).build();
mClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
deliverDataFailure(request, e, callBack);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().string();
deliverDataSuccess(result, callBack);
}
});
}
//-------------------------文件下载--------------------------
public static void downloadAsync(String url, String desDir, DataCallBack callBack) {
getInstance().inner_downloadAsync(url, desDir, callBack);
}
/**
* 下载文件的内部逻辑处理类
*
* @param url 下载地址
* @param desDir 目标地址
* @param callBack
*/
private void inner_downloadAsync(final String url, final String desDir, final DataCallBack callBack) {
final Request request = new Request.Builder().url(url).build();
mClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
deliverDataFailure(request, e, callBack);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
/**
* 在这里进行文件的下载处理
*/
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
//文件名和目标地址
File file = new File(desDir, getFileName(url));
//把请求回来的response对象装换为字节流
inputStream = response.body().byteStream();
fileOutputStream = new FileOutputStream(file);
int len = 0;
byte[] bytes = new byte[2048];
//循环读取数据
while ((len = inputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, len);
}
//关闭文件输出流
fileOutputStream.flush();
//调用分发数据成功的方法
deliverDataSuccess(file.getAbsolutePath(), callBack);
} catch (IOException e) {
//如果失败,调用此方法
deliverDataFailure(request, e, callBack);
e.printStackTrace();
} finally {
if (inputStream != null) {
inputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
}
}
});
}
/**
* 根据文件url获取文件的路径名字
*
* @param url
* @return
*/
private String getFileName(String url) {
int separatorIndex = url.lastIndexOf("/");
String path = (separatorIndex < 0) ? url : url.substring(separatorIndex + 1, url.length());
return path;
}
}
public class Fragment_New extends Fragment {
private View view;
private Banner banner;
RbDatas rbDatas;
private XRecyclerView xrv;
private List list_banner;
private MyRvAdapter myRvAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag_new, null);
showBanner();
showXrv();
return view;
}
private void showXrv() {
xrv = view.findViewById(R.id.xrv);
myRvAdapter = new MyRvAdapter(getActivity(), rbDatas);
xrv.setAdapter(myRvAdapter);
}
private void showBanner() {
banner = view.findViewById(R.id.banner);
OkHttp.getAsync("http://news-at.zhihu.com/api/4/news/latest", new OkHttp.DataCallBack() {
@Override
public void requestFailure(Request request, IOException e) {
}
@Override
public void requestSuccess(String result) throws Exception {
rbDatas = RbDatas.objectFromData(result);
}
});
list_banner = new ArrayList();
// for (int i = 0; i < rbDatas.top_stories.size(); i++) {
// list_banner.add(rbDatas.top_stories.get(i).image);
// }
list_banner.add("https://pic3.zhimg.com/v2-20786b64172211a00c6e611b1bfbb2b6.jpg");
list_banner.add("https://pic4.zhimg.com/v2-97d9c4d8c3c673b10772682e5ac0c137.jpg");
list_banner.add("https://pic2.zhimg.com/v2-e7582788c34b9d40b7b849ea3458d0dd.jpg");
list_banner.add("https://pic1.zhimg.com/v2-e5b5e2342378517d1ddeb3f26496367c.jpg");
list_banner.add("https://pic2.zhimg.com/v2-2f8827e1dd120aecea73713fd27f67d1.jpg");
banner.setImages(list_banner);
banner.setImageLoader(new ImageLoader() {
@Override
public void displayImage(Context context, Object path, ImageView imageView) {
Glide.with(context).load(path).into(imageView);
}
});
banner.start();
}
}
public class Fragment_One extends Fragment {
private View view;
private TabLayout tabLayout;//定义tablayout控件
private ViewPager vp_tab;//定义ViewPager
private List list_frag_tab;//定义存放Fragment的集合
private List list_title;//tab名称列表
private MyTabAdapter myTabAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frag_one, null);
//加载tablayout
initControls(view);
return view;
}
private void initControls(View view) {
tabLayout = (TabLayout)view.findViewById(R.id.tablayout);
vp_tab = (ViewPager)view.findViewById(R.id.vp_tab);
//添加Fragment给tablayout
list_frag_tab = new ArrayList<>();
list_frag_tab.add(new Fragment_New());
list_frag_tab.add(new Fragment_ZL());
list_frag_tab.add(new Fragment_Hot());
list_frag_tab.add(new Fragment_ZT());
//给tablayout添加名字
list_title = new ArrayList<>();
list_title.add("最新日报");
list_title.add("专栏");
list_title.add("热门");
list_title.add("主题日报");
//给tablayout设置模式
//给tablayout设置标题
for (int i = 0; i < list_title.size(); i++) {
tabLayout.addTab(tabLayout.newTab().setText(list_title.get(i)));
}
myTabAdapter = new MyTabAdapter(getFragmentManager(), list_frag_tab, list_title);
vp_tab.setAdapter(myTabAdapter);
}
}
public class MyFTAdapter extends FragmentPagerAdapter {
private FragmentPagerAdapter fm;
private List list_frag;
public MyFTAdapter(FragmentManager fm, List list_frag) {
super(fm);
this.list_frag = list_frag;
}
@Override
public Fragment getItem(int position) {
return list_frag.get(position);
}
@Override
public int getCount() {
return list_frag.size();
}
}
public class MyRvAdapter extends XRecyclerView.Adapter {
private Context context;
private RbDatas rbDatas;
public MyRvAdapter(Context context, RbDatas rbDatas) {
this.context = context;
this.rbDatas = rbDatas;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Glide.with(context).load(rbDatas.stories.get(position).images.get(0)).into(holder.iv_item);
holder.tv_item.setText(rbDatas.stories.get(position).title);
}
@Override
public int getItemCount() {
return rbDatas.stories.size();
}
static class MyViewHolder extends XRecyclerView.ViewHolder{
ImageView iv_item;
TextView tv_item;
public MyViewHolder(View itemView) {
super(itemView);
iv_item = itemView.findViewById(R.id.iv_item);
tv_item = itemView.findViewById(R.id.tv_item);
}
}
}
public class MyTabAdapter extends FragmentPagerAdapter {
private List list_frag_tab;
private List list_title;
public MyTabAdapter(FragmentManager fm, List list_frag_tab, List list_title) {
super(fm);
this.list_frag_tab = list_frag_tab;
this.list_title = list_title;
}
@Override
public Fragment getItem(int position) {
return list_frag_tab.get(position);
}
@Override
public int getCount() {
return list_frag_tab.size();
}
//联动后通过ViewPager适配器里面的getpagetitle方法给tablayout设置标题
@Override
public CharSequence getPageTitle(int position) {
return list_title.get(position);
}
}