简体   繁体   English

如果片段在另一个布局中膨胀,如何获取对该片段的视图引用?

[英]How to get a reference of a view to a fragment if the fragment is inflated inside another layout?

I have a parent layout that is used to inflate various fragments. 我有一个用于放大各种片段的父布局。 On one such fragment layout I need to access the view elements inside the fragment class. 在一种这样的片段布局上,我需要访问片段类内部的view元素。 I'm using below code to get a reference for a text view inside the fragments layout. 我正在使用以下代码来获取片段布局内文本视图的引用。

CreateTaskFragment.java CreateTaskFragment.java

@Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        View view = getView();
        endDateView = (TextView)view.findViewById(R.id.estimated_end_input);
        endDateView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("Date","I'm clicked");
            }
        });
    }

The endDateView is returning null . endDateView返回null

fragment_create_task.xml fragment_create_task.xml

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.tetrissoft.dailyplanner.CreateTaskFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/estimated_end_date"
        android:textSize="15sp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="15dp"
        android:id="@+id/estimated_end_label"/>

     <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/task_end_date_input"
        android:textSize="18sp"
        android:inputType="date"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="15dp"
        android:layout_below="@+id/estimated_end_input"/>

</LinearLayout>

MainActivity.xml MainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/drawer_layout">

    <!-- The Main Content Layout -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="25dp"
            app:elevation="4dp"
            android:clickable="true"
            android:src="@drawable/ic_create_black_24dp"
            android:id="@+id/fab_button"/>
    </FrameLayout>

    <!--  The Navigation Drawer-->
    <ListView
       ...
    </ListView>

</android.support.v4.widget.DrawerLayout>

How can I get the reference to the view elements of CreateTaskFragment? 如何获得对CreateTaskFragment的视图元素的引用?

Copy below method in your fragment and remove your onActivityCreated method from fragment 在片段中复制以下方法,并从片段中删除onActivityCreated方法

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_create_task, container, false);

    TextView endDateView = (TextView)rootView .findViewById(R.id.estimated_end_input);
    endDateView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("Date","I'm clicked");
        }
    });

 return rootView;

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM