簡體   English   中英

如果片段在另一個布局中膨脹,如何獲取對該片段的視圖引用?

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

我有一個用於放大各種片段的父布局。 在一種這樣的片段布局上,我需要訪問片段類內部的view元素。 我正在使用以下代碼來獲取片段布局內文本視圖的引用。

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");
            }
        });
    }

endDateView返回null

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

<?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>

如何獲得對CreateTaskFragment的視圖元素的引用?

在片段中復制以下方法,並從片段中刪除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