当前位置:  开发笔记 > 编程语言 > 正文

在片段中实现RecyclerView

如何解决《在片段中实现RecyclerView》经验,为你挑选了1个好方法。

我试图在AppCompatActivity中的ViewPager中的片段中创建ListView.在AppCompatActivity中,所有视图元素都是CoordinatorLayout中的包装.因为我使用了CoordinatorLayout.我必须使用RecylerView我试图遵循developer.android.com的培训 ,但我的应用程序在我的日志后停止.这是我在myFragment中停止应用程序的代码.

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

//...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_city_list, container, false)

    mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
    mLayoutManager = new LinearLayoutManager(this.getActivity());
    Log.d("debugMode", "The application stopped after this");
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new RecyclerAdapter(getNames());
    mRecyclerView.setAdapter(mAdapter);
    return view;
}

//...

Mohammed Aou.. 10

用这个

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_city_list, container, false)

    // Replace 'android.R.id.list' with the 'id' of your RecyclerView
    mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
    mLayoutManager = new LinearLayoutManager(this.getActivity());
    Log.d("debugMode", "The application stopped after this");
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new RecyclerAdapter(getNames());
    mRecyclerView.setAdapter(mAdapter);
    return view;
}

你应该在上面调用setLayoutManager&setAdaptermethods(分别)Recyclerview.

加,

mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);

你不应该使用android.R.id.list,因为你没有使用ListFragment.将其替换id为您Recyclerview(如在XML布局中).



1> Mohammed Aou..:

用这个

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_city_list, container, false)

    // Replace 'android.R.id.list' with the 'id' of your RecyclerView
    mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);
    mLayoutManager = new LinearLayoutManager(this.getActivity());
    Log.d("debugMode", "The application stopped after this");
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new RecyclerAdapter(getNames());
    mRecyclerView.setAdapter(mAdapter);
    return view;
}

你应该在上面调用setLayoutManager&setAdaptermethods(分别)Recyclerview.

加,

mRecyclerView = (RecyclerView) view.findViewById(android.R.id.list);

你不应该使用android.R.id.list,因为你没有使用ListFragment.将其替换id为您Recyclerview(如在XML布局中).

推荐阅读
女女的家_747
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有