簡體   English   中英

如何從擴展Fragment而不是Activity的類中正確檢索聲明為片段布局的ImageView?

[英]How can I correctly retrieve an ImageView declared into a fragment layout from a class that extends Fragment and not Activity?

我在嘗試將布局元素檢索到類中時遇到以下問題。

所以我有以下情況:

1)我有這個fragment_screen_slide_page.xml文件,其中包含顯示在視圖中的片段元素:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Dummy content. -->
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="0dp">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:scaleType="fitCenter"
            android:layout_height="250dp"
            android:background="@drawable/carbonara" />

        <TextView android:id="@android:id/text1"
            style="?android:textAppearanceLarge"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp" />

    </LinearLayout>

</ScrollView>

如您所見,它包含具有id = imageView1ImageView元素。

然后,我有這個ScreenSlidePageFragment可以在前一個片段上工作,我有這樣的東西:

public class ScreenSlidePageFragment extends Fragment {

    ................................................................
    ................................................................
    ................................................................

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ................................................................
        ................................................................
        ................................................................
        // Retrieve the reference of the ImageView element of the layout having id=imgSlide:
        ImageView imgSlideView = (ImageView) this.getActivity().findViewById(R.id.imageView1);
        ................................................................
        ................................................................
        ................................................................
    }

    ................................................................
    ................................................................
    ................................................................
}

如您所見,我正在嘗試通過以下指令在onCreate()方法中檢索id = imageView1的元素的ImageView引用,該元素已定義到fragment_screen_slide_page.xml文件中:

ImageView imgSlideView = (ImageView) this.getActivity().findViewById(R.id.imageView1);

我完成了this.getActivity ()。findViewById(R.id.imageView1),因為ScreenSlidePageFragment擴展了Fragment而不是Activity,因此它沒有繼承findViewById()方法,因此我無法直接查找findViewById(R.id.imageView1) ; 正如我在擴展Activity的類中所做的那樣。

問題在於以這種方式我得到的結果為null

為什么? 我想念什么? 我怎樣才能正確地找回我的ImageView宣布到從ScreenSlidePageFragmentfragment_screen_slide_page.xml文件(擴展片段 ,而不是活動 )?

嘗試:將代碼從oncreate移到重寫的onCreateView

public  View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
        View view = inflater.inflate(R.layout.fragment_screen_slide_page,container,false);
        ImageView imgSlideView = (ImageView) view.findViewById(R.id.imageView1);
        return view;    
}

onViewCreated調用它

    @Override
    public void onViewCreated(View view, @Nullable Bundle  savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ImageView imgSlideView = (ImageView) view.findViewById(R.id.imageView1);
    }

除了onCreate() ,您可以重寫onViewCreated()並從此處獲取ImageView

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    ImageView imgSlideView = (ImageView) view.findViewById(R.id.imageView1);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM