android - 如何检索在 XML 中定义的 fragment 实例?

标签 android android-fragments android-view

简而言之,问题是我可以通过 id 找到我的元素,但它一直要求我将其转换为 View ,但我无法将其转换为我的对象名称的类型。我不确定转换到 View 是否是它应该的样子,但 Android 示例只是创建 fragment 并添加它,因此它没有在 xml 中定义。但关键是在示例中,对象不是 View 类型。

更多信息: 这可能是一种快速简便的修复方法。我有一个 fragment 类,它包含我用代码创建的自定义 View (这是一种不断绘制的游戏)。我在我的 xml 布局的左上部分添加了 fragment 。 fragment 下方是按钮(在 xml 布局中定义)。我有一个主要 Activity ,我想要一个包含我的自定义 View 的 fragment 实例的句柄,以便在单击按钮时, fragment 中的一些变量会发生变化并重新绘制 View 。

XML:

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <fragment android:name="com.example.dcubebluetooth"
              android:id="@+id/lEDView1"
              android:layout_width="400px"
              android:layout_height="400px" />

<Button
        android:id="@+id/layer1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/lEDView1"
        android:layout_marginTop="50px"
        android:text="Layer1"
        android:textSize="12dp" />

    <Button
        android:id="@+id/layer2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/layer1"
        android:layout_toRightOf="@id/layer1"
        android:text="Layer2"
        android:textSize="12dp" />

    <Button
        android:id="@+id/layer3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/layer2"
        android:layout_toRightOf="@id/layer2"
        android:text="Layer3"
        android:textSize="12dp" />

    <Button
        android:id="@+id/layer4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/layer3"
        android:layout_toRightOf="@id/layer3"
        android:text="Layer4"
        android:textSize="12dp" />

</RelativeLayout>

主要 Activity :

public class MainActivity extends Activity implements LEDGridFragment.TriggerBlueTooth{

    Button l1;
    Button l2;
    Button l3;
    Button l4;
    BluetoothService bt;
    LEDGridFragment gridUI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialize();
    }

@Override
public void onledSelected(int layer, int led) {
    // TODO Auto-generated method stub
    String msg = Integer.toString(layer);
    if(led<10) msg+=0;
    msg+= Integer.toString(led);
    bt.write(msg);
}

//Full method below not showing. I don't know why

    public void initialize(){
gridUI = findViewById(R.id.lEDView1);

bt = new BluetoothService();

l1 = (Button) findViewById(R.id.layer1);
l2 = (Button) findViewById(R.id.layer2);
l3 = (Button) findViewById(R.id.layer3);
l4 = (Button) findViewById(R.id.layer4);

l1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        gridUI.currentLayer = 0;
        gridUI.update();
    }
});

l2.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        gridUI.currentLayer = 1;
        gridUI.update();
    }
});
l3.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        gridUI.currentLayer = 2;
        gridUI.update();
    }
});

l4.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        gridUI.currentLayer = 3;
        gridUI.update();
    }
});

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


}

fragment :

public class LEDGridFragment extends Fragment{

    LEDView view;
    TriggerBlueTooth mCallBack;

    boolean[][][] states = new boolean[4][4][4];

    int currentLayer = 0;

    public interface TriggerBlueTooth{
        public void onledSelected(int layer, int led);
    }

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

        mCallBack = (TriggerBlueTooth) activity;

        view.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int x = (int) (event.getX())/100;
                int y = (int) (event.getY())/100;
                int led = y+(x*4);

                mCallBack.onledSelected(currentLayer, led); //Notify Main Activity of the led that was toggled
                                                            //So it can send data over Bluetooth
                return false;
            }
        });
    }

    public void update(){
        view.drawGrid(states, currentLayer); //Update the view
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        for(int i=0; i<states.length; i++){  //Initialize the board off which the view will be drawn
            for(int j=0; j<states[0].length; j++){
                for(int k=0; k<states[0][0].length; k++){
                    states[i][j][k] = false;
                }
            }
        }

        view = new LEDView(getActivity());
        return view;
        //return super.onCreateView(inflater, container, savedInstanceState); //Auto-formed. Commented out
    }

}

另一方面(如果你不回答这个问题,好吧):图形布局没有显示它在游戏初始绘图方面的假设。如果我添加 View 本身,它确实如此。但现在,它是一个带有“从‘ fragment 布局’上下文菜单中选择预览布局”的灰色框 这个菜单在哪里?我想看看我的观点是否有效。我无法亲自测试它,因为 1. 它无法编译,并且 2. 我没有 android 手机

最佳答案

应该这样做:

FragmentManager mgr = getFragmentManager();
Fragment fragment = mgr.findFragmentById(R.id.lEDView1);

关于android - 如何检索在 XML 中定义的 fragment 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17118339/

相关文章:

android - 当我设置最小高度时,我使用的是什么单位?

android - 将 Android 添加到 Phonegap 项目时出错

android - 因 JVM 堆空间耗尽而导致守护进程到期?

Android fragment 与 Activity 组

android - ListView 没有显示在 fragment 中?

android - 类不是 View android.support.v4.app.Fragment

java - 我的表 (SQLite) 有什么问题吗?

android - 使用Android Studio 3.5中的Build Variant重命名应用程序的存档名称

android - MaterialSearchView android中的OnClick项目选择

android - ViewStub vs View.GONE vs Inflate vs ViewSwitcher