2016-06-15 96 views
1

onCreateViewHolder()onBindViewHolder(),onAttachedToRecyclerView()這樣的函數永遠不會被調用。這是我想要做的。Android,適配器中的函數永遠不會被調用

  1. 交換機從MainActivityRecyclerViewActivity當點擊Button1,和負載fragment_recyclerviewRecyclerViewActivity
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; 
    } 
  • 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()"); 
    } 
    
  • 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); 
    } 
    
  • Adapter.getItemCount()返回30,但是,功能在構造函數以外的其他構造函數永遠不會被調用,這爲我留下了一個純粹的空白屏幕 。我不知道這是怎麼回事
  • +1

    看看'onCreateView()'方法中的'return'語句。你沒有返回你剛纔設置的所有東西。 –

    回答

    0

    你設置你View起來rootView然後,而不是返回該View,返回super.onCreateView()

    而是在onCreateView()如下:

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

    您應該返回View您只需設置:

    return rootView; 
    
    +0

    OMG ...我熬夜想弄清楚什麼是錯......你是我的英雄!!!!! – Rangtian

    0

    第一:

    • 不做適配器的任何初始化在Fragment.onCreateView(,,,) 作爲名字說此方法應用於膨脹或創建視圖:

    所以膨脹視圖:

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
        return inflater.inflate(R.layout.recycler_layout, container, false); 
    } 
    

    後ü創建視圖使用onViewCreated(..)方法來初始化和創建適配器:

    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState){ 
        // here initialize recycler view 
        super.onViewCreated(view, savedInstanceState); 
    } 
    

    和user13說你需要返回充氣視圖不是超級創建的視圖!!!!*

    然後ü將避免這樣的問題

    第二:

    • 取決於片段/父組件的使用(例如當u實現與狀態或正常視圖尋呼機應該使用不同的方法適配器,或者你保留片段)有時你可以或有時不能持有參考膨脹的看法(因此返回或不是一旦創建視圖) - 但我傷心這取決於你使用什麼方法
    +0

    謝謝你的幫助。我錯誤地返回super.onCreateView()而不是像user13所述的rootView。現在問題解決了 – Rangtian

    相關問題