2015-08-16 27 views
0

我試圖將cardview作爲片段。我從應用程序中得到的是,片段上幾乎沒有卡片視圖。通過點擊任何片段,它將啓動特定的活動。將CardView作爲片段執行

但是,在將原來的活動源代碼轉換成我想要的活動的過程中,一個片段。發生錯誤。

之一是,

錯誤:(86,39)誤差:發現LinearLayoutManager沒有合適的構造(CardViewFragment) 構造LinearLayoutManager.LinearLayoutManager(上下文,整型,布爾值)是不適用 (實際和正式參數列表的長度不同) 構造LinearLayoutManager.LinearLayoutManager(上下文)是不適用 (實際參數CardViewFragment不能通過方法調用轉換)轉換爲上下文

這是我的轉換編碼的源代碼,除了已被註釋掉之外,其中也包含相同的原始代碼。

package info.androidhive.materialdesign.activity; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentActivity; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.widget.CardView; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.support.v7.widget.Toolbar; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.LinearLayout; 

import java.util.ArrayList; 

import info.androidhive.materialdesign.R; 


public class CardViewFragment extends Fragment { 

    private Toolbar toolbar; 
    private RecyclerView recyclerView; 
    private CardView cardView; 

    private ArrayList<FeddProperties> os_versions; 

    private RecyclerView.Adapter mAdapter; 
    // private RecyclerView.LayoutManager mLayoutManager; 

    //||Before 
    /*protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     initContrls(); 
    }*/ 

    private LinearLayout llLayout; 
    private FragmentActivity faActivity; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     faActivity = (FragmentActivity) super.getActivity(); 
     llLayout = (LinearLayout) inflater.inflate(R.layout.activity_main, container, false); 

     initContrls(); 

     return llLayout; 
    } 

    private void initContrls() { 

     // toolbar = (Toolbar) findViewById(R.id.toolbar); 
     // cardView = (CardView) findViewById(R.id.cardList); 
     recyclerView = (RecyclerView) llLayout.findViewById(R.id.my_recycler_view); 

     //if (toolbar != null) { 
     // setSupportActionBar(toolbar); 
     //getSupportActionBar().setTitle("Android Versions"); 

     //} 

     final String[] versions = {"Alpha", "Beta", "CupCake", "Donut", 
       "Eclair", "Froyo", "Gingerbread", "Honeycomb", 
       "Ice Cream Sandwitch", "JellyBean", "KitKat", "LollyPop"}; 
     //final int[] icons = {R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.donut, R.drawable.eclair, R.drawable.froyo, R.drawable.gingerbread, R.drawable.honeycomb, R.drawable.icecream_sandwhich, R.drawable.jellybean, R.drawable.kitkat, R.drawable.lollipop}; 


     os_versions = new ArrayList<FeddProperties>(); 

     for (int i = 0; i < versions.length; i++) { 
      FeddProperties feed = new FeddProperties(); 

      feed.setTitle(versions[i]); 
      //feed.setThumbnail(icons[i]); 
      os_versions.add(feed); 
     } 

     recyclerView.setHasFixedSize(true); 

     // ListView 
     recyclerView.setLayoutManager(new LinearLayoutManager(this)); 

     //Grid View 
     // recyclerView.setLayoutManager(new GridLayoutManager(this,2,1,false)); 

     //StaggeredGridView 
     // recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,1)); 

     // create an Object for Adapter 
     mAdapter = new CardViewDataAdapter(os_versions); 

     // set the adapter object to the Recyclerview 
     recyclerView.setAdapter(mAdapter); 


    } 
} 

你們能協助嗎?

+0

讀取錯誤。這個構造函數'新的LinearLayoutManager(this)'不存在。閱讀文檔以找到適合您情況的構造函數 – njzk2

回答

0

構造函數LinearLayoutManager期待Context實例。使用的典型上下文將是您的活動:

recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));