将活动转换为片段

时间:2018-03-29 12:10:56

标签: android android-fragments android-activity

我在项目中进行了导航,而不是活动,我会将这些活动的菜单导入导航。所以我需要将这些活动转换成片段。

这是我的第一个activity.java

public class ToDoList extends AppCompatActivity implements BatListener, OnItemClickListener, OnOutsideClickedListener {

    private BatRecyclerView mRecyclerView;
    private BatAdapter mAdapter;
    private List<BatModel> mGoals;
    private BatItemAnimator mAnimator;

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

        // Navigator
        FoldingTabBar tabBar = (FoldingTabBar) findViewById(R.id.folding_tab_bar);

        tabBar.setOnFoldingItemClickListener(new FoldingTabBar.OnFoldingItemSelectedListener() {
            @Override
            public boolean onFoldingItemSelected(@NotNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.menu_profile:
                        Intent intent0 = new Intent(ToDoList.this, Home.class);
                        startActivity(intent0);
                        break;
                    case R.id.menu_todo:
                        break;
                    case R.id.menu_schedule:
                        Intent intent1 = new Intent(ToDoList.this, TimeTable.class);
                        startActivity(intent1);
                        break;
                    case R.id.menu_settings:
                        break;
                }
                return false;
            }
        });

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("");

        ((TextView) findViewById(R.id.text_title)).setTypeface(TypefaceUtil.getAvenirTypeface(this));

        mRecyclerView = (BatRecyclerView) findViewById(R.id.bat_recycler_view);
        mAnimator = new BatItemAnimator();

        mRecyclerView.getView().setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.getView().setAdapter(mAdapter = new BatAdapter(mGoals = new ArrayList<BatModel>() {{

        }}, this, mAnimator).setOnItemClickListener(this).setOnOutsideClickListener(this));

        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new BatCallback(this));
        itemTouchHelper.attachToRecyclerView(mRecyclerView.getView());
        mRecyclerView.getView().setItemAnimator(mAnimator);
        mRecyclerView.setAddItemListener(this);

        findViewById(R.id.root).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mRecyclerView.revertAnimation();
            }
        });

    }

    @Override
    public void add(String string) {
        mGoals.add(0, new Goal(string));
        mAdapter.notify(AnimationType.ADD, 0);
    }

    @Override
    public void delete(int position) {
        mGoals.remove(position);
        mAdapter.notify(AnimationType.REMOVE, position);
    }

    @Override
    public void move(int from, int to) {
        if (from >= 0 && to >= 0) {
            mAnimator.setPosition(to);
            BatModel model = mGoals.get(from);
            mGoals.remove(model);
            mGoals.add(to, model);
            mAdapter.notify(AnimationType.MOVE, from, to);

            if (from == 0 || to == 0) {
                mRecyclerView.getView().scrollToPosition(Math.min(from, to));
            }
        }
    }

    @Override
    public void onClick(BatModel item, int position) {
        Toast.makeText(this, item.getText(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onOutsideClicked() {
        mRecyclerView.revertAnimation();
    }

}

我尝试使用这段代码,来自Abhi先生的指示,我只是编辑了一点用于删除工具栏,有些我解决了。

这是我的fragment.java

public class ToDoListFragment extends Fragment implements BatListener, OnItemClickListener, OnOutsideClickedListener {

private BatRecyclerView mRecyclerView;
private BatAdapter mAdapter;
private List<BatModel> mGoals;
private BatItemAnimator mAnimator;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.todo_list_fragment, null);
    return root;
}

public void onViewCreated(View view, Bundle savedInstanceState) {
    // you can add listener of elements here
  /*Button mButton = (Button) view.findViewById(R.id.button);
    mButton.setOnClickListener(this); */

    ((TextView) view.findViewById(R.id.tdl_date)).setTypeface(TypefaceUtil.getAvenirTypeface(getActivity()));

    mRecyclerView = (BatRecyclerView) view.findViewById(R.id.tdl_bat_recyclerView);
    mAnimator = new BatItemAnimator();

    mRecyclerView.getView().setLayoutManager(new LinearLayoutManager(getActivity()));
    mRecyclerView.getView().setAdapter(mAdapter = new BatAdapter(mGoals = new ArrayList<BatModel>() {{

    }}, this, mAnimator).setOnItemClickListener(this).setOnOutsideClickListener(this));

    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new BatCallback(this));
    itemTouchHelper.attachToRecyclerView(mRecyclerView.getView());
    mRecyclerView.getView().setItemAnimator(mAnimator);
    mRecyclerView.setAddItemListener(this);

    view.findViewById(R.id.root).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mRecyclerView.revertAnimation();
        }
    });

}

@Override
public void add(String string) {
    mGoals.add(0, new Goal(string));
    mAdapter.notify(AnimationType.ADD, 0);
}

@Override
public void delete(int position) {
    mGoals.remove(position);
    mAdapter.notify(AnimationType.REMOVE, position);
}

@Override
public void move(int from, int to) {
    if (from >= 0 && to >= 0) {
        mAnimator.setPosition(to);
        BatModel model = mGoals.get(from);
        mGoals.remove(model);
        mGoals.add(to, model);
        mAdapter.notify(AnimationType.MOVE, from, to);

        if (from == 0 || to == 0) {
            mRecyclerView.getView().scrollToPosition(Math.min(from, to));
        }
    }
}

@Override
public void onClick(BatModel item, int position) {
    Toast.makeText(getActivity(), item.getText(), Toast.LENGTH_SHORT).show();
}

@Override
public void onOutsideClicked() {
    mRecyclerView.revertAnimation();
}

但是当我启动应用程序时,它就崩溃了。日志显示没有找到id为0x7f09004c(package:id / container)的视图。

我希望你们可以帮助我,就像你教我一样。第二次感谢。

1 个答案:

答案 0 :(得分:0)

要将class Foo def self.bar do_calc end private_class_method def self.do_calc #... end end 转换为Activity,首先必须展开Fragment。那么你将不得不在代码中进行一些基本的必要更改,如:

1. Fragment代替onCreateView(LayoutInflater, ViewGroup, Bundle)

2. onCreate变为findViewById()

您的示例代码:

getView().findViewById()

我没有编辑所有代码,可能需要进一步更改。相应地做出改变。