Java 在 Fragment 中实现 RecyclerView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34278892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:29:43  来源:igfitidea点击:

Implement a RecyclerView in a Fragment

javaandroidandroid-fragmentsandroid-recyclerview

提问by user3432681

I trying to create a ListView in a Fragment within a ViewPager in a AppCompatActivity. In the AppCompatActivity are all view elements are wrappend in a CoordinatorLayout. Because I used the CoordinatorLayout. I have to use a RecylerView I am trying to follow the training from developer.android.com, but my application stopped after my log. This is my code in myFragment where the applications stopped.

我试图在 AppCompatActivity 的 ViewPager 中的 Fragment 中创建一个 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;
}

//...

采纳答案by Mohammed Aouf Zouag

Use this

用这个

@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;
}

You should call the setLayoutManager& setAdaptermethods (respectively) on the Recyclerview.

你应该调用setLayoutManagersetAdapter方法(分别为上)Recyclerview

Plus,

加,

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

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

you should not use android.R.id.list, as you're not using a ListFragment. Replace it with the idof you Recyclerview(as in your XML layout).

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

回答by Daniel Martin Shum

did you set the android.R.id.list as the recyclerview id? otherwise you should set an id to the recyclerview in the xml file, like this:

您是否将 android.R.id.list 设置为 recyclerview id?否则,您应该在 xml 文件中为 recyclerview 设置一个 id,如下所示:

android:id="@+id/recyclerview"

and change this line

并改变这一行

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

to

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

回答by sanjay

public class HomeFragment extends Fragment {

private HomeViewModel homeViewModel;

public View onCreateView(@NonNull LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState) {
    homeViewModel =
            ViewModelProviders.of(this).get(HomeViewModel.class);
    View root = inflater.inflate(R.layout.fragment_home, container, false);
    final TextView textView = root.findViewById(R.id.text_home);
    homeViewModel.getText().observe(this, new Observer<String>() {
        @Override
        public void onChanged(@Nullable String s) {
            textView.setText(s);
        }
    });
     FragmentActivity c = getActivity();
     RecyclerView recyclerView = root.findViewById(R.id.my_recycler_view);
    recyclerView.setLayoutManager(new GridLayoutManager(c, 2));

    List<VillageModel> villageModels=new ArrayList<>();
    VillageModel villageModel =new VillageModel("",0,0,0,0,0,0);
    villageModels.add(villageModel);
    VillageModel villageModel1 =new VillageModel("",0,0,0,0,0,0);
    VillageModel villageModel2 =new VillageModel("",0,0,0,0,0,0);
    villageModels.add(villageModel1);
    villageModels.add(villageModel2);
    VillageAdapter adapter = new VillageAdapter(c,villageModels);
    recyclerView.setAdapter(adapter);
    return root;
 }
}

add the recycle adapter to the oncreateView fragment home xml structure

将回收适配器添加到 oncreateView 片段主页 xml 结构

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    />
</RelativeLayout>