我动态放置片段有什么问题?

时间:2016-04-22 20:58:18

标签: java android android-fragments

我想将我的活动重写为对话框,我想以编程方式替换对话框。我试过片段静态,它工作正常。当我以编程方式调用片段并使用断点调试它时,我不明白为什么它的生命周期从onCreate()开始并以它结束...为什么来自onCreate()而不是来自onAttach(),就像我静态调用它一样?这是我的代码,帮帮我PLZ !!!

base_activity.xml`

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/base_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout

        android:id="@+id/base_fragment_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>

</android.support.v4.widget.DrawerLayout>

`

BaseActivity.java

    public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base_activity);

        DietsFragment dietsFragment = new DietsFragment();
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.base_fragment_holder, dietsFragment);
        fragmentTransaction.commit();

    }
}

DietsFragment.java

public class DietsFragment extends Fragment
        implements AdapterView.OnItemClickListener,
        View.OnClickListener {
    public FragmentEventListener fragmentEventListener;

    Cursor diets;
    DietAdapter adapter;
    DietHelper helper;
    ListView listView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.activity_diets, container, false);

        listView = (ListView) view.findViewById(R.id.act_diets_lv_diets);
        if (listView != null) {
            listView.setOnItemClickListener(this);
        }

        FloatingActionButton floatingActionButton = (FloatingActionButton) view.findViewById(R.id.act_diets_fab);
        if (floatingActionButton != null)
            floatingActionButton.setOnClickListener(this);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);

        setHelper(new DietHelper(getActivity()));
        setDiets(getHelper().getAllTableRecordsCursor());
        setAdapter(new DietAdapter(getActivity(), getDiets()));
        listView.setAdapter(getAdapter());
        fragmentEventListener = (FragmentEventListener) getActivity();
    }

    public void setDiets (Cursor diets) {
        this.diets = diets;
    }

    public void setAdapter (DietAdapter adapter) {
        this.adapter = adapter;
    }

    public void setHelper (DietHelper helper) {
        this.helper = helper;
    }

    public Cursor getDiets() {
        return diets;
    }

    public DietAdapter getAdapter() {
        return adapter;
    }

    public DietHelper getHelper() {
        return helper;
    }
}

在DietsFragment和BaseActivity中我隐藏了它的OnClickListener,OnItemClickListener和FragmentEventListener的实现,因为当我从xml中静态地添加片段在BaseActivity中时它工作正常

1 个答案:

答案 0 :(得分:0)

我刚刚发现我的错误...... 我在BaseActivity中重载了findViewbyId(int resId)以及我的所有问题,现在我问自己为什么我超载这个方法我是什么想要用它做什么?