【问题标题】:ListFragment with different Layout具有不同布局的 ListFragment
【发布时间】:2013-04-27 21:57:27
【问题描述】:

我正在处理 Fragment-ListView 并在图像中显示输出。 每次点击都会改变fragment2的TextView。到现在还可以。

fragmetn1 用于 listView,frag22 用于 DifferentView。

现在我想通过单击更改 fragment2 中的布局。例如单击 item2 时,在 fragment2 中设置了带有 textView 的布局。现在选择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");
        }
    }

还有其他方法可以做到这一点,请您也帮忙。

任何建议都将不胜感激。在此先感谢。

【问题讨论】:

    标签: android android-listfragment


    【解决方案1】:

    试试下面的方法

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

    那么您需要使用以下语法将您的片段添加到此片段事务中。

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

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

    fragmentTransaction.commit();
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        您的方法可能工作正常,但最好使用更松散耦合的方法,其中 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.");
            }
        }
        

        Sample FragmentA(包含文本..这不是功能性的,只是一个示例)

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

        为示例 Activity 定义接口的示例 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>
        

        【讨论】:

          猜你喜欢
          • 2012-05-23
          • 2012-07-23
          • 2011-04-08
          • 2017-07-22
          • 1970-01-01
          • 2012-07-31
          • 2020-06-13
          • 2016-12-20
          • 1970-01-01
          相关资源
          最近更新 更多