ListFragment does not accept my layout

前端 未结 10 1387
情歌与酒
情歌与酒 2020-12-01 02:09

According to this link: ListFragment android developers

I want to set my custom layout for list, but it makes exceptions.

Here is the code:

p         


        
10条回答
  •  时光说笑
    2020-12-01 02:47

    Just replace the original ListView with your CustomListView Layout within the onCreateView method. Worked for me.

        @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View layout = super.onCreateView(inflater, container,
                savedInstanceState);
        ListView lv = (ListView) layout.findViewById(android.R.id.list);
        ViewGroup parent = (ViewGroup) lv.getParent();
    
        // Remove ListView and add CustomView  in its place
        int lvIndex = parent.indexOfChild(lv);
        parent.removeViewAt(lvIndex);
        LinearLayout mLinearLayout = (LinearLayout) inflater.inflate(
                R.layout.custom_list, container, false);
        parent.addView(mLinearLayout, lvIndex, lv.getLayoutParams());
        return layout;
    }
    

提交回复
热议问题