【问题标题】:Want to change Activity to Fragment想要将 Activity 更改为 Fragment
【发布时间】:2019-02-12 08:48:19
【问题描述】:

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

}

}

【问题讨论】:

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


    【解决方案1】:

    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。创建 Fragment 类

    创建一个新的 Java 类文件。

    android.support.v4.app.Fragment 扩展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

    MainActivity 实现中的oncreate() 移动到MainFragment 中的onViewCreated()

    @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&lt;fragment&gt;标签。

    <FrameLayout ... >
    
    <fragment
            android:id="@+id/main_fragment"
            android:name="com.example.blockrecyclerview.MainFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </FrameLayout>
    

    它会导致以下屏幕。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多