LinearLayoutManager нельзя использовать во фрагменте - PullRequest
0 голосов
/ 02 января 2019

У меня ошибка на моем LinearLayoutManager, когда я запускаю этот код, он говорит:

error: incompatible types: NotificationFragment cannot be converted to Context

Вот мой код 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", "Surabaya"));
        notificationModelArrayList.add(new NotificationModel("Event 3", "1 January 2019", "Surabaya"));
        notificationModelArrayList.add(new NotificationModel("Event 4", "1 January 2019", "Surabaya"));
    }

}

Ошибка включена

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

Надеюсь, вы сможете мне помочь, спасибо.

Ответы [ 2 ]

0 голосов
/ 02 января 2019

Используйте getActivity () вместо NotificationFragment.this

RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
0 голосов
/ 02 января 2019

Вы не можете привести Fragment к Context.

Редактировать эту строку:

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

к:

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());

или

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());

Не забудьте проверить нулевое значение для getContext() или getActivity() перед его использованием.

Для вашего нового вопроса из комментария ниже просто перенесите оператор return в конец функции:

@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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...