在本文中,我们将给您介绍关于Android–在SwipeRefreshLayout加载时单击RecyclerView时崩溃的详细内容,并且为您解答swiperefreshlayout加载更多的相关问题
在本文中,我们将给您介绍关于Android – 在SwipeRefreshLayout加载时单击RecyclerView时崩溃的详细内容,并且为您解答swiperefreshlayout加载更多的相关问题,此外,我们还将为您提供关于032 Android智能下拉刷新框架-SmartRefreshLayout+RecyclerView的使用、Android FlowLayout作为RecyclerView LayoutManager、Android MotionLayout + Recyclerview,recyclerview中的视图不可单击、Android SwipeRefreshLayout的知识。
本文目录一览:- Android – 在SwipeRefreshLayout加载时单击RecyclerView时崩溃(swiperefreshlayout加载更多)
- 032 Android智能下拉刷新框架-SmartRefreshLayout+RecyclerView的使用
- Android FlowLayout作为RecyclerView LayoutManager
- Android MotionLayout + Recyclerview,recyclerview中的视图不可单击
- Android SwipeRefreshLayout
Android – 在SwipeRefreshLayout加载时单击RecyclerView时崩溃(swiperefreshlayout加载更多)
我在SwipeRefreshLayout中有一个RecyclerView,当我重新加载我的页面时,当加载SwipeRefreshLayout时,我点击RecyclerView上的项目它就崩溃了.
04-21 13:14:49.605 25586-25586/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.Boxopen.funstack, PID: 25586
java.lang.indexoutofboundsexception: Invalid index 0, size is 0
at java.util.ArrayList.throwindexoutofboundsexception(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.Boxopen.funstack.adapter.RecycleBookAdapter$BookViewHolder.onClickLike(RecycleBookAdapter.java:339)
at com.Boxopen.funstack.adapter.RecycleBookAdapter$BookViewHolder.access$100(RecycleBookAdapter.java:64)
at com.Boxopen.funstack.adapter.RecycleBookAdapter$BookViewHolder$2.ondoubleclick(RecycleBookAdapter.java:122)
at com.Boxopen.funstack.listener.DoubleClickListener.onClick(DoubleClickListener.java:36)
at android.view.View.performClick(View.java:4478)
at android.view.View$PerformClick.run(View.java:18698)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:149)
at android.app.ActivityThread.main(ActivityThread.java:5268)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
04-21 13:14:49.675 182-1124 /? E / IMGSRV :: 0:PVRDRMOpen:TP3,ret = 57
解决方法:
根据我的理解你的问题和日志基本上是你清除onRefresh()中的arraylist但不通知适配器列表已被清除.
清除arraylist后请调用notifyDataSetChanged()
032 Android智能下拉刷新框架-SmartRefreshLayout+RecyclerView的使用
1.SmartRefreshLayout介绍
SmartRefreshLayout的目标是打造一个强大,稳定,成熟的下拉刷新框架,并集成各种的炫酷、多样、实用、美观的Header和Footer。 正如名字所说,SmartRefreshLayout是一个“聪明”或者“智能”的下拉刷新布局,由于它的“智能”,它不只是支持所有的View,还支持多层嵌套的视图结构。 它继承自ViewGroup 而不是FrameLayout或LinearLayout,提高了性能。 也吸取了现在流行的各种刷新布局的优点,包括谷歌官方的 SwipeRefreshLayout, 其他第三方的 Ultra-Pull-To-Refresh、TwinklingRefreshLayout 。 还集成了各种炫酷的 Header 和 Footer。
2.使用步骤
(1)在 build.gradle (app)中添加依赖
implementation ''com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-26''
implementation ''com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-26''//没有使用特殊Header,可以不加这行
implementation ''com.android.support:design:28.0.0''
(2)布局文件(使用SmartRefreshLayout和RecyclerView)
<?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/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
android:scrollbars="vertical"
android:background="#fff" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
(3)RecyclerView的item布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:textSize="16sp"
android:textStyle="bold"
android:text="hehe"/>
</RelativeLayout>
(4)java后台
package com.example.administrator.test65smartrefreshlayout;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.listener.SimpleMultiPurposeListener;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView mRecyclerView;
MyAdapter mAdapter;
LinearLayoutManager mLayoutManager;
RefreshLayout refreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mRecyclerView = findViewById(R.id.my_recycler_view);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
mRecyclerView.setHasFixedSize(true);
mAdapter = new MyAdapter(getDatas());
mRecyclerView.setAdapter(mAdapter);
}
private void initView() {
refreshLayout = findViewById(R.id.refreshLayout);
/**
* 设置不同的头部、底部样式
*/
// refreshLayout.setRefreshFooter(new ClassicsFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
// refreshLayout.setRefreshHeader(new BezierRadarHeader(this));
// refreshLayout.setRefreshHeader(new TwoLevelHeader(this));
refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
refreshLayout.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
//设置样式后面的背景颜色
refreshLayout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
//设置监听器,包括顶部下拉刷新、底部上滑刷新
refreshLayout.setOnMultiPurposeListener(new SimpleMultiPurposeListener(){
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
mAdapter.refreshData(MoreDatas()); //下拉刷新,数据从上往下添加到界面上
refreshLayout.finishRefresh(1000); //这个记得设置,否则一直转圈
}
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
mAdapter.loadMore(MoreDatas()); //上滑刷新,数据从下往上添加到界面上
refreshLayout.finishLoadMore(1000); //这个记得设置,否则一直转圈
}
});
}
//原始的recyclerView数据
private ArrayList<String> getDatas() {
ArrayList<String> data = new ArrayList<>();
String temp = " item";
for(int i = 0; i < 15; i++) {
data.add(i + temp);
}
return data;
}
//刷新得到的数据
private ArrayList<String> MoreDatas() {
ArrayList<String> data = new ArrayList<>();
String temp = "新加数据 ";
for(int i = 0; i < 6; i++) {
data.add(temp + i);
}
return data;
}
public static class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
public ArrayList<String> datas = null;
public MyAdapter(ArrayList<String> datas) {
this.datas = datas;
}
//创建新View,被LayoutManager所调用
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_smartrefreshlayout_item,viewGroup,false);
ViewHolder vh = new ViewHolder(view);
return vh;
}
//将数据与界面进行绑定的操作
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.mTextView.setText(datas.get(position));
}
//获取数据的数量
@Override
public int getItemCount() {
return datas.size();
}
//底部上拉刷新,数据直接在底部显示
public void loadMore(ArrayList<String> strings) {
datas.addAll(strings);
notifyDataSetChanged();
}
//底部下拉刷新,数据直接从上往下添加数据,显示在顶部
public void refreshData(ArrayList<String> strings) {
datas.addAll(0, strings);
notifyDataSetChanged();
// notifyItemInserted(0); 一次只能加一项数据
}
//自定义的ViewHolder,持有每个Item的的所有界面元素
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(View view){
super(view);
mTextView = view.findViewById(R.id.text);
}
}
}
}
3.效果图
Android FlowLayout作为RecyclerView LayoutManager
但我认为最好的方法是使用RecycleView和自定义LayoutManager.
我搜索一个LayoutManager,它将其子项布局为FlowLayout,但什么都没找到.
是否有人发现了这种行为或关于自定义布局管理器的简单教程?我发现没有简单或简单但不完整的文章.
.
解决方法
您已将此行添加到Gradle依赖项中:
compile 'com.xiaofeng.android:flowlayoutmanager:1.2.3.2'
并使用它设置您的Recycler视图布局管理器:
recyclerView.setLayoutManager(new FlowLayoutManager());
您有关于如何在Github上配置它的其他详细信息:https://github.com/xiaofeng-han/AndroidLibs/tree/master/flowlayoutmanager
Android MotionLayout + Recyclerview,recyclerview中的视图不可单击
好的,我已经解决了这个错误。该错误来自recyclerview和motionlayot之间的兼容性。只需将您的recyclerview版本设置为最新版本即可。
Android SwipeRefreshLayout
SwipeRefreshLayout字面意思就是下拉刷新的布局,继承自ViewGroup,在support v4兼容包下,但必须把你的support library的版本升级到19.1。 提到下拉刷新大家一定对ActionBarPullToRefresh比较熟悉,而如今google推出了更官方的下拉刷新组件,这无疑是对开发者来说比较好的消息。利用这个组件可以很方便的实现Google Now的刷新效果,见下图:
主要方法
setOnRefreshListener(OnRefreshListener): 为布局添加一个Listener
setRefreshing(boolean): 显示或隐藏刷新进度条
isRefreshing(): 检查是否处于刷新状态
setColorScheme(): 设置进度条的颜色主题,最多能设置四种
xml布局文件
布局文件很简单,只需要在最外层加上SwipeRefreshLayout,然后他的child是可滚动的view即可,如ScrollView或者ListView。如:
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="@string/hello_world"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"/>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
Activity代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
}
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override public void run() {
swipeLayout.setRefreshing(false);
}
}, 5000);
}
上面的代码很简单,只需要给SwipeRefreshLayout添加一个listener,值得说明的是setColorScheme方法是设置刷新进度条的颜色,最多只能设置4种循环显示,默认第一个是随用户手势加载的颜色进度条。
源码
写了的小demo在github上,地址在:SwipeRefreshLayoutDemo
总结
google在不断完善自己的sdk,推出越来越多的组件,其目的是让开发更简单,设计上更统一,这可能是google未来的方向,不管怎样,这对开发者来说无疑是非常好的消息。
关于Android – 在SwipeRefreshLayout加载时单击RecyclerView时崩溃和swiperefreshlayout加载更多的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于032 Android智能下拉刷新框架-SmartRefreshLayout+RecyclerView的使用、Android FlowLayout作为RecyclerView LayoutManager、Android MotionLayout + Recyclerview,recyclerview中的视图不可单击、Android SwipeRefreshLayout等相关内容,可以在本站寻找。
本文标签: