【问题标题】:fragment is not redirecting in android片段没有在android中重定向
【发布时间】:2018-04-05 07:07:24
【问题描述】:
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
TextView textViewId, textViewUsername, textViewEmail, textViewGender;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    //add this line to display menu1 when the activity is loaded
    //displaySelectedScreen(R.id.nav_menu1);
    if (!SharedPrefManager.getInstance(this).isLoggedIn()) {
        finish();
        startActivity(new Intent(this, Login.class));
    }


    textViewId = (TextView) findViewById(R.id.textViewId);
    textViewUsername = (TextView) findViewById(R.id.textViewUsername);
    textViewEmail = (TextView) findViewById(R.id.textViewEmail);
    textViewGender = (TextView) findViewById(R.id.textViewGender);


    //getting the current user
    User user = SharedPrefManager.getInstance(this).getUser();

    //setting the values to the textviews
    textViewId.setText(String.valueOf(user.getId()));
    textViewUsername.setText(user.getUsername());
    textViewEmail.setText(user.getEmail());
    textViewGender.setText(user.getGender());

    //when the user presses logout button
    //calling the logout method
    findViewById(R.id.buttonLogout).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
            SharedPrefManager.getInstance(getApplicationContext()).logout();
        }
    });

}
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, 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);
}

private void displaySelectedScreen(int itemId) {

    //creating fragment object
    Fragment fragment = null;

    //initializing the fragment object which is selected
    switch (itemId) {
        case R.id.staff_link:
            fragment = new Staff_layout();
            break;
        case R.id.student_link:
            fragment = new Student_layout();
            break;
        case R.id.vehicle_link:
            fragment = new Vehicle_layout();
            break;
    }

    //replacing the fragment
    if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment);
        ft.commit();
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
}


@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {

    //calling the method displayselectedscreen and passing the id of selected menu
    displaySelectedScreen(item.getItemId());
    //make this method blank
    return true;
}


}

这是我的 MainActivity,当单击该项目时,它没有重定向,也没有显示任何 logcat 错误,这是我的 XML 文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/student_link"

        android:title="Student Register" />
    <item
        android:id="@+id/staff_link"

        android:title="Staff Register" />
    <item
        android:id="@+id/vehicle_link"
        android:title="Vehicle Register" />

</group>
</menu>

即使单击该项目也不会显示任何响应

 import android.os.Bundle;
 import android.support.annotation.Nullable;
 import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;



 public class Student_layout extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup      container, @Nullable Bundle savedInstanceState) {
    //returning our layout file
    //change R.layout.yourlayoutfilename for each of your fragments
    return inflater.inflate(R.layout.student_layout, container, false);
}


@Override
public void onViewCreated(View view, @Nullable Bundle 
savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    //you can set the title for your toolbar here for different         
fragments different titles
    getActivity().setTitle("Menu 1");
}
}

点击学生项目时不会重定向到学生片段布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="student"
    android:id="@+id/textView"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

</RelativeLayout>

有什么我应该纠正的或者这不起作用吗?

【问题讨论】:

  • 您以错误的方式切换片段。在切换情况下,您不会在每种情况下切换您的 if 条件。
  • 也发布你的logcat,当你选择项目时有什么错误吗?
  • 不,没有错误
  • 这个方法会被调用 displaySelectedScreen() 吗?

标签: android xml android-fragments


【解决方案1】:

正确切换片段,您在切换案例中声明片段,但您在案例之外切换片段:

你可以使用这个代码

Fragment mCurrentLoadedFragment;
android.support.v4.app.FragmentManager mFragmentManager, fm;
FragmentTransaction mFragmentTransaction;

方法是

    public void switchContent(Fragment fragment) {
    mCurrentLoadedFragment = fragment;
    mFragmentManager = getSupportFragmentManager();
    mFragmentTransaction = mFragmentManager.beginTransaction();
    mFragmentTransaction.replace(R.id.container, mCurrentLoadedFragment);
    mFragmentTransaction.commit();
}

然后这样调用,

  if (id == R.id.nav_home) {
        switchContent(new HomeFragment());
    }

【讨论】:

  • 同样的问题,它没有重定向
  • @ShanmukhaVj 你检查了吗,容器ID
猜你喜欢
  • 2013-01-06
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
相关资源
最近更新 更多