Android在片段内充气多个布局

时间:2015-02-01 02:52:07

标签: android android-layout android-fragments

我正在片段中工作。调用片段时,它会使布局膨胀。哪一切都有效。

 v = createTreeView( inflater,  container,  savedInstanceState);

在我的操作栏上,我有一个搜索按钮,Onclicks会更改布局

btnSearch.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {
             v = createFliterLivstView( linflate, VContainer);
             count = 1;

            Log.i("Create","creating tree view kind of ");
        }
});

两个视图分开工作,但当我将createFilterListView移动到OnClick函数时,它什么都不做。已经处理了日志,这意味着它正在运行。它只是不夸大布局。

以下是onCreateView

@Override
public View onCreateView(  LayoutInflater inflater,   ViewGroup container,  final  Bundle savedInstanceState) {
     linflate = inflater;
     VContainer  =  container;  


    m_Context = inflater.getContext();
    thisApp = (ThisApp)getActivity().getApplicationContext();

    v = createTreeView( inflater,  container,  savedInstanceState);

     FragmentActivity actionBar = getActivity();
     View d =  actionBar.getActionBar().getCustomView().findViewById(R.id.btn_search);
     ImageButton btnSearch = (ImageButton)d.findViewById(R.id.btn_search);


     btnSearch.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {
             v = createFliterLivstView( linflate, VContainer);
             count = 1;

            Log.i("Create","creating tree view kind of ");
        }
    });

     View b =  actionBar.getActionBar().getCustomView().findViewById(R.id.btn_cancel_search);
     ImageButton btnCancelSearch = (ImageButton)b.findViewById(R.id.btn_cancel_search);
     btnCancelSearch.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {

                 v = createTreeView( linflate,  VContainer,  savedInstanceState);
        }
    });



    return v;
}

非常感谢任何帮助。先感谢您。

private View  createFliterLivstView( LayoutInflater inflater, ViewGroup container){
    View v1 = linflate.inflate(R.layout.list_main, VContainer, false);


    historyContainer = (FrameLayout)v1.findViewById(R.id.history_container_layout);
     FragmentActivity actionBar = getActivity();
     View d =  actionBar.getActionBar().getCustomView().findViewById(R.id.txtSearch);

    EditText filterEditText = (EditText)d.findViewById(R.id.txtSearch);

    filterEditText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            Log.i("anish", s.toString());
            historyContainer.removeAllViews();
            final List<String> tempHistoryList = new ArrayList<String>();
            tempHistoryList.addAll(historyList);

            for(String data : historyList) {
                if(data.indexOf(s.toString()) == -1) {
                    tempHistoryList.remove(data);
                }
            }

            viewStub = new ViewStub(getActivity().getBaseContext(), R.layout.history_schedule);

            viewStub.setOnInflateListener(new ViewStub.OnInflateListener() {

                @Override
                public void onInflate(ViewStub stub, View inflated) {
                    setUIElements(inflated, tempHistoryList);
                }
            });

            historyContainer.addView(viewStub);
            viewStub.inflate();
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

        }
    });
    setViewStub();
    return v1;
}

以下是CreateTreeView

private View createTreeView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.layout_list_symptoms, container, false);
    makeList();

    Button btnNext = (Button)v.findViewById(R.id.btn_next);
    btnNext.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View arg0) {

            String bsafe = thisApp.app_pref.getString("BSAFE", "0");
            if(bsafe==null || bsafe.equalsIgnoreCase("0")) {
                thisApp.app_edit.putString("SYMTOMS", "");
                thisApp.app_edit.commit();
            }

            Set<Long> sel = fancyAdapter.selected;
            Iterator<Long> it = sel.iterator();
            boolean hasDone = false;
            int i = 0;
            while(it.hasNext()) {

                Long longId = it.next();
                //Log.i(TAG, "...id = " + longId);

                final Integer[] hierarchy = fancyAdapter.getManager().getHierarchyDescription(longId);

                // 5678
                WorkVO vo = list.get(longId.intValue());

                try {



                     pnObj.put("value" , jsonArr.put(vo.getName()) );


                    //i++;
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                hasDone = true;   
            }

            jsonSymptoms.put(pnObj);





            Log.i("Symtoms ", "      json="+ jsonSymptoms.toString());
            thisApp.app_edit.putString("SYMTOMS", jsonSymptoms.toString());
            thisApp.app_edit.commit();  

            if(hasDone) {
                if(bsafe!=null && bsafe.equalsIgnoreCase("1")) {
                    mCallback.onNextWork(DefinedConstants.FRAG_BSAFE_REPORT);
                } else {
                    mCallback.onNextWork(DefinedConstants.FRAG_FATIGUE_RATING);
                }
            } else {
                Toast.makeText(getActivity(), "Please make a selection", 0).show();
            }
        }
    });

    TreeType newTreeType = null;
    boolean newCollapsible;
    if (savedInstanceState == null) {
        manager = new InMemoryTreeStateManager<Long>();
        final TreeBuilder<Long> treeBuilder = new TreeBuilder<Long>(manager);
        for (int i = 0; i < NODES.length; i++) {
            treeBuilder.sequentiallyAddNextNode((long) i, NODES[i]);
        }
        //Log.d(TAG, manager.toString());
        newTreeType = TreeType.FANCY;
        newCollapsible = true;
    } else {
        manager = (TreeStateManager<Long>) savedInstanceState
                .getSerializable("treeManager");
        if (manager == null) {
            manager = new InMemoryTreeStateManager<Long>();
        }
        newTreeType = (TreeType) savedInstanceState
                .getSerializable("treeType");
        if (newTreeType == null) {
            newTreeType = TreeType.SIMPLE;
        }
        newCollapsible = savedInstanceState.getBoolean("collapsible");
    }
    treeView = (TreeViewList) v.findViewById(R.id.mainTreeView);
    fancyAdapter = new FancyColouredVariousSizesAdapter(getActivity(), selected,
            manager, LEVEL_NUMBER, job_kind, list);
    simpleAdapter = new SimpleStandardAdapter(getActivity(), selected, manager,
            LEVEL_NUMBER, job_kind, list);
    setTreeAdapter(newTreeType);
    setCollapsible(newCollapsible);
    registerForContextMenu(treeView);

    manager.collapseChildren(null);




    return v;
}

2 个答案:

答案 0 :(得分:1)

使用此方法为视图充气。

View v = View.inflate(m_context,R.layout.yourlayout,null);

答案 1 :(得分:0)

createView方法返回View,用于设置片段的视图。您在onClick中所做的是创建一个不会覆盖片段主视图的视图。

说实话,它会更容易,并且如果点击你用一个新片段替换片段,你需要更少的状态逻辑。

拥有处理两个独立布局的Fragment更难维护并保持正确的状态。更不用说会使这个班级更大。