Android listview customAdapter更改行重力&颜色

时间:2015-03-06 08:29:24

标签: android listview layout-inflater custom-adapter

我的ListView有以下CustomAdapter:

public class CustomConvAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Conv> convItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public CustomConvAdapter(Activity activity, List<Conv> convItems) {
    this.activity = activity;
    this.convItems = convItems;
}

@Override
public int getCount() {
    return convItems.size();
}

@Override
public Object getItem(int location) {
    return convItems.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Conv m = convItems.get(position);

    if ((convertView == null)) {
        convertView = inflater.inflate(R.layout.conv_row, null);
    }

    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();
    NetworkImageView thumbNail = (NetworkImageView) convertView
            .findViewById(R.id.thumbnail);
    TextView name = (TextView) convertView.findViewById(R.id.name);
    TextView body = (TextView) convertView.findViewById(R.id.body);


    // thumbnail image
    //thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

    name.setText(c.getName());

    body.setText(c.getBody());

    return convertView;
}

我会改变我的R.layout.conv_row属性(重力,背景,textcolor),如bubblechat:如果消息是我的(name == String myname),那么右重力和bgcolor =#color1,否则重力左和bgcolor =#color2。 看到很多关于这个的例子,但我会使用我的CustomAdapter来实现它。 有可能吗?

任何帮助将不胜感激。提前谢谢。

修改:将我的代码编辑为:

LinearLayout.LayoutParams params;

params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

params.gravity = Gravity.RIGHT; convertView.setLayoutParams(params);

现在我

  

com.xxxxx.yyyy E / AndroidRuntime:FATAL EXCEPTION:main       java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为   android.widget.AbsListView $的LayoutParams               在android.widget.ListView.measureScrapChild(ListView.java:1179)               在android.widget.ListView.measureHeightOfChildren(ListView.java:1261)               在android.widget.ListView.onMeasure(ListView.java:1171)               在android.view.View.measure(View.java:16831)               在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5245)

等,等

我该如何解决?

2 个答案:

答案 0 :(得分:0)

您可以设置parentView的设置重力。假设parentView包含文本和正文。然后你可以做这样的事情。

if (isMe) {
            holder.parentView.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
        } else {
            holder.parentView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        }

答案 1 :(得分:0)

@ПетърПетров就是这样:

conv_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="vertical"
android:id="@+id/LinearLayout"
android:background="@drawable/list_row_selector">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text=""
        android:id="@+id/name" />

    <TextView
        android:id="@+id/body"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="@dimen/rating"
        android:maxLines="5"
        android:layout_below="@+id/title"
        android:layout_alignParentStart="false"
        android:textColor="#ffaaaaaa" />

</LinearLayout>

谢谢。