其实我会在这里问一个以上的问题。请不要禁止我,请阅读完整的故事。让我们开始。所以我需要创建一个活动或片段(无关紧要)与内部(顶部和底部)的部分(视图)。在底部部分内部动态加载按钮(有时是2,有时是30),它们上面有一个点击监听器。当用户单击按钮时,按钮将显示在顶部(视图)上,并在底部视图中消失。顶视图上的按钮也有点击监听器,如果用户点击按钮,它会显示在底部视图上并消失在顶部。所以这是一项任务。我想过如何实现它。我创建的最简单的解决方案是:两个视图是带有两个适配器的回收器视图。嗯,可能它不是最好的解决方案,我很确定。我可以实现两个适配器,但我无法实现第二个适配器的单击侦听器。它不起作用!?我不喜欢这种方式有两个原因:1。两个适配器是相同的; 2.我不能使用点击适配器作为第二个适配器。您可以在下面找到我的代码。 我的适配器 - 标准适配器:
public class KeyboardAdapter extends RecyclerView.Adapter<KeyboardAdapter.KeyboardAdapterViewHolder> {
private List<String> values;
/*
* An on-click handler that we've defined to make it easy for an Activity to interface with
* our RecyclerView
*/
private final KeyboardAdapterOnClickHandler mClickHandler;
/**
* The interface that receives onClick messages.
*/
public interface KeyboardAdapterOnClickHandler {
void onClick(int position, String nameClicked);
}
/**
* Creates a SourceAdapter.
*
* @param clickHandler The on-click handler for this adapter. This single handler is called
* when an item is clicked.
*/
public KeyboardAdapter(List<String> myDataset, KeyboardAdapterOnClickHandler clickHandler) {
values = myDataset;
mClickHandler = clickHandler;
}
/**
* Cache of the children views for a forecast list item.
*/
public class KeyboardAdapterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// each data item is just a string in this case
private Button btnValue;
private String mName;
public View layout;
private int parentId;
private KeyboardAdapterViewHolder(View view) {
super(view);
//layout = view;
btnValue = view.findViewById(R.id.btn);
//parentId = ((View) btnValue.getParent()).getId();
// Call setOnClickListener on the view passed into the constructor (use 'this' as the OnClickListener)
view.setOnClickListener(this);
}
public void setData(String name) {
mName = name;
btnValue.setText(mName);
}
@Override
public void onClick(View view) {
int adapterPosition = getAdapterPosition();
mClickHandler.onClick(adapterPosition, mName);
}
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@Override
@NonNull
public KeyboardAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
return new KeyboardAdapterViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull KeyboardAdapterViewHolder viewHolder, final int position) {
viewHolder.setData(values.get(position));
}
@Override
public int getItemCount() {
return values.size();
}
@Override
public int getItemViewType(int position) {
return 0;
}
public void remove(int position) {
values.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, values.size());
}
}
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
...
String s = "test it";
mAdapter = new KeyboardAdapter(virtualKeyboardInit(s), MainActivity.this);
recyclerView1.setAdapter(mAdapter);
// empty list just to init rv
answerList = new ArrayList<>();
mAdapter1 = new KeyboardAdapter1(answerList, MainActivity.this); // doesn't work, error message "KeyboardAdapter1.KeyboardAdapterOnClickHandler cannot be applied to MainActivity"
recyclerView2.setAdapter(mAdapter1);
}
private List<String> virtualKeyboardInit(String s) {
boolean checkBool = true;
// convert string to array and then to list
String [] strArray = s.split("(?!^)");
stringList = new ArrayList<>(Arrays.asList(strArray));
// shuffle letters in the list
long seed = System.nanoTime();
Collections.shuffle(stringList, new Random(seed));
// API 24
// /String[] strArray = Stream.of(cArray).toArray(String[]::new);
return stringList;
}
@Override
public void onClick(int position, String nameClicked) {
mAdapter.remove(position);
}
这些只是代码片段。那么,在这种情况下我该怎么办?感谢您的关注和帮助。