2013-05-03 55 views
1

我正在处理Fragment-ListView并在图像中显示输出。 每一次点击改变fragment2.till的TextView现在好了。不同布局的ListFragment

fragmetn1用于listView,fragment2用于不同的视图。

现在我想要在点击更改fragment2中的布局。例如当item2被点击时,带有textView的布局在fragment2中设置。现在随着item3的选择,使用Button的不同布局应该设置为fragment2。

我的getView代码在这里。

public void onListItemClick(ListView l, View v, int position, long id) { 

     DetailFrag frag = (DetailFrag) getFragmentManager().findFragmentById(R.id.frag_detail); 

     if (frag != null && frag.isInLayout()) { 
      frag.setText("item "+position+" selected"); 
     } 
    } 

有没有其他办法可以做到这一点,PLZ也有助于这种方式。

任何建议将强烈感谢。提前感谢。

enter image description here

回答

0

试试下面的方法

FragmentManager fragmentManager = getFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 

,那么你需要你的片段具有以下语法添加到该fragmentTransaction。

fragmentTransaction.replace(R.id.detailFragment, layout1); 

最后你必须提交你的交易。否则,更改将不会持续。

fragmentTransaction.commit(); 

有关简要示例,请参阅此博客post。我希望这能帮到您。

谢谢。

0

在我看来,你应该使用不同的布局和使用FragmentTransaction来替换现有的片段。

0

您的方法可能正常工作,但最好使用更松散耦合的方法,其中ListFragment通知托管活动某个项目已被单击,然后该活动告诉“其他”片段进行更新。

无需担心构建和替换新的碎片。

样品活性

public class TestActivity extends Activity implements TestFragmentBListener { 
    private TestFragmentA mOtherFragment; 
    private TestFragmentB mListFrag; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_test); 
     mListFrag = (TestFragmentB) getFragmentManager().findFragmentById(R.id.fragment_b_list); 
     mOtherFragment = (TestFragmentA) getFragmentManager().findFragmentById(R.id.fragment_a_text); 
    } 

    @Override 
    public void onTestFragmentBItemCLicked(int position) { 
     // Implemented from TestFragmentB.TestFragmentBListener 
     mOtherFragment.setText("item " + position + " selected."); 
    } 
} 

样品FragmentA(包含文本..这不是功能性的,仅一个例子)

public class TestFragmentA extends Fragment { 
    public void setText(String text) { 
    } 
} 

样品ListFragment限定用于所述样品的界面活动实施

public class TestFragmentB extends ListFragment { 
    private TestFragmentBListener mListener = null; 

    public interface TestFragmentBListener { 
     void onTestFragmentBItemCLicked(int position); 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     try { 
      mListener = (TestFragmentBListener) activity; 
     } catch (ClassCastException ccex) { 
      // activity does not implement our listener 
     } 
    } 

    @Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 

     if (mListener != null) { 
      mListener.onTestFragmentBItemCLicked(position); 
     } 
    } 
} 

和完整起见,这里是我的样品活动布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".TestActivity" > 

    <fragment 
     android:id="@+id/fragment_a_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <fragment 
     android:id="@+id/fragment_b_list" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/fragment_a_text" /> 

</RelativeLayout>