小编Bra*_*tma的帖子

LinearLayoutManager 不能在 Fragment 中使用

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

error: incompatible types: NotificationFragment cannot be converted to Context
Run Code Online (Sandbox Code Playgroud)

这是我的代码 NotificationFragment.java

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", …
Run Code Online (Sandbox Code Playgroud)

java android-studio

4
推荐指数
1
解决办法
5696
查看次数

标签 统计

android-studio ×1

java ×1