0

我的問題是,當我想打電話給RecyclerViewNavigationDrawer和物品(陣列)Fragment我打電話通過RecyclerView不叫,但是當我不投入Fragment它可以被稱爲。所以這是我的代碼。Android的 - 在導航中的片段抽屜調用RecyclerView

這是我Fragment.java和它說recyclerView.setLayoutManager(layoutManager);

public class fragment_1bakso extends Fragment { 

private AppCompatActivity compat; 
private RecyclerView recyclerView; 
private MakananAdapter adapter; 
public fragment_1bakso() { 
    // Required empty public constructor 
} 

ArrayList<Makanan> mList = new ArrayList<>(); 
MakananAdapter mAdapter; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    /*View v = inflater.inflate(R.layout.fragment_1bakso, container, false); 
    return v;*/ 
    View rootview = inflater.inflate(R.layout.activity_calling, container, false); 
    return rootview; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //mClass.setContentView(R.layout.activity_calling); 
    //return new fragment_1bakso(R.layout.activity_calling, this); 


    GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2, GridLayoutManager.HORIZONTAL, false); 
    recyclerView.setLayoutManager(layoutManager); 

    mAdapter = new MakananAdapter(mList); 
    recyclerView.setAdapter(mAdapter); 


    fillData(); 
} 

private void fillData() { 
    Resources resources = getResources(); 
    String [] arJudul = resources.getStringArray(R.array.bakso); 
    String [] arHarga = resources.getStringArray(R.array.bakso_price); 
    String [] arDeskripsi = resources.getStringArray(R.array.bakso_desc); 
    String [] arLokasi = resources.getStringArray(R.array.bakso_locations); 
    String [] arDetail = resources.getStringArray(R.array.bakso_details); 
    TypedArray a = resources.obtainTypedArray(R.array.bakso_pict); 
    Drawable[] arFoto = new Drawable[a.length()]; 

    for (int i = 0; i < arFoto.length; i++) 
    { 
     arFoto[i] = a.getDrawable(i); 
    } 
    a.recycle(); 

    for (int i = 0; i < arJudul.length; i++) 
    { 
     mList.add(new Makanan(arJudul[i],arHarga[i],arFoto[i],arDeskripsi[i],arDetail[i],arLokasi[i])); 
    } 
    mAdapter.notifyDataSetChanged(); 

}} 

這裏的錯誤是我的Fragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="id.sch.smktelkom_mlg.learn.sideview.fragment_1bakso"> 

    <!-- TODO: Update blank fragment layout --> 
    <include 
     layout="@layout/activity_calling" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</FrameLayout> 

這是我Activity_calling.xml是用於調用數組中的項目(RecyclerView)

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.v7.widget.RecyclerView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/recyclerView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clipToPadding="false" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:scrollbars="vertical" 
    tools:context="id.sch.smktelkom_mlg.learn.sideview.activity_1bakso" /> 

幫幫我吧,感謝advanc即

+0

嘗試使用LinearLayoutManager更改GridLayoutManager –

+0

將您的代碼從'onCreate()'移動到'onViewCreated()' –

回答

2

您還沒有初始化您的recyclerView。

recyclerView = (RecyclerView) view.findViewById(R.id.your_id); 

也將代碼從onCreate()移動到onViewCreated();

+0

非常感謝,這是完美的工作 –

0

如果您需要網格佈局管理器,你可以試試這個

GridLayoutManager layoutManager = new GridLayoutManager(this, 1); 

或者你可以去一個水平線性佈局管理器,像這樣

LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false) 

或者你可以去一個垂直線性佈局經理像這樣

LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false) 
+0

它說不能應用到'this' –

0
final RecyclerView.LayoutManager mLayoutManager =new GridLayoutManager(getContext(), 1); 
    recyclerView_drawer.setLayoutManager(mLayoutManager); 
    recyclerView_drawer.setItemAnimator(new DefaultItemAnimator()); 
    recyclerView_drawer.setAdapter(drawerAdapter); 

希望這段代碼能幫助你。你也可以用相對佈局替換幀佈局,正如你在幀佈局中所說的那樣它不工作。

+0

但recyclerview_drawer中有什麼? –

+0

recyclerView_drawer是我的Recyclerview對象,它是您的一個recyclerView。 –

0

你沒有初始化你recyclerview這是可以做到:

RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview); 

,並定義網格佈局管理器爲:

GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2); 
    recyclerView .setHasFixedSize(true); 
    recyclerView .setItemAnimator(new DefaultItemAnimator()); 
    recyclerView .setLayoutManager(gridLayoutManager); 

而且是從的onCreate移動你的代碼onViewCreated爲:

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
} 

希望它有助於..快樂編碼:)