【问题标题】:Implement a RecyclerView in a Fragment在 Fragment 中实现 RecyclerView
【发布时间】:2015-12-15 00:10:41
【问题描述】:

我试图在 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;
}

//...

【问题讨论】:

  • 可以发布崩溃报告吗?

标签: java android android-fragments android-recyclerview


【解决方案1】:

使用这个

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

您应该在Recyclerview 上调用setLayoutManagersetAdapter 方法(分别)。

另外,

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

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

【讨论】:

  • 哇谢谢!很晚了,我没有看到 R.id.list 没有引用我的列表!我被困了好几个小时!
  • 我有一个问题 getNames 来自哪里?
  • getNames() 是一种返回数据列表以填充RecyclerView的方法
【解决方案2】:

您是否将 android.R.id.list 设置为 recyclerview id?否则你应该在 xml 文件中为 recyclerview 设置一个 id,像这样:

android:id="@+id/recyclerview"

并改变这一行

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

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

【讨论】:

    【解决方案3】:
    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;
     }
    }
    

    将回收适配器添加到 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>
    

    【讨论】:

      猜你喜欢
      • 2020-08-27
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-22
      • 1970-01-01
      相关资源
      最近更新 更多