刷新片段而不是主片段 activity
Refresh fragment instead of main activity
我正在尝试在我的片段中实现 "swipe to refresh"。我通过在我的 main activity 中插入一个定时器来做到这一点,通过使用刷新的定时器 class,但我想通过直接在片段本身内部实现一个函数来改进它。这是代码:
public class HomeFragment extends Fragment {
private RecyclerView recyclerView;
public HomeFragment() {
}
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.main_fragment, container, false);
recyclerView = view.findViewById(R.id.rc_view);
loadMovie();
return view;
}
private void loadMovie() {
ApiService apiService = ApiBuilder.getClient(getContext()).create(ApiService.class);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL, false));
Call<MovieResponse> call = apiService.getDiscover(BuildConfig.API_KEY,Values.LANGUAGE,Values.SORT_BY[0], Values.ADULT, Values.GENRE[1], Values.PAGE[0]);
call.enqueue(new Callback<MovieResponse>() {
@Override
public void onResponse(Call<MovieResponse>call, Response<MovieResponse> response) {
final List<MovieModel> movies = response.body().getResults();
recyclerView.setAdapter(new MoviesAdapter(movies, R.layout.content_main, getContext()));
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
GestureDetector gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
public boolean onSingleTapUp(MotionEvent e){
return true;
}
});
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && gestureDetector.onTouchEvent(e)){
int position = rv.getChildAdapterPosition(child);
Intent i = new Intent(getContext(), DetailActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra(DetailActivity.EXTRA_TITLE, movies.get(position).getTitle());
i.putExtra(DetailActivity.EXTRA_OVERVIEW, movies.get(position).getOverview());
i.putExtra(DetailActivity.EXTRA_TIME, movies.get(position).getReleaseDate());
i.putExtra(DetailActivity.EXTRA_POSTER, movies.get(position).getPosterPath());
i.putExtra(DetailActivity.EXTRA_LANGUAGE, movies.get(position).getOriginalLanguage());
try{
List<Integer> genr = movies.get(position).getGenreIds();
i.putExtra(DetailActivity.EXTRA_GENRES, genr.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
getContext().startActivity(i);
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
if (movies != null ){
MovieModel firstMovie = movies.get(0);
if(firstMovie != null) {
Log.i("TAG", firstMovie.getTitle());
}
}
}
@Override
public void onFailure(Call<MovieResponse>call, Throwable t) {
}
});
}
}
这是我在主要 activity 中实现的计时器,我想删除它:
TimerTask timertask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
startService(new Intent(MainActivity.this, TimerService.class));
}
});
}
};
Timer timer = new Timer();
timer.schedule(timertask, 0, 10*SECONDS);
编辑:
我通过在 onCreate 方法中实现 SwipeRefreshLayout 更新了我的代码,但它仍然不起作用:
mPullToRefresh = view.findViewById(R.id.swipe_list);
final Handler handler = new Handler();
mPullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
loadMovie(); //load your moview from here
mPullToRefresh.setRefreshing(false);
}
}, 1000);
}
});
这是我更新的片段布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Movies"
android:layout_marginLeft="@dimen/_10sdp"
android:layout_marginRight="@dimen/_10sdp"
android:textSize="@dimen/_20ssp"
android:textStyle="bold"
android:textColor="#ffffff"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_5sdp"
android:text="Action"
android:layout_marginLeft="@dimen/_10sdp"
android:layout_marginRight="@dimen/_10sdp"
android:textSize="@dimen/_12ssp"
android:textColor="@color/colorAccent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/_150sdp"
android:layout_marginTop="@dimen/_10sdp"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/rc_view"/>
</LinearLayout>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
有一个布局来实现功能。
将此放入您的片段 xml 文件中:-
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
现在在您的片段中:-
1) 执行
SwipeRefreshLayout mPullToRefresh = view.findViewById(R.id.swipe_list)
2) 那么,
mPullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
loadMovie(); //load your moview from here
mPullToRefresh.setRefreshing(false);
}
}, 1000);
}
});
如果觉得有用请点赞。
我正在尝试在我的片段中实现 "swipe to refresh"。我通过在我的 main activity 中插入一个定时器来做到这一点,通过使用刷新的定时器 class,但我想通过直接在片段本身内部实现一个函数来改进它。这是代码:
public class HomeFragment extends Fragment {
private RecyclerView recyclerView;
public HomeFragment() {
}
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.main_fragment, container, false);
recyclerView = view.findViewById(R.id.rc_view);
loadMovie();
return view;
}
private void loadMovie() {
ApiService apiService = ApiBuilder.getClient(getContext()).create(ApiService.class);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL, false));
Call<MovieResponse> call = apiService.getDiscover(BuildConfig.API_KEY,Values.LANGUAGE,Values.SORT_BY[0], Values.ADULT, Values.GENRE[1], Values.PAGE[0]);
call.enqueue(new Callback<MovieResponse>() {
@Override
public void onResponse(Call<MovieResponse>call, Response<MovieResponse> response) {
final List<MovieModel> movies = response.body().getResults();
recyclerView.setAdapter(new MoviesAdapter(movies, R.layout.content_main, getContext()));
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
GestureDetector gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
public boolean onSingleTapUp(MotionEvent e){
return true;
}
});
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && gestureDetector.onTouchEvent(e)){
int position = rv.getChildAdapterPosition(child);
Intent i = new Intent(getContext(), DetailActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra(DetailActivity.EXTRA_TITLE, movies.get(position).getTitle());
i.putExtra(DetailActivity.EXTRA_OVERVIEW, movies.get(position).getOverview());
i.putExtra(DetailActivity.EXTRA_TIME, movies.get(position).getReleaseDate());
i.putExtra(DetailActivity.EXTRA_POSTER, movies.get(position).getPosterPath());
i.putExtra(DetailActivity.EXTRA_LANGUAGE, movies.get(position).getOriginalLanguage());
try{
List<Integer> genr = movies.get(position).getGenreIds();
i.putExtra(DetailActivity.EXTRA_GENRES, genr.toString());
} catch (Exception ex) {
ex.printStackTrace();
}
getContext().startActivity(i);
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
});
if (movies != null ){
MovieModel firstMovie = movies.get(0);
if(firstMovie != null) {
Log.i("TAG", firstMovie.getTitle());
}
}
}
@Override
public void onFailure(Call<MovieResponse>call, Throwable t) {
}
});
}
}
这是我在主要 activity 中实现的计时器,我想删除它:
TimerTask timertask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
startService(new Intent(MainActivity.this, TimerService.class));
}
});
}
};
Timer timer = new Timer();
timer.schedule(timertask, 0, 10*SECONDS);
编辑: 我通过在 onCreate 方法中实现 SwipeRefreshLayout 更新了我的代码,但它仍然不起作用:
mPullToRefresh = view.findViewById(R.id.swipe_list);
final Handler handler = new Handler();
mPullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
loadMovie(); //load your moview from here
mPullToRefresh.setRefreshing(false);
}
}, 1000);
}
});
这是我更新的片段布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Movies"
android:layout_marginLeft="@dimen/_10sdp"
android:layout_marginRight="@dimen/_10sdp"
android:textSize="@dimen/_20ssp"
android:textStyle="bold"
android:textColor="#ffffff"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_5sdp"
android:text="Action"
android:layout_marginLeft="@dimen/_10sdp"
android:layout_marginRight="@dimen/_10sdp"
android:textSize="@dimen/_12ssp"
android:textColor="@color/colorAccent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="@dimen/_150sdp"
android:layout_marginTop="@dimen/_10sdp"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/rc_view"/>
</LinearLayout>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
有一个布局来实现功能。
将此放入您的片段 xml 文件中:-
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
现在在您的片段中:-
1) 执行
SwipeRefreshLayout mPullToRefresh = view.findViewById(R.id.swipe_list)
2) 那么,
mPullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
loadMovie(); //load your moview from here
mPullToRefresh.setRefreshing(false);
}
}, 1000);
}
});
如果觉得有用请点赞。