如何从片段更改为另一个片段立即由其中一个片段控制?

时间:2015-12-22 02:21:25

标签: android fragment

我有一个导航抽屉,我想在列表视图中按下按钮,然后立即更改右侧的片段。我怎样才能做到这一点?为什么我的代码不起作用?

这是我的代码:

DrawerFragment.class

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import java.util.ArrayList;

public class DrawerFragment extends Fragment {

private static ListView mSelectionListView;
private ImageView mUserIcon;
private TextView mUserName;

//region (Widgets) Declaration
NonScrollListView mNewsListView, mELeaveListView, mEClaimListView, mEAttendanceListView;
//endregion

//DrawerFragmentListener drawerFragmentListener;


//endregion


Fragment mFragment;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
ExpenseClaimPageFragment expenseClaimPageFragment;
ClaimFormPageFragment claimFormPageFragment;

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


    expenseClaimPageFragment = new ExpenseClaimPageFragment();
    claimFormPageFragment = new ClaimFormPageFragment();



    mEClaimListView = (NonScrollListView) view.findViewById(R.id.drawer_fragment_eClaim_selection);

    ArrayList<DrawerSelectionEClaimResult> selectionEClaimResults = GetDrawerSelectionEClaimResult();
    mEClaimListView.setAdapter(new CustomDrawerSelectionEClaimBaseAdapter(getActivity(), selectionEClaimResults));


    mEClaimListView.setOnItemClickListener(mEClaimListViewOnItemClick);

    return view;
    }

public ArrayList<DrawerSelectionEClaimResult> GetDrawerSelectionEClaimResult() {

    ArrayList<DrawerSelectionEClaimResult> results = new ArrayList<>();

    DrawerSelectionEClaimResult dsecr = new DrawerSelectionEClaimResult();
    dsecr.setSelection("Expenses Claim Entry");
    dsecr.setDrawable(R.drawable.expenseclaim);
    results.add(dsecr);

    dsecr = new DrawerSelectionEClaimResult();
    dsecr.setSelection("Expenses Claim Form");
    dsecr.setDrawable(R.drawable.expenseclaim);
    results.add(dsecr);

    return results;
}

    public NonScrollListView.OnItemClickListener mEClaimListViewOnItemClick = new NonScrollListView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        switch(position)
        {
            case 0:
                mFragmentManager = getActivity().getSupportFragmentManager();
                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragment = new ExpenseClaimPageFragment();
                mFragmentTransaction.replace(R.id.MainPageFrameLayout, mFragment);
                mFragmentTransaction.commit();
                break;
            case 1:
                mFragmentManager = getActivity().getSupportFragmentManager();
                mFragmentTransaction = mFragmentManager.beginTransaction();
                mFragment = new ClaimFormPageFragment();
                mFragmentTransaction.replace(R.id.MainPageFrameLayout, mFragment);
                mFragmentTransaction.commit();
                break;
        }

        }
    };
  }

Main.xml

<!--Main Content View (Expense Claim Page Content)-->
<FrameLayout
    android:id="@+id/MainPageFrameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.sample.flexsystem.expenseclaimapplication.ExpenseClaimPageFragment"
        android:id="@+id/fragment2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        tools:layout="@layout/activity_expenseclaim_page" />
</FrameLayout>
<!--Navigation Drawer View-->
<LinearLayout
    android:id="@+id/DrawerLin"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#FFFFFF"
    android:orientation="vertical">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:name="com.sample.flexsystem.expenseclaimapplication.DrawerFragment"
        android:id="@+id/fragment"
        tools:layout="@layout/activity_drawer_fragment" />
</LinearLayout>

我不知道为什么我的代码不起作用,将片段替换为另一个片段的主要代码是什么?我知道我需要一个布局来保存片段并使用replace()方法来处理它,但它不起作用。我的代码有什么问题吗?

我想我不能从另一个片段替换片段,因为片段之间没有相互通信吗?

更新1

Main Activity class

    import android.content.Intent;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentTransaction;
  import android.support.v4.widget.DrawerLayout;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;


  public class ExpenseClaimPage extends AppCompatActivity implements           DrawerFragment.DrawerFragmentListener{

DrawerLayout mDrawerLayout;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;
Fragment mFragment;

@Override
public void setPosition(int position)
{
    switch(position)
    {
        case 0:
            mFragmentManager = getSupportFragmentManager();
            mFragmentTransaction = mFragmentManager.beginTransaction();
            mFragment = new ExpenseClaimPageFragment();
            mFragmentTransaction.replace(R.id.MainPageFrameLayout, mFragment);
            mFragmentTransaction.addToBackStack(null);
            mFragmentTransaction.commit();
            break;

        case 1:
            mFragmentManager = getSupportFragmentManager();
            mFragmentTransaction = mFragmentManager.beginTransaction();
            mFragment = new ClaimFormPageFragment();
            mFragmentTransaction.replace(R.id.MainPageFrameLayout, mFragment);
            mFragmentTransaction.addToBackStack(null);
            mFragmentTransaction.commit();
            break;
    }
}









//region onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_page);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
}
//endregion


@Override
public void onBackPressed() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

}

1 个答案:

答案 0 :(得分:1)

查看您的main.xml,主要内容:

Main Content View (Expense Claim Page Content)
<FrameLayout
    android:id="@+id/MainPageFrameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="ExpenseClaimPageFragment"
    android:id="@+id/fragment2"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    tools:layout="@layout/activity_expenseclaim_page" />
</FrameLayout>

您无法替换使用xml添加的片段。因此,我建议您在创建活动时手动添加ExpenseClaimPageFragment。之后,您可以轻松地将其替换为另一个片段。