问题是:所以我有一个 fragment ,其中有一个回收器 View ,带有自定义适配器,它存储一堆显示大学 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>
因此,因为这是在 fragment 内部的回收器 View 内,所以当我尝试在 onCreateView()
中为此按钮创建 setOnClickListener
时,当我打开回收器 View 所在的 fragment ,它就会崩溃。我认为这是因为在我尝试弄乱按钮的同时,回收器 View 填充在 fragment 内,并且当我收到 java.lang.NullPointerException: Attempt to invoke virtual method 时,它根本看不到它空对象引用时出现“void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)”
异常。这是 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();
}
}
所以一般问题是:如何编写能够定位位于回收器 View 的生成项内的按钮 setOnClickListener 的代码,并且回收器 View 本身位于 fragment 内部?
最佳答案
您在这里可以做的是在支架内的按钮上创建一个 onClickListener,该按钮将您的布局保留在顶部,该布局定义了 recycleview 内的单独 View ,然后在单击监听器上创建您想要在其中执行的任何过程。 在这里,我为您的代码添加了示例代码。
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//process here.
}
关于java - 定位 fragment 内回收器 View 项内的按钮监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59289560/