在Fragment中显示RecyclerView

时间:2017-05-29 10:30:38

标签: java android android-fragments android-recyclerview android-5.0-lollipop

我想问一下如何在片段

中应用recyclerview

我正在使用BottomNavigationView和Fragment,我想在第一个片段中安装recyclerview,我很困惑,因为它太难了,我尝试了很多次,但它总是一个错误

我希望你们能帮助我

我的ItemOneFragment

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

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


    List<ItemObject> rowListItem = getAllItemList();
    lLayout = new LinearLayoutManager(MainActivity.this);

    RecyclerView rView = (RecyclerView)v.findViewById(R.id.recycler_view);
    rView.setLayoutManager(lLayout);

    RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(MainActivity.this, rowListItem);
    rView.setAdapter(rcAdapter);

    return v;
}
private List<ItemObject> getAllItemList(){

    List<ItemObject> allItems = new ArrayList<ItemObject>();
    allItems.add(new ItemObject("United States", R.drawable.newyork));
    allItems.add(new ItemObject("Canada", R.drawable.canada));
    allItems.add(new ItemObject("United Kingdom", R.drawable.uk));
    allItems.add(new ItemObject("Germany", R.drawable.germany));
    allItems.add(new ItemObject("Sweden", R.drawable.sweden));

    return allItems;
}
}

0 个答案:

没有答案