Android - 在导航抽屉的片段中调用RecyclerView

时间:2017-02-03 03:26:35

标签: java android android-fragments android-recyclerview android-drawer

我的问题是,我想将RecyclerView调用Fragment NavigationDrawer RecyclerView和我通过Fragment调用的项目(数组)未调用,但是当我不调用时放入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

以下是用于调用数组中项目的 <?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" /> (RecyclerView)

var aa = [[-122.490402, 37.786453],[-149.882154, 61.138793]];
console.log(aa[0]);
console.log(aa[1]);

var breweryCircles = [];
for (i=0;i<=aa.length;i++) {
    breweryCircles.push(aa[i]);


    g.selectAll("circle")
        .data(breweryCircles)
        .enter()
        .append('circle')
        .attr("cx", function (d) { return projection(breweryCircles)[0]; })
        .attr("cy", function (d) { return projection(breweryCircles)[1]; })
        .attr("r", "2px")
        .attr("fill", "red")            
};

请提前帮助我们。

4 个答案:

答案 0 :(得分:2)

您尚未初始化recyclerView。

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

同样将代码从onCreate()移动到onViewCreated();

答案 1 :(得分:0)

如果您需要网格布局管理器,可以试试这个

GridLayoutManager layoutManager = new GridLayoutManager(this, 1);

或者您可以选择像这样的水平线性布局管理器

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

或者您可以选择像这样的垂直线性布局管理器

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

答案 2 :(得分:0)

<activity
    .....
    android:noHistory="true"
    ...../>

希望这段代码可以帮到你。你也可以用相对布局替换框架布局,正如你在框架布局中说的那样它不起作用。

答案 3 :(得分: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);
}

希望它有所帮助......快乐的编码:)