替换碎片并不起作用

时间:2016-07-30 06:38:20

标签: java android xml android-fragments

我有一个大问题。我的主@Override public void onDrawerItemSelected(View view, int position) { SendPost fragment = null; switch (position) { case 0: SendPost sendpost = new SendPost(); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK); transaction.replace(R.id.container_body,sendpost); transaction.addToBackStack(null); transaction.commit(); Toast.makeText(getApplicationContext(), "index 0 clicked!", Toast.LENGTH_SHORT).show(); break; case 1: Toast.makeText(getApplicationContext(), "index 1 clicked!", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(getApplicationContext(), "index 2 clicked!", Toast.LENGTH_SHORT).show(); break; default: break; } } 文件定义如下:

fragment

我想使用以下代码加载一个简单的xml。我的<LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:gravity="start" tools:context="com.soshia.paad.volleycustomlistview.MainActivity" android:orientation="vertical"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <include android:id="@+id/toolbar" layout="@layout/toolbar" /> <android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignTop="@+id/main_activity_for_draver" android:id="@+id/recy_for_main_activity" android:clipToPadding="false" android:paddingBottom="60dp" android:scrollbars="vertical" > </android.support.v7.widget.RecyclerView> </LinearLayout> <FrameLayout android:id="@+id/container_body" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" tools:ignore="Suspicious0dp"> </FrameLayout> </LinearLayout> <fragment android:id="@+id/fragment_navigation_drawer" android:layout_width="@dimen/nav_drawer_width" android:layout_height="match_parent" android:layout_gravity="start" app:layout="@layout/fragment_navigation_drawer" tools:layout="@layout/fragment_navigation_drawer" android:name="com.soshia.paad.FragmentDrawer" > </fragment> 文件位于底部。

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener

我的活动

<FrameLayout 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"
tools:context="com.soshia.paad.SendPost"
android:background="@color/web_sormeii_color"
>

<!-- TODO: Update blank fragment layout -->
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="سلام"
    android:textSize="80dp"
    android:gravity="center_horizontal|center_vertical"

    />

my fragment xml

public class SendPost extends Fragment {

public SendPost() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

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


    // Inflate the layout for this fragment
    return rootView;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onDetach() {
    super.onDetach();
}}

我的片段代码

public class RadioActivity extends ActionBarActivity implements RadioListener{

private final String[] RADIO_URL = {"url "};
private ActionMenuView amvMenu;

ImageButton mButtonControlStart;
TextView mTextViewControl;
RadioManager mRadioManager = RadioManager.with(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_radio);
    mButtonControlStart = (ImageButton) findViewById(R.id.buttonControlStart);



    mRadioManager.registerListener(this);
    mRadioManager.setLogging(true);

    initializeUI();
    mButtonControlStart.setBackgroundResource(R.drawable.play);
    Toolbar toolbarBottom = (Toolbar) findViewById(R.id.toolbar);
    toolbarBottom.setTitle("Radio");
    amvMenu = (ActionMenuView) toolbarBottom.findViewById(R.id.amvMenu);
    amvMenu.setOnMenuItemClickListener(new ActionMenuView.OnMenuItemClickListener() {
        //  toolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {

        public boolean onMenuItemClick(MenuItem item) {
            switch(item.getItemId()){
                case R.id.activity_aboutus:
                    // TODO
                    Intent intent=new Intent(RadioActivity.this,Contact_us.class);
                    startActivity(intent);
                    break;
                // TODO: Other cases
                case R.id.menu_refresh:
                    // TODO
                    Intent intent1=new Intent(RadioActivity.this,Web.class);
                    startActivity(intent1);
                    break;
                case R.id.menu_about:
                    Intent intent2=new Intent(RadioActivity.this,Song_rqst.class);
                    startActivity(intent2);
                    break;
                case R.id.menu_item_share:
                    Intent intent3=new Intent(RadioActivity.this,Ratee.class);
                    startActivity(intent3);
                    break;



            }
            return true;
        }
    });

  }

public void initializeUI() {
    mButtonControlStart = (ImageButton) findViewById(R.id.buttonControlStart);
    mTextViewControl = (TextView) findViewById(R.id.textviewControl);

    mButtonControlStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!mRadioManager.isPlaying()) {
                mRadioManager.startRadio(RADIO_URL[0]);
                mButtonControlStart.setBackgroundResource(R.drawable.end);
            }
            else {
                mRadioManager.stopRadio();
                mButtonControlStart.setBackgroundResource(R.drawable.play);

            }
        }
    });

}

@Override
protected void onResume() {
    super.onResume();
    mRadioManager.connect();
}


@Override
protected void onDestroy() {
    super.onDestroy();
    mRadioManager.disconnect();
}

@Override
public void onRadioLoading() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            //TODO Do UI works here.
            mTextViewControl.setText("RADIO STATE : LOADING...");
        }
    });
}

@Override
public void onRadioConnected() {

}

@Override
public void onRadioStarted() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            //TODO Do UI works here.
            mTextViewControl.setText("RADIO STATE : PLAYING...");
        }
    });
}

@Override
public void onRadioStopped() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            //TODO Do UI works here
            mTextViewControl.setText("RADIO STATE : STOPPED.");
        }
    });
}

@Override
public void onMetaDataReceived(String s, String s1) {
    //TODO Check metadata values. Singer name, song name or whatever you have.
}

@Override
public void onError() {

}

但我无法理解为什么卷轴不起作用。我搜索了很多这个网站,但我找不到解决方案。我很急。请详细解释给我。 你的。 运行程序后,没有任何反应,前一个片段保持不变。我认为碎片不会移动。

0 个答案:

没有答案