我有一个Activity应用程序,由于DrawerLayout和全局NavBar而必须完成。我有一些碎片。这是我的问题:
我有片段A ,其中有一个RecyclerView可以从服务器获取新闻,还有片段B ,用于显示单个新闻。
片段A 具有无限滚动的RecyclerView,滚动发生后,我正在从服务器获取下一页数据。例如,当我在第三页上并且单击某些新闻时,片段B 开始。当我从片段B 中按回时,片段A 再次开始,但是它将加载第一页并再次执行所有工作。
是否有一种方法可以保持片段A 状态,只需打开其顶部的片段B ,以便当我单击 B 时strong>它会关闭,并再次显示 A 。
代码如下:
AllNewsFragment.java
public class AllNewsFragment extends Fragment {
List<News> newsList = new ArrayList<>();
RecyclerView allNewsRecycler;
ProgressBar progressBar;
NewsRecyclerViewAdapter newsRecyclerViewAdapter;
public NewsByCityDelegate delegate;
public int pageNum = 1;
boolean isLoading = false;
int totalPage;
boolean isLastPage = false;
int tip;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_all_news, container, false);
delegate.setUpFragment(ScreenTypeEnum.ALL_NEWS);
delegate.setSubGroupTag(tip == 1 ? 2 : 1 );
allNewsRecycler = viewGroup.findViewById(R.id.allNewsRecyclerView);
progressBar = viewGroup.findViewById(R.id.progressBar);
Bundle bundle = this.getArguments();
this.tip = bundle.getInt("FRAGMENT_TYPE"); // tip 1: AllNews, tip 2: RecommendedNews
TextView gradLabel = delegate.getGradLabel();
gradLabel.setText(tip == 1 ? "Vijesti" : "Izdvojene vijesti");
allNewsRecycler.setHasFixedSize(true);
loadAllNews(1);
gradLabel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
delegate.getNestedScrollView().smoothScrollTo(0,-1);
}
});
return viewGroup;
}
public void loadAllNews(int pageNum) {
isLastPage = false;
Retrofit retrofit = RetrofitClientInstance.getRetrofitInstance();
final GetDataService getDataService = retrofit.create(GetDataService.class);
Call<AllNews> call = (tip == 1 ? getDataService.getAllNews(pageNum, 1) : getDataService.getRecommendedNews());
call.enqueue(new Callback<AllNews>() {
@Override
public void onResponse(Call<AllNews> call, Response<AllNews> response) {
final PagedNews pagedNews = response.body().getNews();
newsList = pagedNews.getData();
totalPage = pagedNews.getLast_page();
initRecycler();
progressBar.setVisibility(View.GONE);
}
@Override
public void onFailure(Call<AllNews> call, Throwable t) {
System.out.println("NE PROLAZI CALL " + t.toString());
}
});
}
void initRecycler() {
newsRecyclerViewAdapter = new NewsRecyclerViewAdapter(getContext(), newsList, delegate);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
allNewsRecycler.setLayoutManager(linearLayoutManager);
allNewsRecycler.setAdapter(newsRecyclerViewAdapter);
allNewsRecycler.addOnScrollListener(new PaginationScrollListener(linearLayoutManager) {
@Override
protected void loadMoreItems() {
isLoading = true;
if (!isLastPage) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
cacheNextPage();
}
}, 200);
}
}
@Override
public boolean isLastPage() {
return isLastPage;
}
@Override
public boolean isLoading() {
return isLoading;
}
});
}
void cacheNextPage() {
isLoading = false;
if (pageNum == totalPage) isLastPage = true;
else pageNum++;
Retrofit retrofit = RetrofitClientInstance.getRetrofitInstance();
GetDataService getDataService = retrofit.create(GetDataService.class);
Call<AllNews> call2 = (tip == 1 ? getDataService.getAllNews(pageNum, 1) : getDataService.getRecommendedNews());
call2.enqueue(new Callback<AllNews>() {
@Override
public void onResponse(Call<AllNews> call, Response<AllNews> response2) {
PagedNews pagedNews2 = response2.body().getNews();
newsList.addAll(pagedNews2.getData());
newsRecyclerViewAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<AllNews> call, Throwable t) {
System.out.println("NE PROLAZI DRUGI PUT: " + t.toString());
}
});
}
}
SingleNewsFragment.java
public class SingleNewsFragment extends Fragment {
HomeFragmentDelegate delegate;
News singleNews;
LinearLayout gradLabelContainer;
public void setDelegate(HomeFragmentDelegate delegate){
this.delegate = delegate;
}
public void setSelectedNews(News selectedNews) {
this.singleNews = selectedNews;
}
public void setGradLabelContainer(LinearLayout gradLabelContainer){
this.gradLabelContainer = gradLabelContainer;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_single_news,container,false);
System.out.println("SINGLE NEWS: "+singleNews.getCity());
delegate.setSingleNewsFragment();
gradLabelContainer.requestFocus();
gradLabelContainer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
delegate.backPressed();
}
});
final TextView gradTitle, newsTitle, newsContent, newsDate, gradLabel;
ImageView gradCover;
gradLabel = gradLabelContainer.findViewById(R.id.singleNewsGradTextView);
gradTitle = viewGroup.findViewById(R.id.gradImeTextView);
gradTitle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Grad grad = null;
for(Grad g : delegate.getGradovi()){
if(g.getName().equalsIgnoreCase(gradTitle.getText().toString()))
{
grad = g;
}
}
Bundle bundle = new Bundle();
bundle.putInt("CITY_ID", Integer.parseInt(grad.getId()));
bundle.putString("CITY_NAME", grad.getName());
bundle.putString("CITY_COVER", grad.getCover_photo());
bundle.putString("CITY_URL_VALUE", grad.getCity_page_link());
bundle.putString("CITY_COLOR",grad.getColor());
delegate.cityPressed(bundle);
}
});
newsTitle = viewGroup.findViewById(R.id.singleNewsTitleTV);
newsContent = viewGroup.findViewById(R.id.singleNewsContentTV);
newsDate = viewGroup.findViewById(R.id.singleNewsDateTV);
gradCover = viewGroup.findViewById(R.id.singleNewsGradImageView);
//treba iskoristiti Glide
System.out.println("SINGLE NEWS: "+singleNews.getCity()+", "+singleNews.getTitle()+", "+singleNews.getCover_photo());
Glide.with(getContext()).load("http://localinfo.proserver.me/"+singleNews.getCover_photo()).into(gradCover);
System.out.println("SINGLE NEWS: "+singleNews.getCity()+", "+singleNews.getTitle());
gradLabel.setText(singleNews.getCity());
gradTitle.setText(singleNews.getCity());
newsTitle.setText(singleNews.getTitle());
final String OLD_FORMAT = "yyyy-MM-dd";
final String NEW_FORMAT = "dd. MM. yyyy. ";
String newsDate1 = singleNews.getDate().substring(0,11);
String newsDateFormatted;
SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
Date d1 = null;
try {
d1 = sdf.parse(newsDate1);
} catch (ParseException e) {
e.printStackTrace();
}
sdf.applyPattern(NEW_FORMAT);
newsDateFormatted = sdf.format(d1);
newsDate.setText(newsDateFormatted);
newsContent.setText(singleNews.getText());
GradientDrawable background = (GradientDrawable) gradTitle.getBackground();
String colorValue = singleNews.getCity_color();
background.setColor(Color.parseColor(colorValue));
return viewGroup;
}
}
这是onClick在RecyclerItem上的作用:
singleNewsFragment = new SingleNewsFragment();
singleNewsFragment.setDelegate(this);
singleNewsFragment.setGradLabelContainer(singleNewsGradLabelContainer);
singleNewsFragment.setSelectedNews(newsSelected);
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer,singleNewsFragment)
.addToBackStack(null)
.commit();
recyclerAdapter非常简单,但是如果需要,我也可以上传该代码。
我尝试使用add而不是replace和show / hide片段逻辑,但是它没有按我的预期工作,或者可能我不太了解以使其起作用。
有没有人可以指导我解决这个问题?