我的一个活动上有一个刷新按钮。每当用户按下该按钮时,我想使用SwipeRefreshLayout刷新动画,但我不希望用户能够向下滑动以刷新屏幕。我有什么方法可以做到这一点吗?
答案 0 :(得分:1)
你有没有尝试过;
//button clicked...
//show loader, disable further pulltorefresh requests
//do some timed task
pullToRefresh.setRefreshing(true);
//Task done...
//hide loader, allow pulltorefresh requests
pullToRefresh.setRefreshing(false);
我在片段中使用;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.my_fragment, container, false);
pullToRefresh = (SwipeRefreshLayout)view.findViewById(R.id.pullToRefresh);
pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshContent();
}
});
Button refresh = (Button) view.findViewById(R.id.refreshMethod);
refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
refreshContent();
}
});
return view;
}
refreshContent() {
//call Async task via RetrieveFeedTask extends AsyncTask<Void, Void, String> {}
new RetrieveFeedTask().execute();
}
class RetrieveFeedTask extends AsyncTask<Void, Void, String> {
private Exception exception;
protected void onPreExecute() {
recycler.clearOnScrollListeners();
pullToRefresh.setRefreshing(true);
}
protected String doInBackground(Void... urls) {
//do task like get JSON from server
//try sleep for a bit
try {
Thread.sleep(2000);
} catch (InterruptedException error) {
}
}
protected void onPostExecute(String response) {
//do post stuff
//hide pullfresh, full refresh view done
pullToRefresh.setRefreshing(false);
}
}
答案 1 :(得分:1)
你试过swipeRefreshLayout.setEnabled(false);
吗?然后,您只需在按钮的监听器中调用setRefreshing(true)
和setRefreshing(false)