Rangtian Yu Rangtian Yu - 5 months ago 19
Android Question

Android, Functions in Adapter never get called

Functions like onCreateViewHolder, onBindViewHolder, onAttachedToRecyclerView never get called. This is what I'm trying to do.



  1. Switch from MainActivity to RecyclerViewActivity when Button1 is clicked, and load fragment_recyclerview into RecyclerViewActivity




switch(id) {
case R.id.aboutMeButton:
getSupportFragmentManager().beginTransaction().replace(R.id.mainContainer, AboutMeFragment.newInstance()).addToBackStack(null).commit();
break;
case R.id.task1Button:
intent = new Intent(this, RecyclerViewActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT );
startActivity(intent);
break;
default:
break;
}




  1. FragmentViewActivity.onCreate( )




FragmentViewActivity.onCreate()
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclerview);

getSupportFragmentManager().beginTransaction().replace(R.id.recyclerViewContainer, RecyclerViewFragment.newInstance()).commit();

Log.d("AAA", "RecyclerViewActivity.onCreate( )");
}




  1. RecyclerViewFragment.onCreateView():




RecyclerViewFragment.onCreateView(){
final View rootView = inflater.inflate(R.layout.fragment_recyclerview, container, false);
movieData = new MovieData();

mRecyclerView = (RecyclerView) rootView.findViewById(R.id.cardViewContainer);

// Log.d("AAA", "mRecyclerView = " + mRecyclerView.getId());

// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);

// use a linear layout manager
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);

Log.d("AAA", "getActivity() = " + getActivity().toString());
Log.d("AAA", "movieList size = " + movieData.getMoviesList().size());

// specify an adapter (see also next example)
mAdapter = new MovieDataAdapter(getActivity(), movieData.getMoviesList());
mRecyclerView.setAdapter(mAdapter);

return super.onCreateView(inflater, container, savedInstanceState);
}



  1. Adapter.getItemCount( ) returns 30, however, functions in Adapter other than Constructor never get called, which leaves me a pure blank screen. I've no idea what's going on


Answer

You are setting your View up in rootView and then, instead of returning that View, you return super.onCreateView():

Instead of the following in onCreateView():

return super.onCreateView(inflater, container, savedInstanceState);

You should return the View you just set up:

return rootView;
Comments