想要将活动更改为片段

时间:2019-02-12 08:48:20

标签: java android android-fragments android-activity android-fragmentactivity

我想将活动更改为片段。我的代码如下。帮助将不胜感激。我花了两天没有成功。我已经修改了我上面的代码很多,但无法解决此问题。最后五行让我非常困惑。我还使用了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());

}

}

1 个答案:

答案 0 :(得分:2)

1。创建片段布局

创建一个新的布局文件。

在您的情况下,它将是当前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>

2。创建片段类

创建一个新的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);
    }
}

3。膨胀布局

onCreateView()内,从 1充气并返回布局。创建片段布局

return inflater.inflate(R.layout.fragment_layout, container, false);

4。将实现从MainActivity移至MainFragment

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);
}

按原样移动其他所有内容。

5。在MainActivity中使用MainFragment

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>

它会显示以下屏幕。

Result Image