English 中文(简体)
十三、能力建设与重建观点
原标题:Android force Fragment to rebuild View

我有两块零碎块的简单图象,在地貌模式中,两块碎块被一旁边和在孔隙中显示出分裂的A,然后如果他们选择一种选择,就开始显示分裂的B的活动。 我的问题是,如果用户选择一个菜单,我想要更新或重新提出与B分裂相关的观点,并且能够不理解如何开展这项工作,那么我就在这种模式中表现出了分裂。 我尝试了收听方法,并采用了LayoutInflater方法,但由于我认为我正在形成一种新的观点,所以屏幕没有变化。 我还试图提及“分裂”A思想,我可以称之为其例行做法,以建造新的碎片,取代“框架B”,但不能因为没有展示而查阅。 我确实需要做的是,强行要求重新唤醒新观点的方法,但我尝试了几种方法,试图这样做,但不能再打上《刑法》。 对如何做到这一点有什么想法?

问题回答

您将采用<代码>Fragment Transaction,并使用replace()方法。

这样做太困难,但答案取决于您的所在地。 Menu(即:onOptionsItemS(<>/code>)在您的母体内打电话。 活动或活动是否在A/B节中?

为了简单起见,您的菜单执行和onOptionsItemS()的证明属于母体活动,如果选择菜单,你想要重塑碎块。 它想看一下:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
//...
switch (item.getItemId()) {
case R.id.menu_option1:
    //do something
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    Fragment newFragment = new YourFragmentClass();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.your_fragment_id, newFragment);
    transaction.addToBackStack(null);
    transaction.commit();
    return true;
case R.id.menu_option2:
    //do something else;
    return true;
default:
    return super.onOptionsItemSelected(item);
}
}

Alternatively, if your menu is a child of one of your Fragments (which it should be for the sake of more reusable code), then one method is to require that host Activity to implement an interface defined by the Fragment, that can be used as a call back. And in the onOptionsItemSelected() callback inside your fragment class, you simply make a call to this callback method.

尽管它像一个口号一样健全,但你确实需要做两件事。 认为这是你的分裂阶层

public static class FragmentA extends ListFragment {
OnSelectedListener mListener;
// Container Activity must implement this interface
public interface OnSelectedListener {
    public void onSelected();
}
...
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    //This is to ensure that the Activity has implemented the interface we set up above
    try {
        mListener = (OnSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnSelectedListener");
    }
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
//...
switch (item.getItemId()) {
case R.id.menu_option1:
    //do something
    getActivity().onSelected();
    return true;
case R.id.menu_option2:
    //do something else;
    return true;
default:
    return super.onOptionsItemSelected(item);
}
}
...
}

然后,你们会看到:

public class MainActivity extends Activity implements FragmentA.onSelectedListener{
...
public void onSelected(){
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    Fragment newFragment = new YourFragmentClass();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.your_fragment_id, newFragment);
    transaction.addToBackStack(null);
    transaction.commit();
}
}

如果用户选择菜单,我想重述或重述与零碎块B相关的观点,并且不能理解如何使这项工作成为现实。

onOptionsItemS selected()中,该活动用一种方法处理碎片,导致其用新内容更新其植被。 或者,该活动实施了一个<代码>Fragment Transaction,以替换碎片(如果碎片最初是通过<代码>Fragment Transaction建立的)。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签