@作者 : 西野奈留
@博客: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-