【问题标题】:Callback method in list Adapter doesn't work列表适配器中的回调方法不起作用
【发布时间】:2019-11-17 16:19:09
【问题描述】:

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

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

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
}

谢谢!

【问题讨论】:

  • 添加活动内部的实际onItemClick 方法
  • 几分钟前添加。谢谢。
  • 请检查我的答案 [这里] stackoverflow.com/questions/53696176/…
  • ViewHolder 中的 mClickListener 始终为 null,因此您应该将侦听器从 Adapter 设置为 ViewHolder
  • @huang 将监听器(mClickListener?)设置为 ViewHolder 是什么意思?

标签: android callback onclick android-recyclerview


【解决方案1】:

你不需要传递视图和位置,只需传递你想要的对象。

改变你的接口参数

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

你可以传递你需要的对象

public void onClick(View view) {

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

     mClickListener.onItemClick(currentCountry);
}

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

 @Override
 public void onItemClick(Country country) {

      //  get country and do anything you want
 }

希望对你有帮助

【讨论】:

  • 我收到错误:错误:ListActivity 不是抽象的,并且不会覆盖 ItemClickListener 中的抽象方法 onItemClick(Country)。
  • @D.Joe 你必须重写方法public void onItemClick(Country country) { }
  • 谢谢@huang。它终于奏效了!现在我需要解决一个布局问题:由于某种原因,ListActivity 仍然出现在新 Fragment 的顶部。
【解决方案2】:

这是因为您在 RowViewHolder 的 onClick 方法中缺少 @Override 注释。

它应该如下所示。

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);
        }

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

        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多