Android进阶之路
来源:互联网 发布:电脑开机进不了windows 编辑:程序博客网 时间:2024/05/29 07:34
Android内下拉,上拉,再加上列表,这样的搭配几乎没有一个app不用的,在平常开发中下拉最好实现,因为5.0有SwipeRefreshLayout,分分秒秒搞定,但是上拉加载更多,有时候相对麻烦。而我又是一个懒人…所以尽快掌握本文知识,让自己空出一点时间睡觉!hahaha !
本文为博友:”树朾“的切割版,目标是只为满足基本需求,如果说需要切换自己的headLayout与FootLayout或者更详细的使用,可跳转 http://blog.csdn.net/yunyu5120/article/details/74451961
原作者的GIthub地址:
https://github.com/scwang90/SmartRefreshLayout
Effect - 1:
Effect - 2:
Effect - 3 :
bulid:
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3' compile 'com.android.support:recyclerview-v7:24.0.0' //如果头部有需要加载gif的话,需要导入下面的这个依赖 compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.3'
Effect - 1实现方式:
导入成功之后,在Xml中引用“SmartRefreshLayout”,SmartRefreshLayout原作者默认展示的方式就是贝塞尔雷达动画,所以我们直接使用就可以达成我们的Effect - 1效果,具体代码如下。
MainActivity :
package com.bakheet.car.smartrefresh;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.widget.LinearLayoutManager;import android.support.v7.widget.RecyclerView;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import com.scwang.smartrefresh.layout.SmartRefreshLayout;import com.scwang.smartrefresh.layout.api.RefreshLayout;import com.scwang.smartrefresh.layout.listener.OnRefreshListener;import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity { private RecyclerView mRv; private SmartRefreshLayout mRefresh; private LayoutAdapter adapter; private List lists; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //先存入数据 initData(); mRv = (RecyclerView) findViewById(R.id.recyclerview); mRefresh = (SmartRefreshLayout) findViewById(R.id.refreshLayout); mRv.setLayoutManager(new LinearLayoutManager(this)); adapter = new LayoutAdapter(); mRv.setAdapter(adapter); //下拉刷新的监听 mRefresh.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(RefreshLayout refreshlayout) { lists.add(" start "); adapter.notifyDataSetChanged(); mRefresh.finishRefresh(2000); } }); //上拉加载更多的监听 mRefresh.setOnLoadmoreListener(new OnRefreshLoadmoreListener() { @Override public void onLoadmore(RefreshLayout refreshlayout) { lists.add(" end "); adapter.notifyDataSetChanged(); mRefresh.finishLoadmore(2000); } @Override public void onRefresh(RefreshLayout refreshlayout) { } }); } private void initData() { lists = new ArrayList(); for (int i = 0; i < 30; i++) { lists.add("Please go " + i); } } class LayoutAdapter extends RecyclerView.Adapter<LayoutAdapter.myViewholder> { @Override public LayoutAdapter.myViewholder onCreateViewHolder(ViewGroup parent, int viewType) { myViewholder myViewholder = new myViewholder(LayoutInflater.from(MainActivity.this).inflate(R.layout.item_layout, parent, false)); return myViewholder; } @Override public void onBindViewHolder(LayoutAdapter.myViewholder holder, final int position) { holder.mItemData.setText(lists.get(position) + ""); } @Override public int getItemCount() { return lists == null ? 0 : lists.size(); } class myViewholder extends RecyclerView.ViewHolder { public TextView mItemData; public myViewholder(View itemView) { super(itemView); mItemData = (TextView) itemView.findViewById(R.id.item_layout); } } }}
MainActivity Xml:
<?xml version="1.0" encoding="utf-8"?><com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/refreshLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" android:background="#fff" /></com.scwang.smartrefresh.layout.SmartRefreshLayout>
公共的 item_layout 样式:
<?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="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/item_layout" /></LinearLayout>
Effect - 2 实现方式(可自行设置喜欢的背景颜色):
这是我们很常见的一种刷新方式,简单清新;每个App一般都会有自己的Application(如果你没有?那就创建一个,且继承Application),之后在其中加入以下代码:
static {//static 代码段可以防止内存泄露 //设置全局的Header构建器 SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() { @Override public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) { layout.setPrimaryColorsId(R.color.colorAccent, android.R.color.white);//全局设置主题颜色 return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);//指定为经典Header,默认是 贝塞尔雷达Header } }); //设置全局的Footer构建器 SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() { @Override public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) { //指定为经典Footer,默认是 BallPulseFooter return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate); } }); }
如我创建的Appication:
package com.bakheet.car.smartrefresh;import android.app.Application;import android.content.Context;import com.scwang.smartrefresh.layout.SmartRefreshLayout;import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;import com.scwang.smartrefresh.layout.api.RefreshFooter;import com.scwang.smartrefresh.layout.api.RefreshHeader;import com.scwang.smartrefresh.layout.api.RefreshLayout;import com.scwang.smartrefresh.layout.constant.SpinnerStyle;import com.scwang.smartrefresh.layout.footer.ClassicsFooter;import com.scwang.smartrefresh.layout.header.ClassicsHeader;/** * Created by YongLiu on 2017/8/10. */public class App extends Application { static {//static 代码段可以防止内存泄露 //设置全局的Header构建器 SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() { @Override public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) { layout.setPrimaryColorsId(R.color.colorAccent, android.R.color.white);//全局设置主题颜色 return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);//指定为经典Header,默认是 贝塞尔雷达Header } }); //设置全局的Footer构建器 SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() { @Override public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) { //指定为经典Footer,默认是 BallPulseFooter return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate); } }); }}
Effect - 3实现方式:
gif图地址,可以下载一张gif图用于此项效果使用
http://www.xueui.cn/appreciate/motion-design/gif-version-of-tokyo-travel-guide.html
下载好之后扔在mipmap文件夹下,之后在布局中引用,这里我们改变的之后对应的Xml。
加入的Xml:
<pl.droidsonroids.gif.GifImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="centerCrop" android:src="@mipmap/gif_fly" />
MainActivity Xml :
<?xml version="1.0" encoding="utf-8"?><com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/refreshLayout" android:layout_width="match_parent" android:layout_height="match_parent"> <pl.droidsonroids.gif.GifImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="centerCrop" android:src="@mipmap/gif_fly" /> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview" android:layout_width="match_parent" android:layout_height="match_parent" android:overScrollMode="never" android:background="#fff" /></com.scwang.smartrefresh.layout.SmartRefreshLayout>
部分时候我们觉得Head部位与Foot部位太大,可以使用框架内自带的俩个类在xml中实现自己的期望效果,如下代码:
<com.scwang.smartrefresh.layout.SmartRefreshLayout android:id="@+id/sl_refresh_question" android:layout_width="match_parent" android:layout_height="match_parent"> <com.scwang.smartrefresh.layout.header.ClassicsHeader android:layout_width="match_parent" android:layout_height="40dp" android:background="#6666" app:srlTextSizeTime="3sp" app:srlTextSizeTitle="5sp"> </com.scwang.smartrefresh.layout.header.ClassicsHeader> <android.support.v7.widget.RecyclerView android:id="@+id/rv_home_data" android:layout_width="match_parent" android:layout_height="match_parent" /> <com.scwang.smartrefresh.layout.footer.ClassicsFooter android:layout_width="match_parent" android:layout_height="40dp" android:background="#6666" app:srlTextSizeTime="3sp" app:srlTextSizeTitle="5sp"> </com.scwang.smartrefresh.layout.footer.ClassicsFooter> </com.scwang.smartrefresh.layout.SmartRefreshLayout>
- Android进阶之路
- android进阶之路
- Android 进阶之路
- Android 进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- Android进阶之路
- 4:求特殊自然数(程序设计与算法(一)第四周测验(2017夏季))
- 802.11 学习资料汇总
- DEDECMS点击主栏目默认显示第一个子栏目列表的方法
- css解决文字单行两端对齐(兼容ie7)
- oracle中decode()函数的用法
- Android进阶之路
- Linux服务器时间同步
- iOS NSString空白字符替换不掉解决方案
- 状态栏全屏透明
- 用servlet和JSP实现文件上传功能
- JSP内置九大对象和四个作用域
- App 界面设计参考网址
- inner join&left join&right join的区别?
- laytpl模板引擎使用实例2-表格树