我知道我的头衔并不好,但我真的不知道怎么说这是一句话。当我第一次打开我的应用程序时,一切都正确加载。我点击其中一个cardviews打开一个新片段,它会正确显示。当我按回它时,它回到主要活动,并且每件事都是正确的。但是,如果我再次点击cardview打开一个新的片段它不起作用。它会加载新片段,因为选项卡会显示,但viewpager不会加载任何内容。在我提供的图片中有更好的解释。
主要活动:
public class MainActivity extends AppCompatActivity {
private ArrayList<NewsItem> items;
private RecyclerView r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
r = (RecyclerView) findViewById(R.id.newsRecyclerView);
if (getSupportActionBar()!=null) getSupportActionBar().setElevation(0);
r.setHasFixedSize(true);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),3);
r.setLayoutManager(gridLayoutManager);
initializeItems();
NewsAdapter newsAdapter = new NewsAdapter(this,items);
r.setAdapter(newsAdapter);
OverScrollDecoratorHelper.setUpOverScroll(r, OverScrollDecoratorHelper.ORIENTATION_VERTICAL);
}
private void initializeItems(){
items = new ArrayList<NewsItem>();
ArrayList<NewsCat> vp = new ArrayList<NewsCat>();
vp.add(new NewsCat("News","http://vapepolitics.com/feed/"));
vp.add(new NewsCat("Products","https://bitcoinmagazine.com/feed/"));
items.add(new NewsItem(R.mipmap.ic_launcher,"Vape Politics",vp,1));
ArrayList<NewsCat> vm = new ArrayList<>();
vm.add(new NewsCat("News","https://vapemagazine.com/feed/"));
items.add(new NewsItem(R.mipmap.ic_launcher,"Vape Magazine",vm,1));
ArrayList<NewsCat> vv = new ArrayList<>();
vv.add(new NewsCat("News","http://www.vaporvanity.com/feed/"));
items.add(new NewsItem(R.mipmap.ic_launcher,"Vapor Vanity",vv,1));
}
NewsFragment:
public class NewsFragment extends Fragment {
private View v;
private ViewPager mViewPager;
private List<NewsCat> links;
private NavigationTabStrip mCenterNavigationTabStrip;
private String[] titles;
private String[] linkes;
private Context context;
MyPageAdapter pageAdapter;
public NewsFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_news, container, false);
Bundle bundle = getArguments();
Type type = new TypeToken<List<NewsCat>>() {}.getType();
Gson gson = new Gson();
String json = bundle.getString("links");
List<NewsCat> linkers = gson.fromJson(json,type);
links = linkers;
mViewPager = (ViewPager) v.findViewById(R.id.vp);
getFragments();
initUI(v);
setUI();
return v;
}
private void initUI(View v) {
mViewPager = (ViewPager) v.findViewById(R.id.vp);
mCenterNavigationTabStrip = (NavigationTabStrip) v.findViewById(R.id.nts_center);
}
private List<Fragment> getFragments() {
List<Fragment> fList = new ArrayList<Fragment>();
for (int i=0;i <links.size();i++) {
fList.add(HalfFragment.newInstance(links.get(i).getLink()));
}
return fList;
}
@Override
public void onDestroy() {
super.onDestroy();
}
private void setUI() {
List<Fragment> fragments = getFragments();
titles = new String[links.size()];
linkes = new String[links.size()];
for (int i = 0;i<links.size();i++) {
NewsCat newsCat = links.get(i);
titles[i] = newsCat.getTitle();
linkes[i] = newsCat.getLink();
}
pageAdapter = new MyPageAdapter(getActivity().getSupportFragmentManager(), linkes);
mViewPager.setAdapter(pageAdapter);
mCenterNavigationTabStrip.setTitles(titles);
mCenterNavigationTabStrip.setTabIndex(0,true);
mCenterNavigationTabStrip.setViewPager(mViewPager);
}
NewsAdapter:
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.PersonViewHolder> {
private Context context;
private ArrayList<NewsItem> items;
public NewsAdapter(Context context, ArrayList<NewsItem> newsItems) {
this.context = context;
this.items = newsItems;
}
public class PersonViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView personName;
TextView personAge;
ImageView personPhoto;
PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.newsItemCard);
personName = (TextView) itemView.findViewById(R.id.newsItemTitle);
personPhoto = (ImageView) itemView.findViewById(R.id.newsItemImage);
}
}
@Override
public NewsAdapter.PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder holder, int position) {
final NewsItem newsItem = items.get(position);
holder.personName.setText(items.get(position).getTitle());
//holder.personPhoto.setImageResource(items.get(position).getImage());
Picasso.with(context).load(items.get(position).getImage()).resize(400,400).into(holder.personPhoto);
holder.cv.setCardElevation(10);
holder.cv.setClipToPadding(false);
holder.cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle args = new Bundle();
//args.putParcelableArray("links",newsItem.getLink());
Gson gson = new Gson();
Type type = new TypeToken<List<NewsCat>>() {}.getType();
String json = gson.toJson(newsItem.getLink(),type);
args.putString("links",json);
NewsFragment rssFragment = new NewsFragment();
rssFragment.setArguments(args);
FragmentManager fragmentManager = ((FragmentActivity)context).getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_main,rssFragment).addToBackStack(null).commit();
}
});
}
@Override
public int getItemCount() {
return items.size();
}
}
答案 0 :(得分:1)
我需要改变
public class MyPageAdapter extends FragmentPagerAdapter {
到
public class MyPageAdapter extends FragmentStatePagerAdapter {
答案 1 :(得分:0)
虽然不是示例代码,但是当 Fragment实例存储在第二个活动中时,我遇到了这个问题。因此,在完成并重新启动后,FragmentManager首先重用了这个旧的Fragment实例,并仅在第二次单击时创建了一个新实例:
public class SecondActivity {
private static Fragment fragment;
@Override
public void onBackPressed() {
...
SecondActivity.setFragment(null);
...
}
}