Android_RecyclerView&SwipeRefreshLayout_swiperefreshlayout不显示_解决

本文记录了作者在使用Android的RecyclerView和SwipeRefreshLayout时遇到的问题,即SwipeRefreshLayout在下拉时无法显示。经过排查,问题出在MainActivity的onCreate方法中,由于未设置RecyclerView的Adapter,导致SwipeRefreshLayout无法正常工作。解决方案是为RecyclerView添加Adapter,问题得到解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@作者 : 西野奈留
@博客:http://blog.youkuaiyun.com/narunishino
-2016/5/18-


沉重的教训啊!

昨天下午搞了一个下午,然后到现在才解决…..真是郁闷…

具体情况:下拉的时候swiperefreshlayout不显示。

布局文件:

<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout"
        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.support.v4.widget.SwipeRefreshLayout>

上面的布局代码明明没有错啊,为什么swiperefreshlayout就是不显示呢。

原因是:在MainActivity的onCreat中,我只写了以下的代码:

    private void initRecycler() {
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        if (recyclerView != null) {
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            //真是坑....
            //recyclerView.setAdapter(new RecyclerAdapter(this));
        }
    }

    private void init() {
        final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        if (swipeRefreshLayout != null) {
            swipeRefreshLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW);

            swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {

                    swipeRefreshLayout.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            swipeRefreshLayout.setRefreshing(false);
                        }
                    }, 5000);

                }
            });
        }
    }

,没有加上adapter….
加了adapter就没事了….我了个去!!!!!!!

啊啊啊啊啊啊啊啊。。。。。。。。。。。。。。。。。。。。。。。。。。。

//RecyclerAdapter.java

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyHolder> {

    private Context mContext;
    private List<String> mData;

    public RecyclerAdapter(Context context) {
        mContext = context;
        mData = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            mData.add(i + "行");
        }
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyHolder holder = new MyHolder(LayoutInflater.from(mContext).inflate(R.layout.tv_item, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        holder.tv.setText(mData.get(position));
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        TextView tv;
        public MyHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tvItem);
        }
    }
}
//tv_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tvItem"
    android:layout_width="match_parent"
    android:layout_height="50dp">

</TextView>

-End-


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值