我在我的应用程序中使用带有ViewPager和recycleView的DrawerLayoutmenu,其中有三个选项卡。我在片段中使用ViewPager和recycleView。现在我的问题是,第一次没有错误,当我回来片段CongThucFragment它看起来像是错误
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
SharedPreferences sharedPreferencesToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferencesToken=getSharedPreferences("DataChefy",Context.MODE_PRIVATE);
TokenClass.Token = sharedPreferencesToken.getString("Token","");
Log.d("MainActivity10",TokenClass.Token);
if (savedInstanceState == null) {
Fragment fragment = null;
Class fragmentClass = null;
fragmentClass = UserInfoFragment.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_container, fragment).commit();
}
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
int id = item.getItemId();
Class fragmentClass = null;
if (id == R.id.nav_camera) {
fragmentClass = UserInfoFragment.class;
try {
ft.replace(R.id.fragment_container,(Fragment) fragmentClass.newInstance(), "UserInfoFragment");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
fragmentClass = CongThucFragment.class;
try {
ft.replace(R.id.fragment_container,(Fragment) fragmentClass.newInstance(), "CongThucFragment");
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} else if (id == R.id.nav_manage) {
SharedPreferences.Editor editor = sharedPreferencesToken.edit();
editor.putString("Token","");
editor.apply();
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
this.finish();
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public boolean isConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}}
这是我的片段
public class CongThucFragment extends Fragment {
private TabLayout tabLayout;
private ViewPager viewPager;
private FragmentActivity myContext;
static public CongThucFragment newInstance(){
return new CongThucFragment();
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onAttach(Context context) {
myContext = (FragmentActivity) context;
setRetainInstance(true);
super.onAttach(context);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmen_congthuc,container,false);
tabLayout= (TabLayout) view.findViewById(R.id.tabsChampionDetail);
viewPager= (ViewPager) view.findViewById(R.id.viewpagerChampionDetail);
tabLayout.setupWithViewPager(viewPager);
setupViewPager(viewPager);
return view;
}
private void setupViewPager(ViewPager viewPager ) {
ViewPagerAdapter adapter = new ViewPagerAdapter(myContext.getSupportFragmentManager());
for(int i =1 ;i<=3.;i++) {
if(i==1)adapter.addFragment(ChildrenCongthucFragment.newInstance("vn"), "Country " + i + "");
else if(i==2)adapter.addFragment(ChildrenCongthucFragment.newInstance("lao"), "Country " + i + "");
else adapter.addFragment(ChildrenCongthucFragment.newInstance("tay"), "Country " + i + "");
}
viewPager.setAdapter(adapter);
}}
片段2
public class ChildrenCongthucFragment extends Fragment{
String Country = null;
List<Congthuc> mItems = new ArrayList<>();
private RecyclerView mRecyclerView;
private RecyclerDataAdapter recyclerDataAdapter = null;
RecyclerView.LayoutManager layoutManagerNews;
static public ChildrenCongthucFragment newInstance(String country){
Bundle bundle = new Bundle();
bundle.putString("mCountry",country);
ChildrenCongthucFragment fragment = new ChildrenCongthucFragment();
fragment.setArguments(bundle);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initializeData();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.recycle_children_congthuc,container,false);
Country = getArguments().getString("mCountry");
mRecyclerView = (RecyclerView) view.findViewById(R.id.fragment_photo_collection_recycler_view);
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
recyclerDataAdapter =new RecyclerDataAdapter (view.getContext(),R.layout.recycle_children_congthuc,CountryFillter(Country,mItems));
mRecyclerView.setAdapter(recyclerDataAdapter);
return view;
}
private void initializeData(){
mItems.add(new Congthuc("1", "banh mì","vn","http://giadinh.vcmedia.vn/k:2016/photo-0-1472785146823/nhungmonanngonchogiadinhdip29.jpg"));
mItems.add(new Congthuc("2", "bánh gián","vn","http://toinayangi.vn/wp-content/uploads/2015/06/m%C3%B3n-%C4%83n-vi%E1%BB%87t-nam-n%E1%BB%95i-ti%E1%BA%BFng-g%E1%BB%8Fi-cu%E1%BB%91n.jpg"));
mItems.add(new Congthuc("3", "bún","lao", "http://anh.eva.vn/upload/2-2016/images/2016-06-12/5-mon-an-ngon-tuyet-cho-cuoi-tuan-5-1465696055-width640height80.jpg"));
mItems.add(new Congthuc("4", "phỏ", "lao","https://i0.wp.com/theboola.com/wp-content/uploads/2014/09/144837125.jpg?resize=506%2C341"));
mItems.add(new Congthuc("5", "hủ tíu","tay", "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS_AWhsxDkMDqkIhPGSNLZj8TFAkF3pedEa6Tt-l72mSt6PdRXaEg"));
mItems.add(new Congthuc("6", "cam","tay", "https://i.imgur.com/3M37NQM.jpg"));
mItems.add(new Congthuc("7", "quýt","ta", "http://vnngon.com/wp-content/uploads/2016/01/cach-lam-mon-an-ngon-ga-nhoi-thap-cam-xoi-vnngon-2.jpg"));
}ArrayList<Congthuc> CountryFillter(String con,List<Congthuc> cts){
List<Congthuc> Ncts = new ArrayList<>();
for (int i = 0;i<cts.size();i++)
{
Congthuc ct = cts.get(i);
if (con.equals(ct.getQuocGia()))
{
Ncts.add(ct);
}
}
return (ArrayList<Congthuc>) Ncts;}}
我的适配器
public class RecyclerDataAdapter extends RecyclerView.Adapter<RecyclerDataAdapter.ViewHolder> {
private List<Congthuc> congthucs;
private int layout;
private Context context;
public RecyclerDataAdapter(Context context, int layout, List<Congthuc> congthucs) {
this.context = context;
this.layout=layout;
this.congthucs = congthucs;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imgCT;
TextView txtCT;
public ViewHolder(View itemView) {
super(itemView);
imgCT= (ImageView) itemView.findViewById(R.id.fragment_image_View);
txtCT= (TextView) itemView.findViewById(R.id.txtDes);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_collection, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Congthuc congthuc = congthucs.get(position);
holder.txtCT.setText(congthuc.getDescription());
Picasso.with(context)
.load(congthuc.getLink())
.placeholder(R.drawable.loader)
.error(R.drawable.placeholder)
.resize(150,120)
.centerCrop()
.skipMemoryCache()
.into(holder.imgCT);
}
@Override
public int getItemCount() {
return congthucs ==null?0:congthucs.size();
}}