我创建了一个DrawerLayout,它工作得很好。这个DrawerLayout打开片段,我没有创建FragmentActivity来控制我的片段。
在我的应用程序中,我有一个做登录的活动,如果登录是可以的,我将启动。现在,我需要控制我的片段,例如,在一些片段中,我需要停止返回设备。
1- FragmentActivity真的需要创建吗?
2-我怎样才能用of来阻止碎片?
3-如果我需要创建FragmentActivity,我如何添加DrawerLayout?
XML DrawerLayout
<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>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 LoginView extends Activity implements OnClickListener{
private EditText etEmail;
private EditText etSenha;
private Button btCadastrar;
private Button btEntrar;
private Button btEsqueci;
private Intent intentCadastrar;
private Intent intentEsqueci;
private Intent intentInicio;
private ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//remove barra de titulos
setContentView(R.layout.login);
private void doLogin(){
if(etEmail.getText().toString().isEmpty() || etSenha.getText().toString().isEmpty()){
Toast.makeText(this, "Informe todos os campos", Toast.LENGTH_SHORT).show();
}else{
progress = new CustomProgressDialog().getCustomProgress(null, LoginView.this);
progress.show();
Usuario usuario = new Usuario();
usuario.setEmail(etEmail.getText().toString());
usuario.setSenha(etSenha.getText().toString());
ApplicationController app = new UsuarioDAO().isUsuarioLogin(
usuario,
new UsuarioAdapter(){
@Override
public void usuarioIsLogin(Boolean result) {
if(!result){
Toast.makeText(getApplicationContext(), "Email ou senha inválido", Toast.LENGTH_SHORT).show();;
}else{
startActivity(new Intent(getApplicationContext(), CustomDrawerLayout.class));
finish();
}
progress.dismiss();
}
});
CustomVolleySingleton.getInstance(getApplicationContext()).addToRequestQueue(app);
}
}
}片段
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)));
}
}发布于 2014-12-05 12:30:37
FragmentActivity真的需要创建吗?如何通过单击按钮打开片段?
您目前使用的是继承ActionBarActivity的FragmentActivity,因此您已经拥有了FragmentManager。如果您想通过单击按钮打开片段,那么在按钮的OnClickListener代码:getSupportFragmentManager().beginTransaction().replace(R.id.view_to_which_attach_fragment, yourNewFragment).commit()中添加类似的代码
在没有FragmentActivity的情况下,如何在片段中停止“后退按钮按下”事件?
如果我正确地理解了你,你想要的是“覆盖”后退按钮的功能。基本上,您需要理解的是,对按下按钮的事件作出反应的不是片段。总是活动的反应,因为活动包含片段,它可以传播回按钮按下的事件给他们。因此,在活动中实现@Override public void onBackPressed() { \\do something here }。请记住在ActionBarActivity中这样做。Fragment没有这样的方法。如果您想在Fragment中以某种方式对按下的后退按钮事件做出反应,那么现在从ActionBarActivity中的onBackPressed()方法获得getSupportFragmentManager().findFragmentById()或getSupportFragmentManager().findFragmentByTag()的Fragment,并调用它的一些公共方法。
如果我需要创建FragmentActivity,我如何添加DrawerLayout?
只需将包含DrawerLayout的布局设置为ActionBarActivity布局即可。然后在这个DrawerLayout中保存FrameLayout,您将通过FragmentManager向其中添加片段。
发布于 2014-12-04 20:41:42
FragmentActivity真的需要创建吗?
是的,它需要创建。没有FragmentActivity就不能创建片段。FragmentActivity包含管理片段的FragmentManager。ActionBarActivity继承自FragmentActivity。
如果没有FragmentActivity,我如何才能阻止片段的回退?
你做不到。通过FragmentManager控制您的片段。
如果我需要创建FragmentActivity,我如何添加DrawerLayout?
只需将FragmentActivity布局设置为包含DrawerLayout的布局即可。然后在这个DrawerLayout中保存FrameLayout,您将通过FragmentManager向其中添加片段。
https://stackoverflow.com/questions/27303506
复制相似问题