我在Android Studio中为我正在制作的应用创建了一个Master / Flow活动。我能够在应用的详细信息区域中显示指定的活动;但是,没有编码工作。
我尝试运行指定的活动(没有Master / Flow),它运行得很好!如何使活动的编码部分工作。我认为它需要在ItemdetailFragment的代码片段(下面)中修复:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);
// Show the dummy content as text in a TextView.
if (mItem.id.equals("1"))
{
rootView = inflater.inflate(R.layout.activity_music, container, false);
}
return rootView;
}
如果您需要任何其他信息,请告诉我。 谢谢,
答案 0 :(得分:0)
您只是在ItemDetailFragment中夸大音乐布局。只是膨胀布局不会使功能活跃起来。所以有了一些自由我改变了你的Music类扩展片段。我在下面添加一些代码。我希望它有所帮助,如果需要,请随时提出澄清。
ItemDetailFragment
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A fragment representing a single Item detail screen. This fragment is either
* contained in a {@link ItemListActivity} in two-pane mode (on tablets) or a
* {@link ItemDetailActivity} on handsets.
*/
public class ItemDetailFragment extends Fragment {
/**
* The fragment argument representing the item ID that this fragment
* represents.
*/
public static final String ARG_ITEM_ID = "item_id";
/**
* The dummy content this fragment is presenting.
*/
private DummyContent.DummyItem mItem;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ItemDetailFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (getArguments().containsKey(ARG_ITEM_ID)) {
// Load the dummy content specified by the fragment
// arguments. In a real-world scenario, use a Loader
// to load content from a content provider.
mItem = DummyContent.ITEM_MAP.get(getArguments().getString(
ARG_ITEM_ID));
}
View rootView = inflater.inflate(R.layout.fragment_item_detail,
container, false);
// Show the dummy content as text in a TextView.
if (mItem.id.equals("1")) {
rootView = inflater.inflate(R.layout.activity_music, container,
false);
}
if (mItem.id.equals("2")) {
FragmentManager fm = getChildFragmentManager();
fm.beginTransaction().replace(R.id.detail_container, new Music())
.commit();
}
if (mItem.id.equals("3")) {
rootView = inflater.inflate(R.layout.activity_music, container,
false);
}
return rootView;
}
}
fragment_item_detail
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</FrameLayout>
音乐
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class Music extends Fragment {
Button bttnKevin, bttnLecrae, bttnLuke;
// bttnSkillet, bttnSoMo, bttnHunter;
MediaPlayer mpKevin, mpLecrae, mpLuke, mpSkillet, mpSoMo, mpHunter;
int playing;
Button.OnClickListener bKevin = new Button.OnClickListener() {
@Override
public void onClick(View v) {
switch (playing) {
case 0:
mpKevin.start();
playing = 1;
bttnKevin.setText("Pause");
bttnLecrae.setVisibility(View.INVISIBLE);
bttnLuke.setVisibility(View.INVISIBLE);
// bttnSoMo.setVisibility(View.INVISIBLE);
// bttnHunter.setVisibility(View.INVISIBLE);
// bttnSkillet.setVisibility(View.INVISIBLE);
break;
case 1:
mpKevin.pause();
playing = 0;
bttnKevin.setText("Play");
bttnLecrae.setVisibility(View.VISIBLE);
bttnLuke.setVisibility(View.VISIBLE);
// bttnSoMo.setVisibility(View.VISIBLE);
// bttnHunter.setVisibility(View.VISIBLE);
// bttnSkillet.setVisibility(View.VISIBLE);
break;
}
}
};
Button.OnClickListener bLecrae = new Button.OnClickListener() {
@Override
public void onClick(View v) {
switch (playing) {
case 0:
mpLecrae.start();
playing = 1;
bttnLecrae.setText("Pause");
bttnKevin.setVisibility(View.INVISIBLE);
bttnLuke.setVisibility(View.INVISIBLE);
// bttnSoMo.setVisibility(View.INVISIBLE);
// bttnHunter.setVisibility(View.INVISIBLE);
// bttnSkillet.setVisibility(View.INVISIBLE);
break;
case 1:
mpLecrae.pause();
playing = 0;
bttnLecrae.setText("Play");
bttnKevin.setVisibility(View.VISIBLE);
bttnLuke.setVisibility(View.VISIBLE);
// bttnSoMo.setVisibility(View.VISIBLE);
// bttnHunter.setVisibility(View.VISIBLE);
// bttnSkillet.setVisibility(View.VISIBLE);
break;
}
}
};
Button.OnClickListener bLuke = new Button.OnClickListener() {
@Override
public void onClick(View v) {
switch (playing) {
case 0:
mpLuke.start();
playing = 1;
bttnLuke.setText("Pause");
bttnKevin.setVisibility(View.INVISIBLE);
bttnLecrae.setVisibility(View.INVISIBLE);
// bttnSoMo.setVisibility(View.INVISIBLE);
// bttnHunter.setVisibility(View.INVISIBLE);
// bttnSkillet.setVisibility(View.INVISIBLE);
break;
case 1:
mpLuke.pause();
playing = 0;
bttnLuke.setText("Play");
bttnKevin.setVisibility(View.VISIBLE);
bttnLecrae.setVisibility(View.VISIBLE);
// bttnSoMo.setVisibility(View.VISIBLE);
// bttnHunter.setVisibility(View.VISIBLE);
// bttnSkillet.setVisibility(View.VISIBLE);
break;
}
}
};
Button.OnClickListener bSkillet = new Button.OnClickListener() {
@Override
public void onClick(View v) {
switch (playing) {
case 0:
mpSkillet.start();
playing = 1;
// bttnSkillet.setText("Pause");
bttnKevin.setVisibility(View.INVISIBLE);
bttnLecrae.setVisibility(View.INVISIBLE);
// bttnSoMo.setVisibility(View.INVISIBLE);
// bttnHunter.setVisibility(View.INVISIBLE);
bttnLuke.setVisibility(View.INVISIBLE);
break;
case 1:
mpSkillet.pause();
playing = 0;
// bttnSkillet.setText("Play");
bttnKevin.setVisibility(View.VISIBLE);
bttnLecrae.setVisibility(View.VISIBLE);
// bttnSoMo.setVisibility(View.VISIBLE);
// bttnHunter.setVisibility(View.VISIBLE);
bttnLuke.setVisibility(View.VISIBLE);
break;
}
}
};
Button.OnClickListener bSoMo = new Button.OnClickListener() {
@Override
public void onClick(View v) {
switch (playing) {
case 0:
mpSoMo.start();
playing = 1;
// bttnSoMo.setText("Pause");
bttnKevin.setVisibility(View.INVISIBLE);
bttnLecrae.setVisibility(View.INVISIBLE);
// bttnSkillet.setVisibility(View.INVISIBLE);
// bttnHunter.setVisibility(View.INVISIBLE);
bttnLuke.setVisibility(View.INVISIBLE);
break;
case 1:
mpSoMo.pause();
playing = 0;
// bttnSoMo.setText("Play");
bttnKevin.setVisibility(View.VISIBLE);
bttnLecrae.setVisibility(View.VISIBLE);
// bttnSkillet.setVisibility(View.VISIBLE);
// bttnHunter.setVisibility(View.VISIBLE);
bttnLuke.setVisibility(View.VISIBLE);
break;
}
}
};
Button.OnClickListener bHunter = new Button.OnClickListener() {
@Override
public void onClick(View v) {
switch (playing) {
case 0:
mpHunter.start();
playing = 1;
// bttnHunter.setText("Pause");
bttnKevin.setVisibility(View.INVISIBLE);
bttnLecrae.setVisibility(View.INVISIBLE);
// bttnSkillet.setVisibility(View.INVISIBLE);
// bttnSoMo.setVisibility(View.INVISIBLE);
bttnLuke.setVisibility(View.INVISIBLE);
break;
case 1:
mpHunter.pause();
playing = 0;
// bttnHunter.setText("Play");
bttnKevin.setVisibility(View.VISIBLE);
bttnLecrae.setVisibility(View.VISIBLE);
// bttnSkillet.setVisibility(View.VISIBLE);
// bttnSoMo.setVisibility(View.VISIBLE);
bttnLuke.setVisibility(View.VISIBLE);
break;
}
}
};
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_music, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
bttnKevin = (Button) view.findViewById(R.id.bttnKevin);
bttnLecrae = (Button) view.findViewById(R.id.bttnLecrae);
bttnLuke = (Button) view.findViewById(R.id.bttnLuke);
// bttnSkillet = (Button)findViewById(R.id.bttnSkillet);
// bttnSoMo = (Button)findViewById(R.id.bttnSoMo);
// bttnHunter = (Button)findViewById(R.id.bttnHunter);
bttnKevin.setOnClickListener(bKevin);
bttnLecrae.setOnClickListener(bLecrae);
bttnLuke.setOnClickListener(bLuke);
// bttnSkillet.setOnClickListener(bSkillet);
// bttnSoMo.setOnClickListener(bSoMo);
// bttnHunter.setOnClickListener(bHunter);
mpKevin = new MediaPlayer();
mpKevin = MediaPlayer.create(getActivity(), R.raw.meow);
mpLecrae = new MediaPlayer();
mpLecrae = MediaPlayer.create(getActivity(), R.raw.dog_barking_sound);
mpLuke = new MediaPlayer();
mpLuke = MediaPlayer.create(getActivity(), R.raw.meow);
mpSkillet = new MediaPlayer();
mpSkillet = MediaPlayer.create(getActivity(), R.raw.dog_barking_sound);
mpSoMo = new MediaPlayer();
mpSoMo = MediaPlayer.create(getActivity(), R.raw.meow);
mpHunter = new MediaPlayer();
mpHunter = MediaPlayer.create(getActivity(), R.raw.dog_barking_sound);
playing = 0;
}
@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);
}
}