首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LinearLayoutManager不能在片段中使用

LinearLayoutManager不能在片段中使用
EN

Stack Overflow用户
提问于 2019-01-02 17:33:53
回答 2查看 6.3K关注 0票数 4

我的LinearLayoutManager上有一个错误,当我运行这段代码时,它说

代码语言:javascript
复制
error: incompatible types: NotificationFragment cannot be converted to Context

下面是我的代码NotificationFragment.java

代码语言:javascript
复制
public class NotificationFragment extends Fragment {

    private RecyclerView recyclerView;
    private NotificationAdapter adapter;
    private ArrayList<NotificationModel> notificationModelArrayList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_notification,container,false);

        addData();

        recyclerView = (RecyclerView)   getView().findViewById(R.id.recycler_view);

        adapter = new NotificationAdapter(notificationModelArrayList);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(NotificationFragment.this);

        recyclerView.setLayoutManager(layoutManager);

        recyclerView.setAdapter(adapter);
    }

    void addData(){
        notificationModelArrayList = new ArrayList<>();
        notificationModelArrayList.add(new NotificationModel("Event 1", "1 January 2019", "Surabaya"));
        notificationModelArrayList.add(new NotificationModel("Event 2", "1 January 2019", "Surabaya"));
        notificationModelArrayList.add(new NotificationModel("Event 3", "1 January 2019", "Surabaya"));
        notificationModelArrayList.add(new NotificationModel("Event 4", "1 January 2019", "Surabaya"));
    }

}

错误亮起

代码语言:javascript
复制
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(NotificationFragment.this);

希望你能帮助我,谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-02 17:46:57

不能将Fragment转换为Context

编辑此行:

代码语言:javascript
复制
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(NotificationFragment.this);

至:

代码语言:javascript
复制
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());

代码语言:javascript
复制
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());

记住在使用之前检查getContext()getActivity()是否为null。

对于下面评论中的新问题,只需将返回语句放在函数的末尾:

代码语言:javascript
复制
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view =  inflater.inflate(R.layout.fragment_notification,container,false);

    addData();

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

    adapter = new NotificationAdapter(notificationModelArrayList);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(NotificationFragment.this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);

    return view;
}
票数 9
EN

Stack Overflow用户

发布于 2019-01-02 17:47:11

使用getActivity()而不是NotificationFragment.this

代码语言:javascript
复制
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54003985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档