处理按钮单击recyclelerView中的行内部

时间:2016-07-22 13:09:49

标签: android database onclick android-recyclerview adapter

我想知道如果更改适配器,如何处理行的位置 - 例如,如果我向SearchView写入内容 - 适配器已更改且位置不同。我已经完成了这个(下面),但它在更改位置之前显示了该项目。

我的问题是点击Recycler视图项目。

你可以帮帮我吗?

适配器

public class ContactsRecyclerAdapter extends RecyclerView.Adapter<ContactsRecyclerAdapter.BindingHolder> {

    private static OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(View itemView, int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    private List<Contact> mContacts;
    private List<Contact> mContactsFiltered;
    SearchViewFilter filter;
    Context mContext;

    public ContactsRecyclerAdapter(Context context, List<Contact> contact) {
        this.mContext = context;
        this.mContacts = contact;
        this.mContactsFiltered = contact;
        filter = new SearchViewFilter(mContacts, this);
    }

    public static class BindingHolder extends RecyclerView.ViewHolder {
        private ViewDataBinding binding;

        public BindingHolder(final View rowView) {
            super(rowView);
            binding = DataBindingUtil.bind(rowView);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null)
                        listener.onItemClick(itemView, getLayoutPosition());
                }
            });
        }
        public ViewDataBinding getBinding() {
            return binding;
        }
    }

    @Override
    public BindingHolder onCreateViewHolder(ViewGroup parent, int type) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item_contact, parent, false);
        BindingHolder holder = new BindingHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(BindingHolder holder, int position) {
        final Contact contact = mContactsFiltered.get(position);
        holder.getBinding().setVariable(BR.contact, contact);
        holder.getBinding().executePendingBindings();
    }

片段

   public class ContactListFragment extends Fragment implements View.OnClickListener {

        FloatingActionButton fabButton;
        SearchView searchView;
        RecyclerView recyclerView;
        String name, number, email;

        private List<Contact> mContact = new ArrayList<>();
        private ContactsRecyclerAdapter mAdapter;

        public static ContactListFragment newInstance() {
            Bundle args = new Bundle();
            ContactListFragment fragment = new ContactListFragment();
            fragment.setArguments(args);
            return fragment;
        }

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setHasOptionsMenu(true);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_contact_list, container, false);

            searchView = (SearchView) view.findViewById(R.id.search_view);

            recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
            recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            mContact = SugarRecord.listAll(Contact.class);

            mAdapter = new ContactsRecyclerAdapter(getActivity(), mContact);
            recyclerView.setAdapter(mAdapter);

            //item click
            mAdapter.setOnItemClickListener(new ContactsRecyclerAdapter.OnItemClickListener() {

                @Override
                public void onItemClick(View itemView, int position) {
                    Contact contact = SugarRecord.findById(Contact.class, (long) position + 1);

                    name = contact.getName();
                    number = contact.getNumber();
                    email = contact.getEmail();
                    showAlertDialog();
                }
            });

            searchFilter();

            return view;
        }

public void sortList() {
        if(mContact.size() > 0) {
            if (!isSort) {
                Collections.sort(mContact, new Comparator<Contact>() {
                    @Override
                    public int compare(Contact contact1, Contact contact2) {
                        return contact1.getName().compareToIgnoreCase(contact2.getName());
                    }
                });
            }
            else {
                Collections.reverse(mContact);
                isSort = false;
            }
        }
        mAdapter.notifyDataSetChanged();

2 个答案:

答案 0 :(得分:1)

Click listener无法与RecyclerView一起使用。使用OnItemTouchListener()

RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
      @Override public void onItemClick(View view, int position) {
        // do whatever
      }

      @Override public void onLongItemClick(View view, int position) {
        // do whatever
      }
   })
);

答案 1 :(得分:0)

你必须在BindingHolder类中忽略onclick。在onclick方法内部调用通过构造函数等传递的对象的回调方法

public static class BindingHolder extends RecyclerView.ViewHolder {
    private ViewDataBinding binding;

    public BindingHolder(final View rowView) {
        super(rowView);
        binding = DataBindingUtil.bind(rowView);

        public void onClick(View view) {
        //call a callback method here
        }
    }
    public ViewDataBinding getBinding() {
        return binding;
    }
}