线性布局无法转换为RecyclerView onClickItemListener

时间:2016-01-19 01:40:50

标签: android android-recyclerview onclicklistener

我正在尝试实现一个onClickListener,以允许用户单击他们的朋友的RecyclerView列表。但是,单击用户列表项后,它只返回错误:

01-18 20:39:37.317 3413-3413/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android.feastappv2, PID: 3413
    java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams
        at android.support.v7.widget.RecyclerView.getChildViewHolderInt(RecyclerView.java:3411)
        at android.support.v7.widget.RecyclerView.getChildLayoutPosition(RecyclerView.java:3445)
        at com.example.android.feastappv2.feastapp.Friends.FriendFragment$ContactViewHolder.onClick(FriendFragment.java:73)
        at android.view.View.performClick(View.java:5254)
        at android.view.View$PerformClick.run(View.java:21173)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:6840)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

问题在于:

@Override
public void onClick(View view) {
    int itemPosition = mRecyclerView.getChildLayoutPosition(view);
    Toast.makeText(getContext(),
            "Position is: " + String.valueOf(itemPosition),
            Toast.LENGTH_LONG).show();
}
我班上的

public class FriendFragment extends Fragment {

    RecyclerView mRecyclerView;
    ArrayList<Friend> mContacts;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.recycler_layout, container, false);

        mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view_layout);
        mRecyclerView.setHasFixedSize(true);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        mContacts = FriendProvider.getInstance(getActivity()).getFriends();
        mRecyclerView.setAdapter(new ContactAdapter(mContacts));

        return view;
    }

    // Inner Class

    private class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private final TextView mName;
        private final TextView mNumber;
        private Friend mContact;

        public ContactViewHolder(View itemView) {
            super(itemView);

            mName = (TextView) itemView.findViewById(R.id.tv_name);
            mNumber = (TextView) itemView.findViewById(R.id.tv_number);

            mName.setOnClickListener(this);
        }

        public void bindContact(Friend contact){
            mContact = contact;
            mName.setText(contact.getFirstName().toString() + " " + contact.getLastName());
            mNumber.setText(contact.getFriendChannelID().toString());
        }

        @Override
        public void onClick(View view) {
            int itemPosition = mRecyclerView.getChildLayoutPosition(view);
            Toast.makeText(getContext(), "Position is: " + String.valueOf(itemPosition), Toast.LENGTH_LONG).show();
        }

    }

    class ContactAdapter extends RecyclerView.Adapter<ContactViewHolder> {
        ArrayList<Friend> contactList;

        ContactAdapter(ArrayList<Friend> object){
            this.contactList = object;
        }

        @Override
        public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.rv_item, parent, false);

            return new ContactViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ContactViewHolder holder, int position) {
            Friend contact = contactList.get(position);
            holder.bindContact(contact);
        }

        @Override
        public int getItemCount() {
            return contactList.size();
        }
    }
}

2 个答案:

答案 0 :(得分:3)

如果要获取单击项目的位置,可以使用

获取适配器的位置
int itemPosition = getAdapterPosition();
在你的OnClick方法中

答案 1 :(得分:2)

对于包含clicable child的复杂视图,请尝试

View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override public void onClick(View v) {
      View view = v;
      View parent = (View) v.getParent();
      while (!(parent instanceof RecyclerView)){
        view=parent;
        parent = (View) parent.getParent();
      }
      int position = recyclerView.getChildAdapterPosition(view);
}

here

所述