如何正确实现自定义适配器?

时间:2017-12-21 07:50:15

标签: android android-adapter

我正试图正确地实现适配器,并想知道ff:

  1. 将活动上下文传递给适配器是否可以?我在某个地方读到,传播活动背景并发现它令人困惑是不好的做法。
    1. 在获取父视图时,例如在SnackBar等中使用,通过这样的构造函数实例化它 $data 正确的方式或应该在 this.mRootView = ((Activity)mContext).getWindow().getDecorView().findViewById(R.id.activity_country_search);` 中实例化并使用onCreateViewHolder参数,如下所示:
    2. ```

      View parent
      1. 可以在适配器中实现snackbar,还是应该使用eventbus并通过活动显示?
      2. 当前的适配器实现:

        活动

        @Override
        public CountryIssuingViewHolder onCreateViewHolder(ViewGroup parent) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_country_search_issuing_row, parent, false);
        
            this.mRootView = parent.findViewById(R.id.activity_country_search);
            return new CountryIssuingViewHolder(v, true);
        }
        

        适配器

        adapter = new CountryIssuingListAdapter(this, countryIssuingList, new 
        CountryPhoneCodeAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(Country item, View v) {
                    Gson gson = new Gson();
                    Intent intent = new Intent();
                    intent.putExtra("COUNTRY", gson.toJson(item));
                    setResult(RESULT_OK, intent);
                    finish();
                }
            });
        

        非常感谢。

1 个答案:

答案 0 :(得分:0)

  1. 我在这里看不到任何不好的事。但在大多数情况下,您不需要在适配器中使用*
  2. 2-3。您是否使用**来展示事件(例如点击,滑动)?如果是这样,那么你应该将一个监听器传递给适配器并在活动中实现它,就像你为position/keyword argument所做的那样。这样,适配器的工作只是显示项目,通知关于用户的操作但不处理这些操作

    P.S。我会争论使用Context而不是听众,但这不是主题

相关问题