0

我正在构建一个经典的菜单/细节片段平板电脑活动。

我的详细信息片段是由外部库生成的 Graph。这个库给了我一个意图或我的图表的视图。

当我点击我的菜单时,我想刷新我的图表。

这是我的活动代码:

public class myActivity extends FragmentActivity {

Context context;
Graphical graphical;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    context = this;
    setContentView(R.layout.fragment_suivi_conso_graph);
}

/**
 * Called when clicking on the menu Fragment
 */
public void onChooseGraph(UrlAction urlAction){
    GraphFragment graphFragment = (GraphFragment)                         getSupportFragmentManager().findFragmentById(R.id.graph_fragment);

    if (graphFragment == null || !graphFragment.isInLayout()) {
        Intent intentGraph;
    intentGraph = graphical.executeForIntent(this, urlAction);
    startActivity(intentGraph);
    } 
    else {
       View graphView = graphFragment.loadGraph(urlAction);
           // I don't know what to do with this View !
    }
}

这是我的详细片段代码:

public class GraphFragment extends Fragment {

private Context context;
private Graphical graphical;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    context = this.getActivity();
    graphical = new Graphical();

    View graphView = null;
        graphView = graphical.executeForView(context, UrlAction.CONSO_WEEKS);
    return graphView;
}

public View loadGraph(UrlAction urlAction) {
    Log.d("GraphFragment","[loadGraph] START");
    View graphView = graphical.executeForView(context, urlAction);
            //This graphView contains the graph I want to display in my graphFragment
    return graphView;
}
 }

我不知道我是否可以将片段视图重新加载到片段的loadGraph函数中。或者我是否需要在onChooseGraph函数中进行活动。

在这两种情况下,我都不知道如何刷新。

4

1 回答 1

0

我找到了解决方案:

在我的详细片段上:GraphFragment,这里是正确的loadGraph方法:

    public void loadGraph(UrlAction urlAction) {
    Log.d(TAG,"[loadGraph] START, for urlAction : " + urlAction);

    View graphView = null;
    try {
        graphView = graphical.executeForView(context, urlAction);
    } catch (Exception e) {
        Log.e(TAG, "[loadGraph] Error during Parsing : "+e.getMessage());
        e.printStackTrace();
        if(e.getMessage().equals(XmlParserSuivitConso.XML_IS_ERROR)){
            Toast.makeText(context, getString(R.string.checkYouParameters), Toast.LENGTH_LONG).show();
            Intent intentSettings = new Intent(context, SettingsActivity.class);
            startActivity(intentSettings);
            getActivity().finish();
        }
    }

    setFragmentContentView(graphView);
}

void setFragmentContentView(View view) {
    LinearLayout.LayoutParams layoutParams = 
            new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 
                                          ViewGroup.LayoutParams.FILL_PARENT);
    mRootLayout.removeAllViews();
    mRootLayout.addView(view, layoutParams);
}

在活动中myActivity,我使用以下代码调用loadGraph

    public void onChooseGraph(UrlAction urlAction){
    GraphFragment graphFragment = (GraphFragment) getSupportFragmentManager().findFragmentById(R.id.graph_fragment);
    // On essaye de récupérer notre fragment.

    // S'il n'est pas présent, on le crée en lançant son Activity
    if (graphFragment == null || !graphFragment.isInLayout()) 
    {
        Intent intentGraph;
        try {
            intentGraph = graphical.executeForIntent(this, urlAction);
            startActivity(intentGraph);
        } catch (Exception e) {
            Log.e(TAG, "[onListItemClick] Error during Parsing : "+e.getMessage());
            e.printStackTrace();
            if(e.getMessage() == null || e.getMessage().equals(XmlParserSuivitConso.XML_IS_ERROR)){
                Toast.makeText(this, getString(R.string.checkYouParameters), Toast.LENGTH_LONG).show();
                Intent intentSettings = new Intent(this, SettingsActivity.class);
                startActivity(intentSettings);
                finish();
                return;
            }
        }
    } 
    else 
    // Sinon on met à jour le choix de l'utilisateur
    {
       graphFragment.loadGraph(urlAction);
    }

我希望它可以帮助一些人!

于 2012-07-10T10:23:29.073 回答