【问题标题】:Can I limit setText to within the scope of an individual fragment?我可以将 setText 限制在单个片段的范围内吗?
【发布时间】:2017-11-02 21:58:06
【问题描述】:

在我的应用程序中,我有同一个片段 ActivityFragment 的多个实例。在每个片段中,都有一个activity_text 文本视图。将片段添加到布局时,我想在onCreate 期间在该片段中设置activity_text 文本视图。但是,当我尝试这样做时,屏幕上的每个 ActivityFragment 都会更改其 activity_text 文本视图。

有什么方法可以将setText 限制在单个片段的范围内,而无需为每个片段使用唯一的标签或 ID?

这是我的 ActivityFragment 类:

public static class ActivityFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, Bundle savedInstanceState) {
        final View fragment1 = inflater.inflate(R.layout.activity_fragment, container, false);

        final TextView activityText = (TextView) fragment1.findViewById(R.id.activity_text);
        //Calling setText changes the activityText Textview in every fragment onscreen
        activityText.setText(text);


        return fragment1;
    }
}

这是我的 MainActivity 类:

public class MainActivity extends FragmentActivity {
    Static String text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        for (int i = 0; i != 5; i++) {
            //This ensures each fragment receives a unique String
            text = "success" + i;
            ActivityFragment myFragment = new ActivityFragment();
            getFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, myFragment).commit();
        }
    }
}    

【问题讨论】:

  • 您能更具体地回答您的问题吗? “将 setText 限制在单个片段的范围内”到底是什么意思?您可以在 onDetach 上覆盖诸如 onAttach 之类的方法来处理片段被附加的那一刻。您还可以为每个片段通过 bundle 发送数据。这就是你想要的吗?
  • @VictorNeves 根据提供的答案,我应该通过 Bundle 将数据传递给片段。很抱歉这个令人困惑的问题。

标签: android android-fragments


【解决方案1】:

您的代码似乎有两个问题。

首先是您使用static String text 将信息从您的活动传输到您的片段。一般来说,这不是一个好主意。如果您需要在构建时将 Activity 中的某些内容“传递”到 Fragment,您应该使用 Fragment.setArguments(Bundle) 来执行此操作。

第二个是你似乎对FragmentTransactions的时机有误解。您的 for 循环会更改 text 的值,然后将 .commit()s 更改为新片段五次。但这些事务是异步的,片段生命周期有自己的时间线。所以你的onCreateView()实际上会在你的活动onCreate()完成后被调用,所以你的五个片段中的每一个都从text获得相同的值。

以下是我的解决方法:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        for (int i = 0; i != 5; i++) {
            Bundle args = new Bundle();
            args.putString("text", "success" + i);

            ActivityFragment myFragment = new ActivityFragment();
            myFragment.setArguments(args);

            getFragmentManager().beginTransaction()
                                .add(R.id.fragment_container, myFragment).commit();
        }
    }

    public static class ActivityFragment extends Fragment {
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, Bundle savedInstanceState) {
            final View fragment1 = inflater.inflate(R.layout.activity_fragment, container, false);

            final TextView activityText = (TextView) fragment1.findViewById(R.id.activity_text);
            String text = getArguments().getString("text");
            activityText.setText(text);

            return fragment1;
        }
    }
}

【讨论】:

    【解决方案2】:

    您的问题与文本视图的范围无关。 每个片段都按照您的指示将 textView 单独设置为text。发生这种情况是因为 onCreateView 在所有片段事务发生之后才被调用。

    在您的代码中,您添加了注释://This ensures each fragment receives a unique String,实际上,没有任何东西可以确保片段接收到任何东西。我建议您阅读 static 在 Java 中的含义。在 for-loop 上,您连续 5 次更改 text 的值,最后一个值为“success4”。

    将参数传递给片段的正确方法是通过 Bundle 添加调用方法 setArguments(Bundle)

    我搜索了这个答案https://stackoverflow.com/a/35737232/906362 很好地解释了如何做到这一点。要点是这样的:

        for (int i = 0; i != 5; i++) {
            ActivityFragment myFragment = new ActivityFragment();
            Bundle b = new Bundle();
            // This really ensures each fragment receives a unique String
            b.putString("text", "success" + i)
            myFragment.setArguments(b);
            getFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, myFragment).commit();
        }
    

    然后在片段上获取这个值:

      TextView activityText = (TextView) fragment1.findViewById(R.id.activity_text);
      activityText.setText(getArguments().getString("text");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 2018-09-15
      • 2023-03-26
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      相关资源
      最近更新 更多