如何使用ImageButton从Fragment更改为Fragment?

时间:2016-01-14 17:09:02

标签: java android xml android-fragments mobile

我尝试使用ImageButton从MainFragment转到AddPostFragment。 这怎么可能......我尝试了很多东西...... 我想使用片段将我的导航抽屉放在上面而不是新活动。

MainFragment:

public class MainFragment extends Fragment implements View.OnClickListener{

ImageButton floatButton;

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

    floatButton = (ImageButton)rootView.findViewById(R.id.postButtonMain);
    floatButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent post = new Intent(getActivity(), AddPostFragment.class);
            startActivity(post);
        }
    });

    return rootView;
}

@Override
public void onClick(View v) {

}

}

AddPostFragment:

public class AddPostFragment extends Fragment {

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

    return rootView;
}

}

XML MainFragment:

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Home"
    android:id="@+id/frag_home"
    android:layout_gravity="center_horizontal|top" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/postButtonMain"
    android:src="@drawable/rsz_add_car"
    android:background="?android:attr/selectableItemBackground"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_gravity="right|bottom" />

XML AddPostFragment:

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Add Post"
    android:id="@+id/add_post"
    android:layout_gravity="center_horizontal|top"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Airport"
    android:id="@+id/text_airport"
    android:layout_marginTop="88dp"
    android:layout_below="@+id/add_post"
    android:layout_toLeftOf="@+id/add_post"
    android:layout_toStartOf="@+id/add_post" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Date"
    android:id="@+id/text_date"
    android:layout_below="@+id/text_airport"
    android:layout_alignRight="@+id/text_airport"
    android:layout_alignEnd="@+id/text_airport"
    android:layout_marginTop="36dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Time"
    android:id="@+id/text_time"
    android:layout_centerVertical="true"
    android:layout_alignRight="@+id/text_date"
    android:layout_alignEnd="@+id/text_date" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Persons"
    android:id="@+id/text_persons"
    android:layout_marginTop="43dp"
    android:layout_below="@+id/add_time"
    android:layout_toLeftOf="@+id/add_time"
    android:layout_toStartOf="@+id/add_time" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Address"
    android:id="@+id/text_address"
    android:layout_marginTop="48dp"
    android:layout_below="@+id/text_persons"
    android:layout_alignLeft="@+id/text_persons"
    android:layout_alignStart="@+id/text_persons" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="time"
    android:ems="10"
    android:id="@+id/add_time"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/text_time"
    android:layout_alignRight="@+id/add_post"
    android:layout_alignEnd="@+id/add_post" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="date"
    android:ems="10"
    android:id="@+id/add_date"
    android:layout_alignTop="@+id/text_date"
    android:layout_toRightOf="@+id/text_date"
    android:layout_alignRight="@+id/add_time"
    android:layout_alignEnd="@+id/add_time" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/add_persons"
    android:layout_above="@+id/text_address"
    android:layout_toRightOf="@+id/text_persons"
    android:layout_alignRight="@+id/add_post"
    android:layout_alignEnd="@+id/add_post" />

<android.support.v7.widget.SearchView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/add_search"
    android:layout_above="@+id/text_date"
    android:layout_toRightOf="@+id/text_airport"
    android:layout_toEndOf="@+id/text_airport" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/add_address"
    android:layout_alignBottom="@+id/text_address"
    android:layout_alignRight="@+id/add_post"
    android:layout_alignEnd="@+id/add_post"
    android:layout_toRightOf="@+id/text_address"
    android:layout_toEndOf="@+id/text_address" />

MainMenu带导航抽屉:

public class MainMenu extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

Fragment someFragment;


/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
private GoogleApiClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);
    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);


    FragmentManager fm = getFragmentManager();
    fm.beginTransaction().replace(R.id.content_main, new MainFragment()).commit();



}


@Override
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, 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

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    FragmentManager fm = getFragmentManager();

    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_home) {
        // Handle the home action
        fm.beginTransaction().replace(R.id.content_main, new MainFragment()).commit();
    } else if (id == R.id.nav_demand) {
        fm.beginTransaction().replace(R.id.content_main, new DemandFragment()).commit();
    } else if (id == R.id.nav_posts) {
        fm.beginTransaction().replace(R.id.content_main, new PostsFragment()).commit();
    } else if (id == R.id.nav_messages) {
        fm.beginTransaction().replace(R.id.content_main, new MessagesFragment()).commit();
    } else if (id == R.id.nav_settings) {
        fm.beginTransaction().replace(R.id.content_main, new SettingsFragment()).commit();
    } else if (id == R.id.nav_logout) {

    }

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

1 个答案:

答案 0 :(得分:0)

你必须在onNavigationItemSelected

上做同样的事情
floatButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           FragmentManager fm = getActivity().getFragmentManager();
           fm.beginTransaction().replace(R.id.content_main, new PostsFragment()).commit();
        }
    });