【问题标题】:Target a button listener that is inside a recycler view item that is inside a fragment定位在片段内的回收器视图项内的按钮侦听器
【发布时间】:2019-12-11 15:54:51
【问题描述】:

问题是:所以我有一个片段,其中有一个回收器视图,带有自定义适配器,它存储了一堆显示大学 ID 和名称的大学卡。他们的右侧还有一个按钮。这是这张卡片模板的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/gradientEnd"
    android:layout_margin="2dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="3"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/college_name"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text="EXAMPLE COLLEGE"
                android:textSize="18sp"
                android:paddingLeft="10dp"
                android:paddingTop="10dp"
                android:textColor="@color/colorPrimaryDark"
                android:fontFamily="sans-serif-medium"/>

            <TextView
                android:id="@+id/college_id"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text="ID: 1234567"
                android:textSize="16sp"
                android:paddingLeft="10dp"
                android:paddingBottom="5dp"
                android:textColor="@color/colorPrimaryDark"
                android:fontFamily="sans-serif-medium"/>

        </LinearLayout>

        <Button
            android:id="@+id/btn_college_details"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Details"
            android:textStyle="bold"
            android:textColor="@color/colorPrimaryDark"
            android:fontFamily="sans-serif-medium"
            android:background="@drawable/btn_bg"
            android:layout_marginBottom="10dp"
            android:layout_marginEnd="5dp"
            android:padding="5dp"
            android:layout_marginTop="8dp"/>

    </LinearLayout>
</LinearLayout>

因此,因为这是在片段内部的回收器视图中,所以当我尝试在onCreateView() 中为该按钮创建setOnClickListener 时,当我打开回收器视图所在的片段时,它就会崩溃。我认为这是因为回收器视图填充在片段中,同时我试图弄乱按钮,它根本看不到它,因为我得到一个java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference 异常。下面是 Fragments Java 代码:

public class CollegeFragment extends Fragment implements ItemClickListener{

    private RecyclerView recyclerView;
    private CustomRecyclerAdapter customRecyclerAdapter;
    private List<CollegeItem> collegeItemList = new ArrayList<>();
    private DatabaseCollege dbCollege;
    private EditText cName, cId;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View myFragmentView = inflater.inflate(R.layout.fragment_college, container, false);

        dbCollege = new DatabaseCollege(getActivity());

        recyclerView = myFragmentView.findViewById(R.id.recycler);
        customRecyclerAdapter = new CustomRecyclerAdapter(dbCollege.getCollegeData(), getActivity()); //populate recycler view with college cards

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(customRecyclerAdapter);
        customRecyclerAdapter.setItemClickListener(this);

        cId = myFragmentView.findViewById(R.id.et_ipeds);
        cId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                CollegeFragment.this.customRecyclerAdapter.getFilter().filter(s);
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        Button details = myFragmentView.findViewById(R.id.btn_college_details);
        details.setOnClickListener(new View.OnClickListener() { //This line causes the crash 
            @Override
            public void onClick(View v) {
                //do stuff
            }
        });


        return myFragmentView;

    }

    @Override
    public void onClick(View v, int position) {
        String name = collegeItemList.get(position).getName();
        String id = collegeItemList.get(position).getId();
    }
}

所以一般的问题是:如何编写能够定位按钮 setOnClickListener 的代码,该按钮位于回收器视图的生成项中,而回收器视图本身位于片段内部?

【问题讨论】:

    标签: java android button nullpointerexception


    【解决方案1】:

    你可以在这里做的是在持有人内的按钮​​上创建一个 onClickListener ,它将你的布局保存在顶部,它定义了你在 recycleview 内的单独视图,然后在点击监听器中创建你想要执行的任何过程。 在这里,我为您的代码添加了示例代码。

    holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    //process here.
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-26
      • 1970-01-01
      • 2014-01-05
      • 1970-01-01
      • 2013-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多