我想我的活动改为片段。我的代码如下所示。帮助将不胜感激。我花两天时间没有任何成功。我已经修改了很多我上面的代码,但未能解决这个问题。最后五行混淆了我太多。我也用一个MediaPlayerPool类我认为这是不产生问题。
package com.example.soundpoolexample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private MediaPlayerPool mediaPlayerPool;
Button button;
Button button2;
class b1 implements OnTouchListener {
b1() {
}
public boolean onTouch(View view, MotionEvent motionEvent) {
view.setPressed(true);
if (motionEvent.getAction() == 0) {
MainActivity.this.mediaPlayerPool.play("android.resource://com.example.soundpoolexample/raw/a1");
return true;
} else if (motionEvent.getAction() != 1 && motionEvent.getAction() != 3) {
return false;
} else {
MainActivity.this.mediaPlayerPool.stop("android.resource://com.example.soundpoolexample/raw/a1");
return false;
}
}
}
class b2 implements OnTouchListener {
b2() {
}
public boolean onTouch(View view, MotionEvent motionEvent) {
view.setPressed(true);
if (motionEvent.getAction() == 0) {
MainActivity.this.mediaPlayerPool.play("android.resource://com.example.soundpoolexample/raw/a1");
return true;
} else if (motionEvent.getAction() != 1 && motionEvent.getAction() != 3) {
return false;
} else {
MainActivity.this.mediaPlayerPool.stop("android.resource://com.example.soundpoolexample/raw/a1");
return false;
}
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView((int) R.layout.activity_main);
this.button = (Button) findViewById(R.id.button);
this.button2 = (Button) findViewById(R.id.button2);
this.mediaPlayerPool = new MediaPlayerPool(this);
this.button.setOnTouchListener(new b1());
this.button2.setOnTouchListener(new b2());
}
}
创建一个新的布局文件。
你的情况,这将是目前的activity_main.xml中文件的副本。
就我而言,我只是用红色背景。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00">
</FrameLayout>
创建一个新的Java类文件。
从Fragment
扩展android.support.v4.app.Fragment
并覆盖onCreateView()
。
public class MainFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
}
里面onCreateView()
,膨胀和返回布局从1.创建片段布局。
return inflater.inflate(R.layout.fragment_layout, container, false);
移动oncreate()
在MainActivity
实施onViewCreated()
到MainFragment
。
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
this.button = (Button) findViewById(R.id.button);
this.button2 = (Button) findViewById(R.id.button2);
this.mediaPlayerPool = new MediaPlayerPool(this);
this.button.setOnTouchListener(new b1());
this.button2.setOnTouchListener(new b2());
}
你应该在onCreate()
离开MainActivty
如下。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_java);
}
将一切按原样。
在activity_main.xml中,使用MainFragment
与<fragment>
标签。
<FrameLayout ... >
<fragment
android:id="@+id/main_fragment"
android:name="com.example.blockrecyclerview.MainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
这将导致以下屏幕。