我已经在RecyclerView上创建了一个用于实现监听器的额外类,但它引用了行的整个视图,我还希望在该行的一个View中有一个监听器,在本例中是按钮。
我知道我可以从CustomViewHolder访问该视图,但问题是我无法将某些值从适配器传递到适配器调用的活动。
我如何从addOnItemTouchListener
获得此视图?这是代码:
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), list, new ClickListener() {
@Override
public void onClick(View view, int position) {
// Is it possible to check which view is clicked here
// Something like this
if (view == findViewById(R.id.btn)) {
// do something
// but this is not working.
}
}
@Override
public void onLongClick(View view, int position) {
}
}));
或任何其他建议我可以实现此目的,请告诉我。
答案 0 :(得分:0)
你可以用这种方式创建你的Callback监听器
class ViewController: NSViewController {
// Input and output text fields
@IBOutlet weak var inputStr: NSTextField!
@IBOutlet weak var outputStr: NSTextField!
// Input and output conversion selection
@IBOutlet weak var inputSegments: NSSegmentedControl!
@IBOutlet weak var outputSegments: NSSegmentedControl!
@IBAction func outputIsSelected(sender: NSSegmentedControl) {
// Passing all UI elements in (not shown)
// Not ideal, want to be global obj (see below code)
guard ErrorCheck().stringIsNotEmpty(inputStr.stringValue) else {
ErrorResponse().invalidEmptyInput()
return
}
// 0 = DNA, 1 = mRNA, 2 = English
if (inputSegments.selectedSegment == 0) {
checkPossibleConversionAndConvertDNA()
} else if (inputSegments.selectedSegment == 1) {
checkPossibleConversionAndConvertmRNA()
} else if (inputSegments.selectedSegment == 2) {
checkPossibleConversionAndConvertEnglish()
}
}
}
让您的活动实现它并将实例传递给您的适配器和适配器public interface OnRecyclerRowListener {
void onClick(int position, View v);
void onLongClick(int position, View v);
}
onBindViewHolder