1

我有一个自定义数组适配器,我想在 ListView 中有一个图像视图。这只发生在 2.2 和 2.3.X 设备上。

我的行模板如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/place_category_icon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="8dp"
        android:background="@drawable/photo1"
        android:scaleType="centerCrop" >
    </ImageView>

    <TextView
        android:id="@+id/place_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/list_timestamp"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="Harsha Mallikarjun Vantagudi"
        android:textStyle="bold" >
    </TextView>

    <TextView
        android:id="@+id/place_distance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:padding="6dp"
        android:paddingLeft="30dp"
        android:singleLine="true"
        android:text="2 hours" >
    </TextView>

</LinearLayout>

当我加载列表时。图像已显示,但当我尝试滚动文本时文本消失,文本随机重新出现并消失。

在此处输入图像描述

在此处输入图像描述

适配器

public class PlacesListAdapter extends ArrayAdapter<Place> implements
        Filterable {
    public Context context;
    private List<Place> places;

    public PlacesListAdapter(Context context, List<Place> places) {
        super(context, R.layout.list_item_place, places);
        this.context = context;
        this.places = places;

    }

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

        View view = convertView;

        if (view == null) {
            LayoutInflater inflater;
            inflater = LayoutInflater.from(getContext());
            view = inflater.inflate(R.layout.list_item_place, null);
        }

        Place place = places.get(position);

        if (place != null) {

            TextView place_title = (TextView) view
                    .findViewById(R.id.place_name);
            TextView place_distance = (TextView) view
                    .findViewById(R.id.place_distance);

            if (place_title != null) {
                place_title.setText(place.getPlaceName());
            }

            if (place_distance != null) {
                place_distance.setText(place.getPlaceDistance());
            }
        }

        return view;
    }

}

在 2.X 设备上测试 APK

4

2 回答 2

1

我错了,我用的是充气机:)

这段代码肯定可以在 android 2.3 中运行,并且有图像和文本并且是可滚动的。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_row, parent, false);

        ImageView imgThumb = (ImageView) rowView.findViewById(R.id.thumbnail);
        TextView tvDate = (TextView) rowView.findViewById(R.id.tvDate);
        TextView tvHeadline = (TextView) rowView.findViewById(R.id.tvHeadline);
        // Button btDetail = (Button)rowView.findViewById(R.id.btDetail);

        NewsHeadline newsHeadline = values.get(position);
        if (newsHeadline != null) {
            imgThumb.setImageBitmap(newsHeadline.getThumbnail());
            DateFormat df = SimpleDateFormat.getDateInstance();
            tvDate.setText(df.format(newsHeadline.getDate()));
            tvHeadline.setText(newsHeadline.getTitle());
        }
        return rowView;
    }

调用/设置代码:

protected void onPostExecute(String result) {
        Log.d("ReloadTask", "Done");

        newsListActivity.getListView().setAdapter(new               NewsDataArrayAdapter(newsListActivity, MyApplication.getListNewsHeadline()));

和布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="86dp"
    android:background="#363C4A"
    android:orientation="horizontal"
  >

    <!-- ListRow Left sied Thumbnail image -->

    <ImageView
        android:id="@+id/thumbnail"
        android:layout_width="80dip"
        android:layout_height="67dip"
        android:layout_alignLeft="@+id/lineBottom"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dip" />

    <!-- Date -->

    <TextView
        android:id="@+id/tvDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="12.07.2012"
        android:textColor="#747474"
        android:textSize="11dip"
        android:textStyle="normal"
        android:typeface="sans" />

    <!-- headline -->

    <TextView
        android:id="@+id/tvHeadline"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tvDate"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="1dip"
        android:layout_toLeftOf="@+id/tvDetail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Just gona stand there and ... is multilined?"
        android:textColor="#d4d4d4"
        android:textSize="14dip"
        android:textStyle="bold" />

    <!-- Rightend Arrow -->

    <TextView
        android:id="@+id/tvDetail"
        android:layout_width="13dp"
        android:layout_height="19dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/arrow" />

    <!-- Bottom Arrow -->

    <ImageView
        android:id="@+id/lineBottom"
        android:layout_width="288dip"
        android:layout_height="3dip"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/line" />

</RelativeLayout>
于 2012-12-24T19:38:11.400 回答
1

好吧,我知道我的响应可能有点晚了,但是我通过将其添加到 TextView 来修复它。

android:background="@android:color/transparent"
于 2014-06-18T14:30:53.690 回答