qstumn/BadgeView

targetView must have a parent

kscMaster opened this issue · 2 comments

在fragment里使用,外部嵌套了recyclerview,具体代码如下:
adapter = new HomeAdapter((AppCompatActivity) mActivity, getAllMune()); rv_list.setAdapter(adapter);
其中getAllMune是获取菜单对象列表,
public HomeAdapter(AppCompatActivity mActivity, List<BeanHomeItem> allMune) { this.context = mActivity; mInflater = LayoutInflater.from(context); itemBean = allMune; }
这段代码是构造方法
下面这段是内部Holder
`class Holder extends RecyclerView.ViewHolder
{
TextView tv_title;
Badge bv_badge;

    public Holder(View itemView)
    {
        super(itemView);
        tv_title = (TextView) itemView.findViewById(R.id.tv_title);
        bv_badge = new QBadgeView(context).bindTarget(itemView.findViewById(R.id.root));
        bv_badge.setBadgeGravity(Gravity.CENTER | Gravity.END);
        bv_badge.setBadgeTextSize(14, true);
        bv_badge.setBadgePadding(6, true);
        bv_badge.setOnDragStateChangedListener((dragState, badge, targetView) ->
        {
            if (dragState == Badge.OnDragStateChangedListener.STATE_SUCCEED)
            {
                Toast.makeText(context, String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
            }
        });
    }
}`

item的xml:
`

<TextView
    android:id="@+id/tv_title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:textSize="16sp"/>

`
运行时报错
java.lang.IllegalStateException: targetView must have a parent
at q.rorbin.badgeview.QBadgeView.bindTarget(QBadgeView.java:166),求指教

哦,找到答案了,item父类必须加frameLayout,正确的item应该如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:textSize="16sp"/>
</RelativeLayout>

`

希望补充一下

我是自定义的view,addView(TargetView)之后,获取不到parentView,抛出异常,这个能解决吗?