将活动转化为片段

时间:2018-10-17 23:45:06

标签: java android android-activity fragment

我是Android的新手,我正在尝试将此Activity转换为Fragment,但是我不知道如何解决错误。

将其更改为Fragment时,会得到很多红色代码setContentViewfindViewByIdgetMenuInflater。我了解这些颜色可能是红色的,因为它们打算用于Activities,但我不确定它们将被更改为什么。

    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.Menu;
    import android.view.MenuItem;

    public class LocationsFragment extends Fragment {

    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    RecyclerView.Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_card_demo);
        // Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        // setSupportActionBar(toolbar);

        recyclerView =
                (RecyclerView) findViewById(R.id.recycler_view);

        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        adapter = new RecyclerAdapter();
        recyclerView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_card_demo, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:3)

setContentView()

在Fragments中没有这样的方法。相反,您可以覆盖onCreateView()并从中返回一些视图。您可以使用传入的LayoutInflater来膨胀通常传递给setContentView()的布局ID,然后返回该ID。

findViewById()

片段没有findViewById()方法。但是,在onCreateView()返回之后的任何时间,您都可以使用getView().findViewById()。在onCreateView()内部,如果您为展开的视图命名为root,则可以调用root.findViewById()。在onCreateView()之前,您无法查找任何视图(因为它们尚未被夸大)。

getMenuInflater()

片段对onCreateOptionsMenu()使用不同的签名。看起来像这样:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // ...
}

您可以使用传入的MenuInflater代替调用getMenuInflater()


将所有内容放在一起,这就是您想要的:

public class LocationsFragment extends Fragment {

    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    RecyclerView.Adapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.activity_card_demo, container, false);

        recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view);

        layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);

        adapter = new RecyclerAdapter();
        recyclerView.setAdapter(adapter);

        return root;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_card_demo, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}