我在RecyclerView适配器中定义了一个方法,并希望从MainActivity中调用它,但我不能。请指导我。
我的主要活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetDataAdapter1 = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView1);
recyclerViewlayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(recyclerViewlayoutManager);
JSON_DATA_WEB_CALL();
recyclerViewadapter = new RecyclerViewAdapter(MainActivity.this, GetDataAdapter1, this);
recyclerView.setAdapter(recyclerViewadapter);
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy);
LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
int firstVisible = manager.findFirstVisibleItemPosition();
int lastVisible = manager.findLastVisibleItemPosition();
Log.i("TAG", "onScrolled: " + firstVisible);
Log.i("TAG", "Last Index: " + lastVisible);
recyclerViewadapter.changeItem(firstVisible,lastVisible);
}
});
我的适配器
@Override
public int getItemCount() {
return getDataAdapter.size();
}
public void changeItem(int position){
firstVisible = position;
notifyItemChanged(firstVisible);
notifyDataSetChanged();
}
class ViewHolder extends RecyclerView.ViewHolder{
public TextView IdTextView;
public TextView NameTextView;
public TextView PhoneNumberTextView;
public TextView SubjectTextView;
我想从MainActivity调用此changeItem
答案 0 :(得分:0)
您使用一个参数声明了该函数:
public void changeItem(int position)
但你用2个参数调用它:
recyclerViewadapter.changeItem(firstVisible,lastVisible);
我也很确定在这些方面:
firstVisible = position;
notifyItemChanged(firstVisible);
firstVisible
不在范围内。您应该了解参数和范围的工作原理。