如何在 PlaceHolderFragment 的 onPause 方法中使用 findViewById 找到我的 textview

How to find my textview with findViewById in my onPause method in PlaceHolderFragment

我在我的应用程序中使用选项卡布局,我有一个按钮可以启动计时器然后停止它。问题出在我的 placeholderfragment 中,我只能使用 root.FindViewById 在我的 onCreateView() 方法中找到文本视图。

当然我无法在 onCreateView() 方法之外访问 root。有什么办法可以解决这个问题吗?这是我的占位符片段代码:

public class PlaceholderFragment extends Fragment {

    private static final String ARG_SECTION_NUMBER = "section_number";

    private PageViewModel pageViewModel;
    Context thiscontext;

    TextView timerView;
    long startTime = 0;

    Handler timerHandler = new Handler();
    Runnable timerRunnable = new Runnable() {

        @SuppressLint("DefaultLocale")
        @Override
        public void run() {

            long millis = System.currentTimeMillis() - startTime;
            int seconds = (int) (millis / 1000);
            int minutes = seconds / 60;
            seconds = seconds % 60;

            timerView.setText(String.format("%d:%02d", minutes, seconds));

            timerHandler.postDelayed(this,500);
        }
    };
    public static PlaceholderFragment newInstance(int index) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ARG_SECTION_NUMBER, index);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageViewModel = ViewModelProviders.of(this).get(PageViewModel.class);
        int index = 1;
        if (getArguments() != null) {
            index = getArguments().getInt(ARG_SECTION_NUMBER);
        }
        pageViewModel.setIndex(index);


    }

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_main, container, false);
        thiscontext = container.getContext();

        final TextView textView = root.findViewById(R.id.section_label);
        pageViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });

        timerView = (TextView) root.findViewById(R.id.hoursConnected);

        Button b = root.findViewById(R.id.b);
        b.setText("start");
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Button b = (Button) v;

                if(b.getText().equals("stop")){
                    timerHandler.removeCallbacks(timerRunnable);
                    b.setText("start");
                }else{
                    startTime = System.currentTimeMillis();
                    timerHandler.postDelayed(timerRunnable, 0);
                    b.setText("stop");
                }
            }
        });

        return root;
    }

----------------// HERE IS THE PROBLEM //----------------------------------------------------------------

    @Override
    public void onPause(){
        super.onPause();

        timerHandler.removeCallbacks(timerRunnable);
        Button b = findViewById(R.id.b); ----------------// THIS IS NOT WORKING //----------------
        b.setText("start");
    }

我们所做的是定义 class 的属性来存储视图引用,然后将它们保存在 onCreate 方法中。这样您就可以在整个 class.

期间使用它们

例如,如果您想访问 R.id.b

public class PlaceholderFragment extends Fragment {
    Button button;


    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

            ...

            button = root.findViewById(R.id.b);
            ...
        }

        @Override
        public void onPause(){
            super.onPause();

            timerHandler.removeCallbacks(timerRunnable);
            button.setText("start");
        }
}

如果要重复使用此视图,您应该了解一下 ViewHolder 的工作原理。在这两种情况下,最好在视图膨胀时保存引用。