无法解析可扩展的Recyclerview findViewById(int)

时间:2017-12-10 16:50:15

标签: android android-fragments expandablelistadapter findviewbyid

错误findViewById(int)无法解析方法

尝试在片段中实现

自定义RecyclerView,允许将可扩展视图附加到每个ViewHolder

有没有办法在展开视图中点击操作

perl -ne 'if (/^#/) { push @buffer, $_ }
          else {
              print @buffer, $_ if /API/;
              @buffer = ();
          }' -- file

尝试实现可扩展的Recyclerview

错误 :( 34,37)错误:找不到符号方法findViewById(int)

1 个答案:

答案 0 :(得分:1)

findViewById不是Fragment方法,因此无法在其onCreate中解析。你不能在活动中做到。

您必须在onCreateViewonViewCreated方法中使用它,并且必须在充气的视图上调用它。

例如:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    //you can set the title for your toolbar here for different fragments different titles
    getActivity().setTitle("Syllabus");

    recyclerView =(RecyclerView) view.findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    adapter adapter =new adapter(getActivity(),initData());
    adapter.setParentClickableViewAnimationDefaultDuration();
    adapter.setParentAndIconExpandOnClick(true);

    recyclerView.setAdapter(adapter)
}

或..

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    //returning our layout file
    //change R.layout.yourlayoutfilename for each of your fragments
    View rootView = inflater.inflate(R.layout.fragment_sy, container, false);

    recyclerView =(RecyclerView) rootView.findViewById(R.id.recyclerview);

     ...
    return rootView;
}