swiperefreshlayout嵌套recyclerview swipe怎么处理数据为空

【安卓笔记】RecyclerView+SwipeRefreshLayout示例,swiperefreshlayout-android100学习网
【安卓笔记】RecyclerView+SwipeRefreshLayout示例,swiperefreshlayout
【安卓笔记】RecyclerView+SwipeRefreshLayout示例,swiperefreshlayout 通过这篇文章你将学会:1.RecyclerView的基本用法;2.Swipe
【安卓笔记】RecyclerView+SwipeRefreshLayout示例,swiperefreshlayout
通过这篇文章你将学会:1.RecyclerView的基本用法;2.SwipeRefreshLayout的基本用法;3.为RecyclerView的item添加响应事件。RecyclerView简单说下,它是用来替代传统ListView的,RecyclerView更加灵活,而且可以与动画很好的结合,你可以很方便的为每个item增加各种动画效果,另外,RecyclerView强制使用ViewHolder模式,可以提高性能。截图:
步骤:1.添加依赖:
compile 'com.android.support:recyclerview-v7:21.0.0'
compile 'com.android.support:support-v4:22.0.0'
2.编写activity布局:&RelativeLayout xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.taobao.recyclerviewwithrefresh.ui.activity.GridActivity"&
&android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/refreshLayout_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"&
&android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
&&/android.support.v7.widget.RecyclerView&
&/android.support.v4.widget.SwipeRefreshLayout&
&/RelativeLayout&
3.编写每个item的布局:&?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
xmlns:tools="/tools"
android:layout_height="50dp"&
&ImageView
android:src="@mipmap/ic_launcher"
android:padding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/&
android:id="@+id/tv"
android:layout_width="wrap_content"
android:textSize="18sp"
android:gravity="center"
android:layout_height="match_parent"
tools:text="ss"/&
&/LinearLayout&4.数据源:package com.taobao.recyclerviewwithrefresh.
import java.util.ArrayL
import java.util.L
* Created by Rowandjj on .
public class DataSource
public static final List&String& generateData(int size)
if (size &= 0)
List&String& datas = new ArrayList&&();
for (int i = 0; i & i++)
datas.add("这是列表数据"+i);
5.分隔条:package com.taobao.recyclerviewwithrefresh.ui.
import android.content.C
import android.content.res.TypedA
import android.graphics.C
import android.graphics.drawable.D
import android.support.v7.widget.RecyclerV
import android.view.V
* Created by Rowandjj on .
public class MyItemDecoration extends RecyclerView.ItemDecoration
private static final int[] ATTRS = {android.R.attr.listDivider};
private Drawable mD
public MyItemDecoration(Context context)
TypedArray array = context.obtainStyledAttributes(ATTRS);
// 获取分隔条
mDivider = array.getDrawable(0);
array.recycle();
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state)
super.onDrawOver(c, parent, state);
int count = parent.getChildCount();
int left = parent.getPaddingLeft();
int right = parent.getWidth()-parent.getPaddingRight();
for(int i = 0; i & i++)
View v = parent.getChildAt(i);
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) v.getLayoutParams();
int top = v.getBottom() + params.bottomM
int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left,top,right,bottom);
mDivider.draw(c);
6.编写数据适配器,并为其增加点击响应事件package com.taobao.recyclerviewwithrefresh.ui.
import android.support.v7.widget.RecyclerV
import android.view.LayoutI
import android.view.V
import android.view.ViewG
import android.widget.TextV
import com.taobao.recyclerviewwithrefresh.R;
import java.util.L
* Created by Rowandjj on .
public class MainRecyclerAdapter extends RecyclerView.Adapter&MainRecyclerAdapter.ViewHolder&
private List&String& datas =
private OnItemClickListener mL
public void setOnItemClickListener(OnItemClickListener listener)
this.mListener =
public MainRecyclerAdapter(List&String& datas)
this.datas =
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
final View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
itemView.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
if(mListener != null)
mListener.OnItemClick(v, (String) itemView.getTag());
return new ViewHolder(itemView);
public void onBindViewHolder(ViewHolder holder, int position)
String s = datas.get(position);
holder.bindData(s);
holder.itemView.setTag(s);
public int getItemCount()
return datas.size();
* 批量增加
public void addItems(List&String& items)
if (items == null)
this.datas.addAll(0, items);
this.notifyItemRangeInserted(0, items.size());
public interface OnItemClickListener
public void OnItemClick(View view,String data);
static class ViewHolder extends RecyclerView.ViewHolder
private TextView mC
public ViewHolder(View itemView)
super(itemView);
mContent = (TextView) itemView.findViewById(R.id.tv);
public void bindData(String s)
if (s != null)
mContent.setText(s);
RecyclerView没有提供类似onItemClickListener之类的回调,这里我们自己实现了一个。。。7.主页面代码:package com.taobao.recyclerviewwithrefresh.ui.
import android.content.I
import android.os.AsyncT
import android.os.B
import android.support.v4.widget.SwipeRefreshL
import android.support.v7.app.ActionBarA
import android.support.v7.widget.LinearLayoutM
import android.support.v7.widget.RecyclerV
import android.view.M
import android.view.MenuI
import android.view.V
import android.widget.T
import com.taobao.recyclerviewwithrefresh.R;
import com.taobao.recyclerviewwithrefresh.data.DataS
import com.taobao.recyclerviewwithrefresh.ui.adapter.MyItemD
import com.taobao.recyclerviewwithrefresh.ui.adapter.MainRecyclerA
import java.util.ArrayL
import java.util.L
* 1.RecyclerView基本用法
LayoutManager
ItemAnimator
ItemDecoration
RecyclerView.Adapter
RecyclerView.ViewHolder
* 2.SwipeRefreshLayout用法
* 3.给RecyclerView的每个item添加响应事件的方式
* 4.控制RecyclerView滚动到某一具体位置: RecyclerView#scrollToPosition
public class MainActivity extends ActionBarActivity implements SwipeRefreshLayout.OnRefreshListener
private RecyclerView mRecyclerV
private SwipeRefreshLayout mR
private LinearLayoutManager mLinearLayoutM
private MainRecyclerAdapter mA
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRefreshlayout = (SwipeRefreshLayout) findViewById(R.id.refreshLayout);
mRefreshlayout.setOnRefreshListener(this);
mLinearLayoutManager = new LinearLayoutManager(this);
mAdapter = new MainRecyclerAdapter(DataSource.generateData(20));
mRecyclerView.setAdapter(mAdapter);
//每个item高度一致,可设置为true,提高性能
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mRecyclerView.addItemDecoration(new MyItemDecoration(this));
//为每个item增加响应事件
mAdapter.setOnItemClickListener(new MainRecyclerAdapter.OnItemClickListener()
public void OnItemClick(View view, String data)
Toast.makeText(MainActivity.this, "data:" + data, Toast.LENGTH_SHORT).show();
public void onRefresh()
new UpdateTask().execute();
private class UpdateTask extends AsyncTask&Void,Void,List&String&&
protected List&String& doInBackground(Void... params)
Thread.sleep(2000);
} catch (InterruptedException e)
e.printStackTrace();
List&String& strings = new ArrayList&&();
strings.add("新数据1");
strings.add("新数据2");
strings.add("新数据3");
strings.add("新数据4");
protected void onPostExecute(List&String& strings)
mAdapter.addItems(strings);
//通知刷新完毕
mRefreshlayout.setRefreshing(false);
//滚动到列首部---&这是一个很方便的api,可以滑动到指定位置
mRecyclerView.scrollToPosition(0);
public boolean onCreateOptionsMenu(Menu menu)
getMenuInflater().inflate(R.menu.menu_main, menu);
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_grid)
this.startActivity(new Intent(this, GridActivity.class));
}else if(id == R.id.action_settings)
Toast.makeText(MainActivity.this, "run...", Toast.LENGTH_SHORT).show();
return super.onOptionsItemSelected(item);
/Androidjc/975670.true/Androidjc/975670.htmlTechArticle【安卓笔记】RecyclerView+SwipeRefreshLayout示例,swiperefreshlayout 通过这篇文章你将学会: 1.RecyclerView的基本用法;2.SwipeRefreshLayout的基本用法;3...I have added a headerView to my RecyclerView as described in the answer of this question:
The header is an empty view which will be hidden under toolBar.
&?xml version="1.0" encoding="utf-8"?&
&View xmlns:android="/apk/res/android"
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" /&
The problem is the place of SwipeRefreshLayout. It will be started from Header which is under Toolbar, so it partially hidden under Toolbar.
&android.support.v4.widget.SwipeRefreshLayout xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="com.test.android.client.fragment.MyFragment"&
&android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" &
&/android.support.v7.widget.RecyclerView&
&/android.support.v4.widget.SwipeRefreshLayout&
How can I solve this problem?
解决方案 I found setProgressViewOffset method as a workaround
, int, int)
But still I am not able to figure out how can I get default start and end postion. (parameter which will be passed in the method)
本文地址: &
我添加了一个headerView我RecyclerView在这个问题的答案说明:Is有一个addHeaderView相当于RecyclerView? 标题是将工具栏下面隐藏空视图。 <?XML版本=“1.0”编码=“UTF-8”&GT?;<景观的xmlns:机器人=“/apk/res/android”
机器人:ID =“@ + ID /头”
机器人:layout_width =“match_parent”
机器人:“ATTR / actionBarSize”layout_height = /> 问题是SwipeRefreshLayout的地方。它将从标题是工具栏下启动,所以它的工具栏下的部分隐藏。 < android.support.v4.widget.SwipeRefreshLayout的xmlns:机器人=“/apk/res/android”
的xmlns:工具=“/tools”
机器人:ID =“@ + ID / swipe_refresh_layout”
机器人:layout_width =“match_parent”
机器人:layout_height =“WRAP_CONTENT”
工具:上下文=“com.test.android.client.fragment.MyFragment”>
< android.support.v7.widget.RecyclerView
机器人:ID =“@ + ID / recyclerView”
机器人:layout_width =“match_parent”
机器人:layout_height =“match_parent”
机器人:滚动条=“垂直”>
< /android.support.v7.widget.RecyclerView&
< /android.support.v4.widget.SwipeRefreshLayout& 我该如何解决这个问题呢?解决方案 我发现setProgressViewOffset方法作为一种解决方法 /reference/android/support/v4/widget/SwipeRefreshLayout.html#setProgressViewOffset(boolean, INT,INT)不过还是我无法弄清楚我如何能得到默认的开始和结束现在的位置。 (参数将在该方法被传递)
本文地址: &
扫一扫关注官方微信关于SwipeRefreshLayout的一些问题
[问题点数:20分,结帖人u]
关于SwipeRefreshLayout的一些问题
[问题点数:20分,结帖人u]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。

我要回帖

更多关于 swiperecyclerview 的文章

 

随机推荐