使用片段

时间:2018-04-23 05:41:18

标签: java android user-interface

我正在创建一个应用,我正在尝试通过观看此tutorial来制作多个水平滚动回收视图, 但是我正在使用片段,当我在片段中为createDummyData添加一个方法时,当我在createDummyData方法中使用该对象时,它显示了arraylist对象的错误 这是HomeFragment的代码

我通过观看教程正确实现了所有其他内容,但由于我对片段知之甚少,我不知道哪里出错了。请不要理解任何帮助。

public class HomeFragment extends Fragment {

    LadderLayoutManager llm;
    RecyclerView.LayoutManager multi_scroll_layout_manager;
    RecyclerView rcv,multi_scroll_recyclerview;
    HSAdapter adapter;
    RecyclerView.Adapter multi_scroll_adapter;
    int scrollToPosition;
    public HomeFragment() {
        // Required empty public constructor
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        createDummyData();
        int[] images = {R.drawable.vancouver,R.drawable.party,R.drawable.hands_ip,R.drawable.dj};
        // Inflate the layout for this fragment
        ArrayList<SectionDataModel> allSampleData;
        allSampleData = new ArrayList<SectionDataModel>();

        HorizontalAdapter firstAdapter = new HorizontalAdapter(images);
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        llm = new LadderLayoutManager(1.5f, 0.85f, LadderLayoutManager.HORIZONTAL).
                setChildDecorateHelper(new LadderLayoutManager
                        .DefaultChildDecorateHelper(getResources().getDimension(R.dimen.item_max_elevation)));
        llm.setChildPeekSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                30, getResources().getDisplayMetrics()));
        llm.setMaxItemLayoutCount(5);

        rcv = (RecyclerView) view.findViewById(R.id.rcv);
        rcv.setLayoutManager(llm);

        new LadderSimpleSnapHelper().attachToRecyclerView(rcv);
        adapter = new HSAdapter();
        rcv.setAdapter(adapter);

        multi_scroll_recyclerview = (RecyclerView)view.findViewById(R.id.multi_scroll_recyclerview);
        multi_scroll_layout_manager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
        multi_scroll_recyclerview.setLayoutManager(multi_scroll_layout_manager);
        multi_scroll_adapter = new RecyclerViewDataAdapter(getActivity(),allSampleData);
        multi_scroll_recyclerview.setAdapter(multi_scroll_adapter);
        multi_scroll_recyclerview.setHasFixedSize(true);






       /* MultiSnapRecyclerView firstRecyclerView = (MultiSnapRecyclerView)view.findViewById(R.id.first_recycler_view);
        LinearLayoutManager firstManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        firstRecyclerView.setLayoutManager(firstManager);
        firstRecyclerView.setAdapter(firstAdapter);

        HorizontalAdapter secondAdapter = new HorizontalAdapter(images);
        MultiSnapRecyclerView secondRecyclerView =(MultiSnapRecyclerView) view.findViewById(R.id.second_recycler_view);
        LinearLayoutManager secondManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        secondRecyclerView.setLayoutManager(secondManager);
        secondRecyclerView.setAdapter(secondAdapter);

        HorizontalAdapter thirdAdapter = new HorizontalAdapter(images);
        MultiSnapRecyclerView thirdRecyclerView = (MultiSnapRecyclerView)view.findViewById(R.id.third_recycler_view);
        LinearLayoutManager thirdManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        thirdRecyclerView.setLayoutManager(thirdManager);
        thirdRecyclerView.setAdapter(thirdAdapter);*/


        return view;

    }
    public void createDummyData() {
        for (int i = 1; i <= 5; i++) {

            SectionDataModel dm = new SectionDataModel();

            dm.setHeaderTitle("Section " + i);

            ArrayList<SingleItemModel> singleItem = new ArrayList<SingleItemModel>();
            for (int j = 0; j <= 5; j++) {
                singleItem.add(new SingleItemModel("Item " + j, "URL " + j));
            }

            dm.setAllItemsInSection(singleItem);

            allSampleData.add(dm);//line which is causing issue

        }
    }

}

2 个答案:

答案 0 :(得分:0)

尝试换行  ArrayList<SectionDataModel> allSampleData;public class HomeFragment extends Fragment {

或只是粘贴这个

public class HomeFragment extends Fragment {

ArrayList<SectionDataModel> allSampleData;


LadderLayoutManager llm;
RecyclerView.LayoutManager multi_scroll_layout_manager;
RecyclerView rcv,multi_scroll_recyclerview;
HSAdapter adapter;
RecyclerView.Adapter multi_scroll_adapter;
int scrollToPosition;
public HomeFragment() {
    // Required empty public constructor
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    int[] images = {R.drawable.vancouver,R.drawable.party,R.drawable.hands_ip,R.drawable.dj};
    // Inflate the layout for this fragment
           allSampleData = new ArrayList<SectionDataModel>();

            createDummyData();


    HorizontalAdapter firstAdapter = new HorizontalAdapter(images);
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    llm = new LadderLayoutManager(1.5f, 0.85f, LadderLayoutManager.HORIZONTAL).
            setChildDecorateHelper(new LadderLayoutManager
                    .DefaultChildDecorateHelper(getResources().getDimension(R.dimen.item_max_elevation)));
    llm.setChildPeekSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
            30, getResources().getDisplayMetrics()));
    llm.setMaxItemLayoutCount(5);

    rcv = (RecyclerView) view.findViewById(R.id.rcv);
    rcv.setLayoutManager(llm);

    new LadderSimpleSnapHelper().attachToRecyclerView(rcv);
    adapter = new HSAdapter();
    rcv.setAdapter(adapter);

    multi_scroll_recyclerview = (RecyclerView)view.findViewById(R.id.multi_scroll_recyclerview);
    multi_scroll_layout_manager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
    multi_scroll_recyclerview.setLayoutManager(multi_scroll_layout_manager);
    multi_scroll_adapter = new RecyclerViewDataAdapter(getActivity(),allSampleData);
    multi_scroll_recyclerview.setAdapter(multi_scroll_adapter);
    multi_scroll_recyclerview.setHasFixedSize(true);






   /* MultiSnapRecyclerView firstRecyclerView = (MultiSnapRecyclerView)view.findViewById(R.id.first_recycler_view);
    LinearLayoutManager firstManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
    firstRecyclerView.setLayoutManager(firstManager);
    firstRecyclerView.setAdapter(firstAdapter);

    HorizontalAdapter secondAdapter = new HorizontalAdapter(images);
    MultiSnapRecyclerView secondRecyclerView =(MultiSnapRecyclerView) view.findViewById(R.id.second_recycler_view);
    LinearLayoutManager secondManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
    secondRecyclerView.setLayoutManager(secondManager);
    secondRecyclerView.setAdapter(secondAdapter);

    HorizontalAdapter thirdAdapter = new HorizontalAdapter(images);
    MultiSnapRecyclerView thirdRecyclerView = (MultiSnapRecyclerView)view.findViewById(R.id.third_recycler_view);
    LinearLayoutManager thirdManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
    thirdRecyclerView.setLayoutManager(thirdManager);
    thirdRecyclerView.setAdapter(thirdAdapter);*/


    return view;

}
public void createDummyData() {
    for (int i = 1; i <= 5; i++) {

        SectionDataModel dm = new SectionDataModel();

        dm.setHeaderTitle("Section " + i);

        ArrayList<SingleItemModel> singleItem = new ArrayList<SingleItemModel>();
        for (int j = 0; j <= 5; j++) {
            singleItem.add(new SingleItemModel("Item " + j, "URL " + j));
        }

        dm.setAllItemsInSection(singleItem);

        allSampleData.add(dm);//line which is causing issue

    }
  }

}

希望这会有所帮助:)

答案 1 :(得分:0)

在这里,您首先使用数组列表,然后在之后声明它会导致问题。

如果你看一下&#34; onCreateView&#34;功能你可以看到你正在调用&#34; createDummyData&#34;在创建&#34; allSampleData&#34;之前的功能在此函数中使用的array-list。

要解决此问题,您只需调用&#34; createDumyData&#34;创建&#34; allSampleData&#34;之后的功能阵列列表。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
   // createDummyData();
    int[] images = {R.drawable.vancouver,R.drawable.party,R.drawable.hands_ip,R.drawable.dj};
    // Inflate the layout for this fragment
           allSampleData = new ArrayList<SectionDataModel>();

   createDummyData();  // after Array-list