【问题标题】:Can I limit findViewById() to within the scope of an individual fragment?我可以将 findViewById() 限制在单个片段的范围内吗?
【发布时间】:2017-07-27 14:51:45
【问题描述】:

在我的应用程序中,我在 LinearLayout 中显示了同一片段 Activity_Fragment 的多个实例。在每个片段中,都有一个activity_text 文本视图和一个edit_button。当在片段中按下按钮时,它应该更改同一片段中 textview 的文本。但是,当添加片段的多个实例时,在任何片段中按下任何edit_button 只会更改添加到LinearLayout 的第一个片段中的activity_text 文本视图。这是因为所有片段都是相同的,并且它们的子视图共享相同的 id。每个片段应该相互独立;在片段中按下的任何edit_button 都应该只影响该片段。

是否可以将 findViewById() 方法限制在单个片段的范围内,以便一个片段不会受到另一个片段中的事件的影响,或者我是否必须以某种方式更改每个视图的 ID创建时的片段?

这里是活动片段 xml:

 <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/activity_fragment">

        <CheckBox
            android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginTop="16dp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/activity_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="5dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:text="Edit Activity"
            android:textSize="18sp"
            app:layout_constraintBaseline_toBaselineOf="@+id/checkbox"
            app:layout_constraintLeft_toRightOf="@+id/checkbox"
            app:layout_constraintRight_toLeftOf="@+id/edit_button"/>

        <Button
            android:id="@+id/edit_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginTop="16dp"
            android:onClick="editActivity"
            android:text="Edit Activity"
            app:layout_constraintBaseline_toBaselineOf="@+id/checkbox"
            app:layout_constraintRight_toRightOf="parent"/>

这是我的带有 ActivityFragment 类的 MainActivity.java。 edit_button 的 onClick 方法在最底部:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    public static class ActivityFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup 
        container, Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.activity_fragment, container, 
            false);
        }
    }

    public void addActivity(View view) {
        ActivityFragment fragment1 = new ActivityFragment();
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, fragment1).commit();
    }

    //This is the editButton method
    public void editActivity(View view) {
        TextView activityText = (TextView) findViewById(R.id.activity_text);
        activityText.setText("success");
    }
}

【问题讨论】:

    标签: android android-fragments findviewbyid android-identifiers


    【解决方案1】:

    您的问题可以通过限制findViewById() 扫描的范围来解决。如果您想查看特定片段,您可能需要调用myFragment.getView().findViewById()(而不是仅使用活动中的findViewById())。

    那么你怎么能做到这一点?好吧,您设置事物的方式并不容易。由于您使用android:onClick 属性来设置您的点击监听器,您必须从活动内部处理事情。

    所以不要使用android:onClick

    相反,在 Fragment 的 onCreateView() 方法中,编写如下内容:

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.your_fragment, container, false);
    
        Button button = (Button) root.findViewById(R.id.edit_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView activityText = (TextView) getView().findViewById(R.id.activity_text);
                activityText.setText("success");
            }
        });
    
        return root;
    }
    

    【讨论】:

    • 谢谢!我刚开始使用 Android,而且我一直在使用 Android 开发人员指南,所以您所做的大部分更改对我来说都是新的,我不太了解其中的一些。主要是@Override@Nullableroot.OnClickListener。 Android 培训或 API 指南稍后会介绍这一点吗?您是否有链接或知道我可以在哪里阅读更多相关信息?
    • @Override@Nullable 可以忽略。它们是帮助您编写不会中断的代码的注释,但它们不会影响编写良好的程序的行为。 root 只是我喜欢在Fragment.onCreateView() 中使用的变量名(希望您可以在片段教程中阅读)。 View.OnClickListenerandroid:onClick 属性的概念相同,但以不同的方式完成。您可以在 Button 文档中阅读一些关于它的信息:developer.android.com/reference/android/widget/Button.html。最终,所有这些都将成为第二天性。
    【解决方案2】:

    您可以使用view.findViewById(R.id.activity_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
      相关资源
      最近更新 更多