片段类中的“无法应用于”

时间:2017-08-14 12:31:10

标签: java android android-layout android-fragments swiperefreshlayout

我在APP中创建了4个tablayouts,每个都有一个布局源文件和类。我还想在这个片段中添加一个SwipeRefreshLayout。 但是当我添加swipeRefreshLayout.setOnRefreshListener(getActivity());时出现了问题 swipeRefreshLayout.setOnRefreshListener(this);也不起作用..  error

片段:

public class tab_ask extends Fragment {
    public CoordinatorLayout coordinatorLayout;
    public boolean isConnected;
    public static final String NA = "NANA";
    public List<Post> postList;
    public RecyclerView recycler_post;
    public recadapter adapter;
    public SwipeRefreshLayout swipeRefreshLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.tab4ask, container, false);
        recycler_post = (RecyclerView) getActivity().findViewById(R.id.recycler_post);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recycler_post.setLayoutManager(layoutManager);
        recycler_post.setItemAnimator(new DefaultItemAnimator());
        swipeRefreshLayout = (SwipeRefreshLayout) getActivity().findViewById(R.id.swipe_refresh);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorAccent));
        getData();

        return rootView;
    }

}

1 个答案:

答案 0 :(得分:3)

如果您想将其设置为侦听器,则您的片段应实现android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener