我已经为列表视图创建了一个自定义适配器,并且我想显示一行的徽章。我没有得到任何类似的错误,但是徽章也没有出现。当我查看logcat时,出现一条消息“ BadgeView:需要ParentView”。但是我已经提供了一个。

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.jauker.widget.BadgeView;
import java.util.ArrayList;
import java.util.List;

public class AdminViewAdapter extends BaseAdapter {
List<String> result = new ArrayList<String>();
List<Integer> resnum = new ArrayList<Integer>();
public static Context context;
private static LayoutInflater inflater=null;
public AdminViewAdapter(Context mainActivity, List<String> ADMINFUNCTIONS, List<Integer> NUMBERS) {
    // TODO Auto-generated constructor stub
    result=ADMINFUNCTIONS;
    resnum = NUMBERS;
    context=mainActivity;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}
@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public static class Holder
{
    TextView os_text;
    BadgeView badgeView;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View v = convertView;
    Holder holder =new Holder();
    if(v == null) {
        v = inflater.inflate(R.layout.list_functions, null);
        holder.os_text = (TextView) v.findViewById(R.id.receptionistfunctions);
        if (result.toArray()[position].toString().contains("ACCOUNT")) {
            holder.os_text.setTextAlignment(v.TEXT_ALIGNMENT_TEXT_START);
            holder.os_text.setText(result.toArray()[position].toString());
            holder.badgeView = new BadgeView(context);
            holder.badgeView.setTargetView(holder.os_text);
            holder.badgeView.setBadgeGravity(Gravity.BOTTOM);
            holder.badgeView.setBadgeMargin(0, 0, 0, 0);
            holder.badgeView.setBadgeCount(22);
        }
        else if (result.toArray()[position].toString().contains("LEAVE")) {
           /* holder.os_text.setTextAlignment(v.TEXT_ALIGNMENT_TEXT_START);
            holder.os_text.setText(result.toArray()[position].toString());
            BadgeView badge = new BadgeView(context);
            badge.setTag(target);
            badge.setText(resnum.toArray()[1].toString());
            badge.show();*/
        }
        else {
            holder.os_text.setText(result.toArray()[position].toString());
        }
        v.setTag(holder);
    }
    else {
        holder = (AdminViewAdapter.Holder)convertView.getTag();
    }

    return v;
}}


没有其他错误。我尝试为徽章使用另一个View变量。

最佳答案

您没有将BadgeView添加到任何视图组,因此您的BadgeView没有父级。这就是为什么您看到该消息的原因。视图应该有一个父项!

我认为您可以通过使用ViewGroup#addView()方法(例如BadgeView)以编程方式将((ViewGroup) v).addView(holder.badgeView)添加到适配器布局中来解决此问题。甚至更好的是,修改适配器布局以在其中包含BadgeView

编辑:

您可以像这样修改getView()方法:


@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View v = convertView;
    Holder holder =new Holder();
    if(v == null) {
        v = inflater.inflate(R.layout.list_functions, null);
        // ...
        holder.badgeView = new BadgeView(context);
        holder.badgeView.setTargetView(holder.os_text);
        holder.badgeView.setBadgeGravity(Gravity.BOTTOM);
        holder.badgeView.setBadgeMargin(0, 0, 0, 0);
        holder.badgeView.setBadgeCount(22);
        // Add this line
        ((ViewGroup) v).addView(holder.badgeView);
    }
    // ...
}}

关于android - BadgeView:需要ParentView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46516334/

10-09 00:08