通过向现有ListView添加片段来丢失数据

时间:2013-03-22 11:00:24

标签: java android android-listview fragment

我正在尝试使用页脚填充ListView。出于这个原因,我创建了一个片段。但是我在将数据传递给片段时遇到了问题。在一个不同的地方它工作得很好,就像我做的那样。

    listView1 = (ListView) findViewById(R.id.listView1);     

    MenueAdapter adapter = new MenueAdapter(MainActivity.this,
            R.layout.mainview_menue_item_row, data_Storage_news, 
            getWindowManager().getDefaultDisplay().getWidth());    


    Intent intent_onTv = new Intent(context,
            OnTvFragment.class);

    intent_onTv.putExtra("data_Storage_tv", data_Storage_tv);

    OnTvFragment frag = new OnTvFragment();
    frag.setArguments(intent_onTv.getExtras());
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();        
    ft.commit();    

    LayoutInflater inflater = getLayoutInflater();      
    View footer = inflater.inflate(R.layout.ontv_fragment, null);       

    getSupportFragmentManager().executePendingTransactions();

    listView1.addFooterView(footer);       
    listView1.setAdapter(adapter);

Fragment包含一个ViewFlipper,在另一个活动中完美运行。

任何人都知道我做错了什么或者能告诉我一个正确的方法是怎么做的?

1 个答案:

答案 0 :(得分:0)

将片段作为页脚添加到ListView - 我的解决方案:

ListView.LayoutParams lp = new ListView.LayoutParams(ListView.LayoutParams.WRAP_CONTENT, ListView.LayoutParams.WRAP_CONTENT);        

    View footer = new View(listView1.getContext()); 

    footer = ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.on_tv_fragment_empty, null);        
    footer.setLayoutParams(lp);     

    OnTvFragment frag = new OnTvFragment();
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();        
    ft.replace(R.id.ll, frag).commit();       
    getSupportFragmentManager().executePendingTransactions();

    listView1.addFooterView(footer);
    listView1.setAdapter(adapter);

确保footer正在为一个空的FrameLayout充气,如:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"     
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"   
    android:background="@layout/gradient_box"
    android:id="@+id/ll"
    />        

避免以下错误: How to make fragment transaction when clicked on a listitem?

我的结果如下: enter image description here

希望这有助于任何人:)