我有一个片段(PokedexFragment.java),其中包含带有一些项目的GridView(通过Custom Recyclerview)。当我单击一个项目时,当前片段将被另一个片段(SinglePokemonFragment.java)替换为所选项目的详细信息。 用户想直接转到gridview中的最后一项或下一项(按“前进” /“后退”按钮)。当他按下“硬件”“后退按钮”时,旧的片段(SinglePokemonFragment.java)仍然可见!
我不喜欢使用addBackStack,因为不应该有任何堆栈。
代码的哪一部分是错误的或必须更新,为什么? 我以为替换意味着替换...
SinglePokemonFragment.java(按钮)
/usr/local/rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/fileutils.rb:90: warning: already initialized constant FileUtils::VERSION
/usr/local/rvm/gems/ruby-2.5.0/gems/fileutils-1.1.0/lib/fileutils.rb:92: warning: previous definition of VERSION was here
/usr/local/rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/fileutils.rb:1188: warning: already initialized constant FileUtils::Entry_::S_IF_DOOR
/usr/local/rvm/gems/ruby-2.5.0/gems/fileutils-1.1.0/lib/fileutils.rb:1267: warning: previous definition of S_IF_DOOR was here
/usr/local/rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/fileutils.rb:1446: warning: already initialized constant FileUtils::Entry_::DIRECTORY_TERM
/usr/local/rvm/gems/ruby-2.5.0/gems/fileutils-1.1.0/lib/fileutils.rb:1541: warning: previous definition of DIRECTORY_TERM was here
/usr/local/rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/fileutils.rb:1448: warning: already initialized constant FileUtils::Entry_::SYSCASE
/usr/local/rvm/gems/ruby-2.5.0/gems/fileutils-1.1.0/lib/fileutils.rb:1543: warning: previous definition of SYSCASE was here
/usr/local/rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/fileutils.rb:1501: warning: already initialized constant FileUtils::OPT_TABLE
/usr/local/rvm/gems/ruby-2.5.0/gems/fileutils-1.1.0/lib/fileutils.rb:1596: warning: previous definition of OPT_TABLE was here
/usr/local/rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/fileutils.rb:1555: warning: already initialized constant FileUtils::LOW_METHODS
/usr/local/rvm/gems/ruby-2.5.0/gems/fileutils-1.1.0/lib/fileutils.rb:1650: warning: previous definition of LOW_METHODS was here
/usr/local/rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/fileutils.rb:1562: warning: already initialized constant FileUtils::METHODS
/usr/local/rvm/gems/ruby-2.5.0/gems/fileutils-1.1.0/lib/fileutils.rb:1657: warning: previous definition of METHODS was here
/home/ubuntu/workspace/config/initializers/devise.rb:289:in `block in <top (required)>': undefined method `action_mailer' for Devise:Module (NoMethodError)
from /usr/local/rvm/gems/ruby-2.5.0/gems/devise-4.5.0/lib/devise.rb:307:in `setup'
from /home/ubuntu/workspace/config/initializers/devise.rb:5:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.5.0/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:287:in `load'
from /usr/local/rvm/gems/ruby-2.5.0/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:287:in `block in load'
from /usr/local/rvm/gems/ruby-2.5.0/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:259:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.5.0/gems/activesupport-5.0.7/lib/active_support/dependencies.rb:287:in `load'
from /usr/local/rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/engine.rb:648:in `block in load_config_initializer'
from /usr/local/rvm/gems/ruby-2.5.0/gems/activesupport-5.0.7/lib/active_support/notifications.rb:166:in `instrument'
from /usr/local/rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/engine.rb:647:in `load_config_initializer'
from /usr/local/rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/engine.rb:612:in `block (2 levels) in <class:Engine>'
from /usr/local/rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/engine.rb:611:in `each'
from /usr/local/rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/engine.rb:611:in `block in <class:Engine>'
from /usr/local/rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/initializable.rb:30:in `instance_exec'
from /usr/local/rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/initializable.rb:30:in `run'
from /usr/local/rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/initializable.rb:55:in `block in run_initializers'
from /usr/local/rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/tsort.rb:228:in `block in tsort_each'
from /usr/local/rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
from /usr/local/rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from'
from /usr/local/rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/tsort.rb:431:in `each_strongly_connected_component_from'
from /usr/local/rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/tsort.rb:421:in `block in each_strongly_connected_component_from'
from /usr/local/rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/initializable.rb:44:in `each'
from /usr/local/rvm/gems/ruby-2.5.0/gems/railties-5.0.7/lib/rails/initializable.rb:44:in `tsort_each_child'
from /usr/local/rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/tsort.rb:415:in `call'
from -e:1:in `<main>'
MainFragment.java(带有ViewPager)
cLayBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create new fragment and transaction
Fragment newFragment = new SinglePokemonFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putInt("POKEMON_ID", Integer.parseInt(db.getPokemonID(intPokeDexID-1)));
bundle.putInt("ARRAY_ID", intPokeArrayID-1);
bundle.putInt("POKEMON_ID_MAX", getIntPokemonIDMax);
bundle.putInt("POKEDEX_ID", intPokeDexID-1);
newFragment.setArguments(bundle);
transaction.replace(R.id.frame_container, newFragment);
// Commit the transaction
transaction.commit();
}
});
cLayForward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create new fragment and transaction
Fragment newFragment = new SinglePokemonFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
Bundle bundle = new Bundle();
bundle.putInt("POKEMON_ID", Integer.parseInt(db.getPokemonID(intPokeDexID+1)));
bundle.putInt("ARRAY_ID", intPokeArrayID+1);
bundle.putInt("POKEMON_ID_MAX", getIntPokemonIDMax);
bundle.putInt("POKEDEX_ID", intPokeDexID+1);
newFragment.setArguments(bundle);
transaction.replace(R.id.frame_container, newFragment);
// Commit the transaction
transaction.commit();
}
});
PokedexFragment.java
public class MainFragment extends Fragment {
private TextView titleTxtView;
private static final String ARG_PARAM1 = "param1";
private String mParam1;
TabLayout tabLayout;
ViewPager viewPager;
private FragmentActivity myContext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
viewPager = (ViewPager) view.findViewById(R.id.pager);
if (viewPager != null) {
Log.v("ViewPager", "IS NOT NULL");
setupViewPager(viewPager);
tabLayout.getTabAt(0).setIcon(R.drawable.ic_pokedex);
tabLayout.getTabAt(1).setIcon(R.drawable.pokedex_kanto);
tabLayout.getTabAt(2).setIcon(R.drawable.pokedex_johto);
tabLayout.getTabAt(3).setIcon(R.drawable.pokedex_hoenn);
}else{
Log.v("ViewPager", "ISNULL");
}
return view;
}
@Override
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
}
}
private void setupViewPager(ViewPager viewPager) {
PagerAdapter adapter = new PagerAdapter(getChildFragmentManager(), getActivity());
adapter.addFragment(new PokedexFragment().newInstance(0, 9999), "Alle");
adapter.addFragment(new PokedexFragment().newInstance(1, 151), "Kanto");
adapter.addFragment(new PokedexFragment().newInstance(152, 251), "Johto");
adapter.addFragment(new PokedexFragment().newInstance(252, 386), "Hoenn");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
}
}
CustomRecyclerAdapterPokedexGrid.java(ItemClickListener)
package com.spicysoftware.goexpert;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
import static android.content.ContentValues.TAG;
public class PokedexFragment extends Fragment{
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
MySQLiteHelper db;
// TODO: Rename and change types of parameters
private int param1, param2;
public PokedexFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
param1 = getArguments().getInt(ARG_PARAM1);
param2 = getArguments().getInt(ARG_PARAM2);
}
}
public static PokedexFragment newInstance(int intDexFrom, int intDexTo) {
PokedexFragment fragment = new PokedexFragment();
Bundle args = new Bundle();
args.putInt(ARG_PARAM1, intDexFrom);
args.putInt(ARG_PARAM2, intDexTo);
fragment.setArguments(args);
Log.d(TAG, "newInstance: 2F");
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View myView = inflater.inflate(R.layout.fragment_pokedexall, container, false);
db = new MySQLiteHelper(getActivity());
RecyclerView recyclerView = (RecyclerView)myView.findViewById(R.id.rvPokedex);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(),4);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemViewCacheSize(20);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
CustomRecyclerAdapterPokedexGrid adapter = new CustomRecyclerAdapterPokedexGrid(getActivity(), db.getPokemon(param1,param2));
recyclerView.setAdapter(adapter);
return myView;
}
}