简体   繁体   中英

java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams

I using android RecycleView to replace ListLiew.

What I wanna do is to enabled onClick on one of the ImageView in my List Item (imaging this List item got a image and a text, I want to add a onClick listener to the image but not the list item itself)

I create one RecycleView Adapter with following layout xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:layout_height="48dp"
    android:background="@drawable/border_bottom">

<ImageView
    android:id="@+id/icon"        
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/ic_action_play_dark"
    android:layout_centerVertical="true" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginLeft="56dp"
    android:text="Search"
    android:textColor="#333"
    android:textStyle="bold"
    android:layout_centerVertical="true"  
    android:gravity="center_vertical"/>

</RelativeLayout>

and my onCreateViewHolder

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
{
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_list_item, parent, false);

    ImageView icon = (ImageView)v.findViewById(R.id.icon);
    icon.setOnClickListener(listener);

    PropertyViewHolder vh = new PropertyViewHolder(v);

    return vh;
}

I want to bind the listener to the ImageView icon (child of the View), but not the View itself, however, it always throw exception :

12-23 17:37:36.033: E/AndroidRuntime(5806): java.lang.ClassCastException:
android.widget.RelativeLayout$LayoutParams cannot be cast to
android.support.v7.widget.RecyclerView$LayoutParams
at android.support.v7.widget.RecyclerView.getChildPosition(RecyclerView.java:2589)
.....

when I click on the image, my onclickListner here

@Override
public void onClick(View v) {

    int itemPosition = getRecycleView().getChildPosition(v); 

    Toast.makeText(getActivity(), "click ", Toast.LENGTH_SHORT).show();
}

any idea ?

From the comments, you may already know that it's not working because your ImageView is not the view which resides in the RecyclerView .

In this case, I would suggest you to implement listener in the ViewHolder, and then pass ViewHolder reference when icon is clicked rather than it's View

I will provide an example below,


Sample RecyclerView

Notice how I've assigned a listener in the constructor of both MyRecyclerViewAdapter and MyViewHolder which comes from either an Activity or Fragment **. That Activity or Fragment will implement MyViewHolderListener interface which will provide onIconClicked method.

Now notice that, I am providing MyViewHolder.this reference in the onIconClicked method. Using that ViewHolder object, you can get adapter position and then actual Data object eventually. I have provided implementation of onIconClicked method, too.

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private List<Object> mData;

private MyViewHolder.MyViewHolderListener mViewHolderListener;

public MyRecyclerViewAdapter(MyViewHolder.MyViewHolderListener listener) {
    mData = new ArrayList<>();
    mViewHolderListener = listener;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
    return new MyViewHolder(view, mViewHolderListener);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
    ((MyViewHolder) viewHolder).update(mData.get(position));
}

public void setData(@NonNull List<Object> newData) {
    mData.addAll(newData);
    notifyDataSetChanged();
}

public Object getItemAtPosition(int position) {
    try {
        return mData.get(position);
    } catch (IndexOutOfBoundsException e) {
        e.printStackTrace();
        return null;
    }
}

@Override
public int getItemCount() {
    return mData == null ? 0 : mData.size();
}

public void reset() {
    mData.clear();
    notifyDataSetChanged();
}

public static final class MyViewHolder extends RecyclerView.ViewHolder {

    ImageView icon;

    public MyViewHolder(View itemView, final MyViewHolderListener listener) {
        super(itemView);
        icon = (ImageView) itemView.findViewById(R.id.icon);
        icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onIconClicked(MyViewHolder.this);
            }
        });
    }

    public void update(Object myObject) {
        // update icon
    }

    public interface MyViewHolderListener {
        void onIconClicked(MyViewHolder clickedViewHolder);
    }
}

}


Implementation of OnIconClicked inside an Activity or a Fragment

@Override
public void onIconClicked(MyRecyclerViewAdapter.MyViewHolder clickedViewHolder) {
    int adapterPosition = clickedViewHolder.getAdapterPosition();
    if(adapterPosition != RecyclerView.NO_POSITION) {
        Object selectedItem = mAdapter.getItemAtPosition(adapterPosition);
    }
}

I hope this solves your query!

I ran into this problem when applying a ClickListener from the Fragment into the Adapter. I found that the cause of the issue was that I was applying a listener on a nested view within the parent view.

Here's an example of an application that I'm working, eg.

<LinearLayout>
    <LinearLayout>
        <ImageView forClickingOn />
    </LinearLayout>
</LinearLayout>

This is the onClick implementation

private View.OnClickListener onCompleteClick =
        new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                int iposition = mRecyclerView.getChildLayoutPosition(view);
                TodoItem item = mAdapter.getItem(iposition);

                item.setCompleted(!item.isCompleted());

                if (item.isCompleted()) {
                    item.setPinned(false);
                }

                replaceAdapterItem(iposition, item);
            }
        };

I found the solution was the traverse the tree back to the parent element, cast it as a View and find the position using the parent View.

View rootView = (View) view.getParent() // Frame Layout
        .getParent() // Linear Layout
        .getParent() // Linear Layout
        .getParent(); // Linear Layout

int iposition = mRecyclerView.getChildLayoutPosition(rootView);

I imagine that there's better way of finding the parent element, but this is what solved it for me.

在此输入图像描述

when call

mRecycleView.getChildLayoutPosition(view) or mRecycleView.getChildAdapterPosition(view) ,

the parameter "view" should be RecycleView ItemRootView , it's mean "view" is RecycleView direct ChildView.

so your code

ImageView icon = (ImageView)v.findViewById(R.id.icon);
icon.setOnClickListener(listener);

.

@Override
public void onClick(View v) {
     //v is the ImageView inside item_layout.
     int itemPosition = getRecycleView().getChildPosition(v); 
     Toast.makeText(getActivity(), "click ", Toast.LENGTH_SHORT).show();
     }

试试这个

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_list_item, (ViewGroup) null, false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM