首页 > 解决方案 > 如何在 Activity(基本 java 文件)中访问 Recycler View 的布局?

问题描述

这是我的布局(fragment_hadees.xml):

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".Hadees">


    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:id="@+id/myrecyclerview"
        android:layout_height="match_parent"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fab"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:elevation="40dp"
        android:layout_margin="30dp"
       android:backgroundTint="#ffffff"
        android:src="@drawable/favorited"/>


</RelativeLayout>

这是 Hadees.java(根 java 文件):

   @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View root = inflater.inflate(R.layout.fragment_hadees, container, false);
   ((MainActivity) getActivity()).getSupportActionBar().setTitle("Hadees");

   HadeesAdapter hadeesAdapter;
   fab= root.findViewById(R.id.fab);
    ImageView copybutton= root.findViewById(R.id.copybutton);
    final TextView textView=root.findViewById(R.id.textView);
    ImageView whatsapText=root.findViewById(R.id.whatsApp_txt);


    toggleModelList= new ArrayList<>();

    fab.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {
          Toast.makeText(getActivity(), "FAB", Toast.LENGTH_SHORT).show();


       }
    });


    myrecyclerview=(RecyclerView)root.findViewById(R.id.myrecyclerview);
    myrecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));

这是我的 hadees_contents.xml:

 <LinearLayout
    android:id="@+id/l1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_margin="10dp"
        android:textIsSelectable="true"
        android:textColor="#000000"
        android:gravity="center"
        android:maxLines="100"
        android:fontFamily="sans-serif-condensed-medium"
        android:text="This is Arabic Text"
        android:textSize="20sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="2dp"
        android:layout_marginBottom="2dp"
        android:background="@android:color/darker_gray" />

    <LinearLayout
        android:id="@+id/l2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="6dp"
        android:layout_marginBottom="6dp"
        android:weightSum="4">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/copybutton"
            android:src="@drawable/ic_content_copy_black_24dp" />

如何访问 Hadees.java 中的 hadees_contents.xml 中的视图?我可以访问 FAB 但不能访问 Recycler 的 View inflated layout 的内容,即 hadees_contents.xml

标签: android

解决方案


您正在尝试从 Hadees.java 访问 hadees_contents 的字段,这不会使 xml hadees_contents 膨胀。它膨胀了 xml fragment_hadees,这就是为什么如果您尝试从 Hadees.java 访问 hadees_contents 会出现空指针异常的原因。我可以想办法解决这个问题,但我不知道它在你的情况下是否实用。您必须有一个将 hadees_contents 的字段添加到 recyclerview 的活动,您可以从 addActivity 发送一个将 hadees_contents 膨胀到您的 Hadees.java 类的意图,并在您从 Hadees.java 调用 OnActivityResult 方法时接收它。


推荐阅读