android - 列表适配器中的回调方法不起作用

标签 android callback onclick android-recyclerview

我使用了上一个问题中指导使用的回调方法。这似乎不起作用。未调用 onClick() 方法。回调方法似乎是一个非常广泛的概念。我不知道如何缩小搜索范围以获得相关信息,也不知道如何找出我得到的代码有什么问题。

ListActivity - 适配器已初始化并在此处设置点击监听器

public class ListActivity extends AppCompatActivity implements ListAdapter.ItemClickListener {

    ArrayList<Country> countriesList;
    ListAdapter adapter;
    RecyclerView rv;

    protected void onCreate(@Nullable Bundle savedInstanceState) {

        adapter = new ListAdapter(context, this);
        adapter.setClickListener(this);

        //more irrelevant code
    }
}

    private Country getCurrentCountry(){
        return currentCountry;
    }

    public void setCurrentCountry(Country currentCountry){
        this.currentCountry = currentCountry;
    }

    @Override
    public void onItemClick(View view, int position) {

        FragmentTransaction ft;

        Bundle countryBundle = new Bundle();
        countryBundle.putParcelable("countriesList", getCurrentCountry());

        Fragment countryFragment = new CountryFragment();
        countryFragment.setArguments(countryBundle);
        ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.fragments_container, countryFragment);

        Log.d("Monitoring", getCurrentCountry().toString());

        ft.commit();
    }

Adapter类中的回调方法

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.RowViewHolder> {
// Adapter class code
...
    class RowViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView viewName;
        ImageView viewFlag;
        LinearLayout countryEntry;

        RowViewHolder(View view) {

            super(view);
            viewName = view.findViewById(R.id.name);
            viewFlag = view.findViewById(R.id.flag);
            countryEntry = view.findViewById(R.id.rowId);
            view.setOnClickListener(this);
        }

        public void onClick(View view) {
            if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
            ListActivity.position = getAdapterPosition();
            final Country currentCountry = countriesList.get(getAdapterPosition());
            listActivity.setCurrentCountry(currentCountry);

        }
    }

    public void setClickListener(ItemClickListener itemClickListener) {
        this.mClickListener = itemClickListener;
    }

    public interface ItemClickListener {
        void onItemClick(View view, int position);
    }
//more irrelevant code
}

谢谢!

最佳答案

不需要传递 View 和位置,只需传递你想要的对象即可。

更改接口(interface)参数

 public interface ItemClickListener {
     void onItemClick(Country country);
 }

你可以传递你需要的对象

public void onClick(View view) {

     final Country currentCountry = countriesList.get(getAdapterPosition());

     mClickListener.onItemClick(currentCountry);
}

在您的 Activity 中,获取您的国家/地区

 @Override
 public void onItemClick(Country country) {

      //  get country and do anything you want
 }

希望这有帮助

关于android - 列表适配器中的回调方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58894651/

相关文章:

android - Android 中的平滑擦洗视频

android - 应该使用android :showAsAction when not using the appcompat library

java - 注销 OnDataPointListener 时 task.getResult() 返回 false

c++ - 将返回 void 但具有整数参数的函数作为 ARGUMENT 本身传递给另一个函数

javascript - js 回调给出了未定义的 var 以及如何定义回调

javascript - 未捕获的 TypeError : 1 argument required, 但只有 0 存在...但是我的函数没有参数?

java - 如何从 Java 中的字符串中获取日期和时间?

将方法作为回调传递的 javascript 问题

android - 由 performClick() 单击的按钮或单击显示 android

JavaScript按钮onclick不起作用