[英]Pull refresh with recycle view doesn't work
When I move the screen of my device under to refresh rss news feed, it doesn't work. 当我将设备的屏幕移到下面以刷新rss新闻提要时,它不起作用。 i tried to refresh news with
swiperefreshlayout
. 我试图用
swiperefreshlayout
刷新新闻。 I don't know why but it doesn't work: 我不知道为什么,但是不起作用:
MAIN ACTIVITY: 主要活动:
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
SwipeRefreshLayout refreshLayout;
private LinearLayoutManager linearLayoutManager;
private MyAdapter myRecyclerViewAdapter;
Context context;
ArrayList<FeedItem> feedItems=new ArrayList<FeedItem>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
refreshLayout=(SwipeRefreshLayout)findViewById(R.id.swipeLayout);
refreshLayout.setOnRefreshListener((SwipeRefreshLayout.OnRefreshListener) context);
refreshLayout.setColorSchemeColors(0, 0, 0, 0);
refreshLayout.setProgressBackgroundColor(android.R.color.transparent);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
ReadRss readRss=new ReadRss(this,recyclerView);
readRss.execute();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
int topRowVerticalPosition =
(recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
refreshLayout.setEnabled(topRowVerticalPosition >= 0);
refreshLayout.setEnabled(linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0);
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
});
/*recyclerView= (RecyclerView) findViewById(R.id.recyclerview);
linearLayoutManager =
new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
myRecyclerViewAdapter = new MyAdapter(context,feedItems);
//myRecyclerViewAdapter.setOnItemClickListener(this);
recyclerView.setAdapter(myRecyclerViewAdapter);
recyclerView.setLayoutManager(linearLayoutManager);*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
CONTENT MAIN.XML: 内容MAIN.XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.rssreader.MainActivity"
tools:showIn="@layout/activity_main">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipeLayout"
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:scrollbars="vertical" />
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
You have defined setOnRefreshListener
in your refreshLayout
but didn't implemented it. 您已经在
refreshLayout
定义了setOnRefreshListener
,但是没有实现它。
you have to use setOnRefreshListener
method on your swipe refresh layout like this. 您必须像这样在滑动刷新布局上使用
setOnRefreshListener
方法。
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
// call your Refresh method here
mswipeRefreshLayout.setRefreshing(false);
}
});
You have to set OnRefreshListener to the SwipeRefreshListener and handle the refresh in the callback. 您必须将OnRefreshListener设置为SwipeRefreshListener并处理回调中的刷新。
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener()
{
@Override
public void onRefresh()
{
// handle your refresh logic
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.