Android添加徽章列表

时间:2014-09-02 17:47:26

标签: android

我有一个列表,我想为每个项目添加徽章。我正在使用此库:https://github.com/jgilfelt/android-viewbadger

这是我的代码:

static class ViewHolder {
    TextView nameView;
    ImageView imageView;
    BadgeView badge;
}
public void placeRandomUsers(final String search) {
    randomAdapter = new ArrayAdapter<JsonObject>(this, 0) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.random_bars, null);
                holder = new ViewHolder();
                holder.nameView=(TextView)convertView.findViewById(R.id.tweet);
                holder.badge = new BadgeView(getContext(), holder.nameView);
                holder.badge.setTextColor(Color.BLACK);
                holder.badge.setBadgeBackgroundColor(Color.parseColor("#A4C639"));
                holder.imageView=(ImageView)convertView.findViewById(R.id.image);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if (position >= getCount() - 3 && search.equals("") == true) {
                //loadRandomUsers("");
            }
            JsonObject user=getItem(position);
            String name=user.get("name").getAsString();
            String image_url="http://domain/photos/profile/thumb/"+user.get("photo").getAsString();

            holder.nameView.setText(name);


            Ion.with(holder.imageView)
            .placeholder(R.drawable.twitter)
            .load(image_url);
            holder.badge.setText("1");
            holder.badge.show();
            return convertView;
        }
    };

    ListView listView = (ListView)findViewById(R.id.list);
    listView.setAdapter(randomAdapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            //Log.w("Pos","asd"+randomAdapter.getItem(position).get("name").getAsString());
            startChat(randomAdapter.getItem(position));

        }
    });
    loadRandomUsers(search);
}

这是我的random_bars.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:padding="4dp"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

    <ImageView
            android:id="@+id/image"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:src="@drawable/twitter"/>

    <LinearLayout
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:layout_weight="1"
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="wrap_content">

        <TextView
                style="@android:style/TextAppearance.Medium"
                android:id="@+id/handle"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>

        <TextView
                android:id="@+id/tweet"
                style="@android:style/TextAppearance.Small"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

当我为每件商品添加徽章时,nameView未显示。这是输出:

enter image description here

如果我删除徽章代码,则输出为:

enter image description here

在这里你可以看到.Image正在显示,但是当我使用徽章类时,名字没有显示。这可能是什么原因?

1 个答案:

答案 0 :(得分:0)

我将random_bars.xml代码更改为此

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:padding="4dp"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">

    <ImageView
            android:id="@+id/image"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:src="@drawable/twitter"/>

    <LinearLayout
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:layout_weight="1"
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="wrap_content">

        <TextView
                style="@android:style/TextAppearance.Medium"
                android:id="@+id/handle"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>

        <TextView
                android:id="@+id/tweet"
                style="@android:style/TextAppearance.Small"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

问题已解决。