2015-08-16 80 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()));