Android 碎片活动和抽屉布局?
我创造了一个抽屉布局,很好用。这个抽屉布局打开片段,我没有创建FragmentActivity来控制我的片段 在我的应用程序中,我有一个可以登录的活动,如果登录正常,我将启动活动抽屉布局。现在,我需要控制我的片段的“返回”,例如,在一些片段中,我需要停止返回设备 1-是否真的需要创建碎片活动 2-如果没有碎片活动,我如何停止碎片的背面 3-如果我需要创建FragmentActivity,如何添加抽屉布局 XML抽屉布局Android 碎片活动和抽屉布局?,android,android-fragments,Android,Android Fragments,我创造了一个抽屉布局,很好用。这个抽屉布局打开片段,我没有创建FragmentActivity来控制我的片段 在我的应用程序中,我有一个可以登录的活动,如果登录正常,我将启动活动抽屉布局。现在,我需要控制我的片段的“返回”,例如,在一些片段中,我需要停止返回设备 1-是否真的需要创建碎片活动 2-如果没有碎片活动,我如何停止碎片的背面 3-如果我需要创建FragmentActivity,如何添加抽屉布局 XML抽屉布局 <android.support.v4.widget.DrawerLa
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dl"
>
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
<ListView
android:id="@+id/lv"
android:layout_width="250dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#e9ba68"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:dividerHeight="1dp"
android:background="#ac453c"
android:layout_gravity="start"
>
</ListView>
</android.support.v4.widget.DrawerLayout>
public class CustomDrawerLayout extends ActionBarActivity implements OnItemClickListener{
private ActionBar ab;
private DrawerLayout dl;
private ListView lv;
private ActionBarDrawerToggle tg;
private List<ItensListView> fragments;
private CharSequence tl; //titulo principal
private CharSequence tlf; //titulo fragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_drawerlayout);
init();
if(savedInstanceState == null){
selectedItem(0);
}
}
private void init(){
//actionbar
onConfigActionBar();
//listview
configItensListView();
lv = (ListView)findViewById(R.id.lv);
lv.setAdapter(new DrawerLayoutListViewAdapter(this, fragments));
lv.setOnItemClickListener(this);
//drawerlayout
dl = (DrawerLayout)findViewById(R.id.dl);
//actionbardrawertoggle
tg = new ActionBarDrawerToggle(this, dl, R.drawable.btmenu, R.string.nomeActionBar){
public void onDrawerClosed(View view) {
ab.setTitle(tl);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View view) {
ab.setTitle(tlf);
supportInvalidateOptionsMenu();
}
};
dl.setDrawerListener(tg);
tl = tlf = getTitle();
}
/** ativa actionbar e botao home na action bar */
private void onConfigActionBar(){
ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeButtonEnabled(true);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
tg.onConfigurationChanged(newConfig);
}
/** necessario */
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
tg.syncState();
}
/** necessario */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (tg.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** necessario */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_drawer_layout, menu);
return true;
}
/** necessario */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean status = dl.isDrawerOpen(lv);
menu.findItem(R.id.action_settings).setVisible(!status);
return super.onPrepareOptionsMenu(menu);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
selectedItem(position);
}
/** open fragments */
private void selectedItem(int position){
FragmentTransaction ft;
Fragment frag;
switch(position){
case 0:
//frag = new InicioFrag();
frag = new InicioFrag();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl, frag);
ft.addToBackStack("back");
ft.commit();
break;
case 1:
frag = new ApresentacaoFrag();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl, frag);
ft.addToBackStack("back");
ft.commit();
break;
case 3:
frag = new PerfilFrag();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl, frag);
ft.addToBackStack("back");
ft.commit();
break;
}
public class ApresentacaoFrag extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.apresentacao_view, container, false);
TextView txtView = (TextView)view.findViewById(R.id.tvApresentacao);
txtView.setText(Html.fromHtml(getString(R.string.apresentacao)));
return view;
}
private void alteraTextView(String texto){
TextView txtView = (TextView)getView().findViewById(R.id.tvApresentacao);
txtView.setText(Html.fromHtml(getString(R.string.apresentacao)));
}
}
片段
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/dl"
>
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
<ListView
android:id="@+id/lv"
android:layout_width="250dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:divider="#e9ba68"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:dividerHeight="1dp"
android:background="#ac453c"
android:layout_gravity="start"
>
</ListView>
</android.support.v4.widget.DrawerLayout>
public class CustomDrawerLayout extends ActionBarActivity implements OnItemClickListener{
private ActionBar ab;
private DrawerLayout dl;
private ListView lv;
private ActionBarDrawerToggle tg;
private List<ItensListView> fragments;
private CharSequence tl; //titulo principal
private CharSequence tlf; //titulo fragment
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_drawerlayout);
init();
if(savedInstanceState == null){
selectedItem(0);
}
}
private void init(){
//actionbar
onConfigActionBar();
//listview
configItensListView();
lv = (ListView)findViewById(R.id.lv);
lv.setAdapter(new DrawerLayoutListViewAdapter(this, fragments));
lv.setOnItemClickListener(this);
//drawerlayout
dl = (DrawerLayout)findViewById(R.id.dl);
//actionbardrawertoggle
tg = new ActionBarDrawerToggle(this, dl, R.drawable.btmenu, R.string.nomeActionBar){
public void onDrawerClosed(View view) {
ab.setTitle(tl);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View view) {
ab.setTitle(tlf);
supportInvalidateOptionsMenu();
}
};
dl.setDrawerListener(tg);
tl = tlf = getTitle();
}
/** ativa actionbar e botao home na action bar */
private void onConfigActionBar(){
ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeButtonEnabled(true);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
tg.onConfigurationChanged(newConfig);
}
/** necessario */
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
tg.syncState();
}
/** necessario */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (tg.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** necessario */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_drawer_layout, menu);
return true;
}
/** necessario */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean status = dl.isDrawerOpen(lv);
menu.findItem(R.id.action_settings).setVisible(!status);
return super.onPrepareOptionsMenu(menu);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
selectedItem(position);
}
/** open fragments */
private void selectedItem(int position){
FragmentTransaction ft;
Fragment frag;
switch(position){
case 0:
//frag = new InicioFrag();
frag = new InicioFrag();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl, frag);
ft.addToBackStack("back");
ft.commit();
break;
case 1:
frag = new ApresentacaoFrag();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl, frag);
ft.addToBackStack("back");
ft.commit();
break;
case 3:
frag = new PerfilFrag();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fl, frag);
ft.addToBackStack("back");
ft.commit();
break;
}
public class ApresentacaoFrag extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.apresentacao_view, container, false);
TextView txtView = (TextView)view.findViewById(R.id.tvApresentacao);
txtView.setText(Html.fromHtml(getString(R.string.apresentacao)));
return view;
}
private void alteraTextView(String texto){
TextView txtView = (TextView)getView().findViewById(R.id.tvApresentacao);
txtView.setText(Html.fromHtml(getString(R.string.apresentacao)));
}
}
碎片活动真的需要创建吗
是的,它需要被创建。如果没有FragmentActivity,则无法创建片段。FragmentActivity包含管理片段的FragmentManager。ActionBarActivity继承自FragmentActivity
如果没有碎片活动,我如何停止碎片的背面
你不能。通过FragmentManager控制您的片段
如果需要创建FragmentActivity,如何添加抽屉布局
只需将抽屉布局设置为您的FragmentActivity布局。然后在抽屉布局中保留FrameLayout,您将通过FragmentManager向其中添加片段
碎片活动真的需要创建吗?如何通过单击按钮打开片段
您当前正在使用继承了碎片活动的
ActionBarActivity
,因此您已经有了FragmentManager
。如果要通过单击按钮打开片段,请在按钮的OnClickListener代码中添加类似的代码:getSupportFragmentManager().beginTransaction().replace(R.id.view)\u to\u,其中h_fragment,yourNewFragment).commit()
如何在没有碎片活动的情况下停止碎片中的“按下后退按钮”事件
如果我理解正确,您需要的是“覆盖”后退按钮的功能。基本上,您需要了解,对按下后退按钮事件做出反应的并不是片段。总是活动做出反应,因为活动包含片段,所以它可以将按下按钮的事件传播给它们。因此,在活动中实现@Override public void onBackPressed(){\\do some here}
。请记住在ActionBarActivity
中执行此操作<代码>片段没有这样的方法。如果您想在片段中对按下后退按钮事件做出某种反应,那么现在从ActionBarActivity
中的onBackPressed()
方法通过getSupportFragmentManager().findFragmentById()或getSupportFragmentManager().findFragmentByTag()获取片段
并调用它的一些公共方法
如果需要创建FragmentActivity,如何添加抽屉布局
只需将抽屉向外的布局设置为ActionBarActivity
布局。然后在DrawerLayout
中保留FrameLayout
,您将通过FragmentManager
向其中添加片段,但ActionBarActivity继承自FragmentActivity:从ActionBarCompat扩展活动类:ActionBarCompat包含一个活动类,您的所有活动类都应包含该活动类扩展:ActionBarActivity。此类本身从FragmentActivity扩展而来,因此您可以继续在应用程序中使用片段。没有需要扩展的ActionBarCompat片段类,因此您应该继续使用android.support.v4.Fragment作为片段的基类。
非常好。所以,我有一个片段活动:)。但是如果我想从我的布局中的按钮打开一个片段?如果你想通过点击一个按钮来打开片段,那么在按钮的OnClickListener
codegetSupportFragmentManager().beginTransaction().replace(R.id.view\u to\u哪个附件\u片段,yourNewFragment.commit())中添加类似的代码。
哇,我这么做了,真的很好!!!但我有一个屏幕,我想控制设备的“背面”,你知道吗?如果用户按下设备的“后退”按钮,屏幕就不会后退,你能理解吗?好的,我试着总结一下:)