简体   繁体   English

Android Master / Detail流程-在Activity中操作Detail片段视图

[英]Android Master/Detail flow - manipulating Detail fragment Views in Activity

So I am new to Android development, and trying to wrap my head around the Master/Detail flow design. 因此,我是Android开发的新手,并试图围绕Master / Detail流程设计工作。 As such, I'm using the default classes Eclipse creates for you - so ScreenDetailActivity , ScreenDetailFragment , ScreenListActivity , and ScreenListFragment . 因此,我使用的是默认的Eclipse类为您创建-所以ScreenDetailActivityScreenDetailFragmentScreenListActivityScreenListFragment One of my Detail fragments uses a layout that contains a series of checks and fields for entering data, and a button that is supposed to make the Activity class using the fragment pass that data to a calculator class, which then performs some basic calculations with the data. 我的一个Detail片段使用一个布局,该布局包含用于输入数据的一系列检查和字段,以及一个按钮,该按钮应该使用该片段使Activity类将数据传递给计算器类,然后该计算器类执行一些基本计算数据。 For example, found in the calculation_layout.xml file used by one of the Detail fragment in question: 例如,在有问题的Detail片段之一使用的calculation_layout.xml文件中找到:

<EditText android:id="@+id/edit_value"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:inputType="numberDecimal|numberSigned"
      android:hint="@string/background_value" />

<Button android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="@string/button_singleCalc"
    android:onClick="calculate" />

I think I have the the Button's calculate function working (ie the app doesn't crash when the calculate is empty or does trivial things); 我认为我的按钮的calculate功能正常工作(即,当计算为空或执行琐碎的事情时,应用程序不会崩溃); it's implemented in both ScreenListActivity and ScreenDetailActivity , since either could be using the fragment. 它可以在ScreenListActivityScreenDetailActivity ,因为两者都可以使用该片段。

However, whenever I try to access the EditText objects in the Detail fragment, the app crashes. 但是,每当我尝试访问Detail片段中的EditText对象时,应用程序就会崩溃。 I'm trying something like this: 我正在尝试这样的事情:

public void calculate(View view){
    //Retrieve all the information, and set the values in the Calculator
    EditText editText = (EditText) findViewById(R.id.edit_value);
    String number = editText.getText().toString();
    double angle = Double.parseDouble(number);

    Calculator.longCalc();
}

And inflating the layout in my ScreenDetailFragment like this, not unlike how the default method generated by Eclipse works (where mItem is basically an instance of a little class containing information on which fragment should be displazed): 然后像这样使我的ScreenDetailFragment中的布局膨胀,这与Eclipse生成的默认方法的工作原理没有什么不同(mItem本质上是一个小类的实例,其中包含有关应显示哪个片段的信息):

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // The returned view
        View rootView;
        // If mItem is non-null...
        if (mItem != null) {
            if (mItem.title == "Calculation") {
                // If the title is Calculation, use the calculation layout
                rootView = inflater.inflate(R.layout.calculation_layout, container, false);
            } else {
                // Otherwise, show the dummy content as text in a TextView
                rootView = inflater.inflate(R.layout.fragment_screen_detail, container, false);
                ((TextView) rootView.findViewById(R.id.screen_detail)).setText(mItem.title);
            }
        } else {
            rootView = inflater.inflate(R.layout.fragment_screen_detail, container, false);
        }

        return rootView;
    }

The result, as said earlier, is a crash. 如前所述,结果是崩溃。

I assume that what I am supposed to do is somehow access rootView from the Activity, but I don't really know how to do that safely and effectively. 我假设我应该做的是以某种方式从Activity中访问rootView ,但是我真的不知道如何安全有效地做到这一点。

Can someone give me some pointers here? 有人可以给我一些指示吗?

UPDATE: 更新:

I have tried implementing OnClickListener, setting it up as such when that particular layout is inflated: 我试过实现OnClickListener,在膨胀特定布局时将其设置为:

((Button)rootView.findViewById(R.id.button_calc)).setOnClickListener(this);

and implementing the onClick(View) function as such: 并像这样实现onClick(View)函数:

public void onClick(View view) {
        //Retrieve all the information, and set the values in the Calculator
        view = (View) view.getParent();
        EditText editText = (EditText) layout.findViewById(R.id.edit_phiD);
        String number = editText.getText().toString();

        Calculator.angle = Double.parseDouble(number) * 2.0 * Math.PI/360.0;

        Calculator.longCalc();
    }

However, the error persists. 但是,错误仍然存​​在。 It also persists if I recast the ViewParent to a LinearLayout , a ViewGroup , or if I use view straight as is comes. 如果将ViewParent重铸为LinearLayoutViewGroup ,或者如果我直接使用view ,它也将持续存在。 To be clear, I am trying to get at the parent layout of the button that was clicked, so that I can go back down into that layout's other child Views and access their states. 为了清楚起见,我试图获得被单击按钮的父级布局,以便可以返回到该布局的其他子Views并访问其状态。

You don't need to go through your activity in order to achieve this. 您无需完成任何活动即可实现这一目标。 Remove the onclick line and add an id for the button in your layout: 删除onclick行,并在布局中为该按钮添加一个ID:

<Button android:id="@+id/calc_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:text="@string/button_singleCalc" />

Then, simply add an OnClickListener to your button within your Fragment. 然后,只需在片段中的按钮上添加一个OnClickListener即可。 Something like that: 像这样:

private View mRootView;
private  EditText mEditText;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // If mItem is non-null...
    if (mItem != null) {
        if (mItem.title == "Calculation") {
            // If the title is Calculation, use the calculation layout
            mRootView = inflater.inflate(R.layout.calculation_layout, container, false);
            mEditText = (EditText) mRootView.findViewById(R.id.edit_phiD);    
            ((Button)mRootView.findViewById(R.id.calc_button)).setOnClickListener(this);         
        } else {
            // Otherwise, show the dummy content as text in a TextView
            mRootView = inflater.inflate(R.layout.fragment_screen_detail, container, false);
            ((TextView) mRootView .findViewById(R.id.screen_detail)).setText(mItem.title);
        }
    } else {
        mRootView = inflater.inflate(R.layout.fragment_screen_detail, container, false);
    }

    return mRootView;
}

(Your fragment needs to implement OnClickListener for this). (您的片段需要为此实现OnClickListener)。 Then you'll get a callback on your Fragment, and here's what you do there: 然后,您将在Fragment上获得回调,这是您在此处执行的操作:

public void onClick(View v){
    //Retrieve all the information, and set the values in the Calculator
    String number = mEditText.getText().toString();
    double angle = Double.parseDouble(number);

    Calculator.longCalc();
}

https://developer.android.com/training/basics/fragments/index.html https://developer.android.com/training/basics/fragments/index.html

This is the best example, there are two fragments headlinesfragment and articlefragment with dynamic UI, follow how they have interface on articlefragment to let which headline is picked. 这是最好的示例,标题片段和文章片段有两个带有动态UI的片段,然后按照它们在文章片段上的界面来选择哪个标题。 Follow the tutorial properly. 正确遵循本教程。 You should understand it well. 您应该很好理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM