没有在Android上的getView()中获得最后一个位置

时间:2015-01-14 20:34:34

标签: android debugging android-listview android-adapter

我有一个带适配器的ListView。我的问题是我没有收到getView()函数的最后一个位置,而是两次收到第二个最后一个位置。可能是什么问题?

在下面找到适配器的相关部分。

static class ItemViewHolder {
    private RelativeLayout messageRow;
    private LinearLayout messageContainer;
    private TextView messageText;
    private TextView messageSentTime;
    private TextView PictureReportTextView;
    private ImageView messageState;
    private ImageView messagePicture;
    private WebView messageGifView;
    private ProgressBar messageProgressbar;
    private int position;
}

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

    View rowView = convertView;
    final ItemViewHolder subViews;
    if (rowView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.message_list_item, parent, false);
        subViews = new ItemViewHolder();
        subViews.messageRow = (RelativeLayout) rowView.findViewById(R.id.message_row);
        subViews.messageContainer = (LinearLayout) rowView.findViewById(R.id.message_container);
        subViews.messageText = (TextView) rowView.findViewById(R.id.message_text);
        subViews.messageSentTime = (TextView) rowView.findViewById(R.id.message_sent_time);
        subViews.messagePicture = (ImageView) rowView.findViewById(R.id.message_picture);
        subViews.messageProgressbar = (ProgressBar) rowView.findViewById(R.id.message_picture_progress_bar);
        subViews.PictureReportTextView = (TextView) rowView.findViewById(R.id.message_picture_report);
        subViews.messageState = (ImageView) rowView.findViewById(R.id.message_state);
        rowView.setTag(subViews);
    } else {
        subViews = (ItemViewHolder) rowView.getTag();
    }

    final MessageModel messageObj = getValues().get(position);

    subViews.position = position;
    subViews.messagePicture.setVisibility(View.GONE);
    subViews.messageProgressbar.setVisibility(View.GONE);
    subViews.PictureReportTextView.setVisibility(View.GONE);
    subViews.messagePicture.imageURl = "";

    if (messageObj.getMessage().equals("")) {
        subViews.messageText.setVisibility(View.GONE);
        subViews.messageText.setText("");
    } else {
        subViews.messageText.setVisibility(View.VISIBLE);
        subViews.messageText.setText(messageObj.getMessage());
    }
    Log.d(TAG, "for position: " + position + " - " + messageObj.getMessage());
    Log.d(TAG, "for position: " + position + " - " + messageObj.getMessageId());

    subViews.messageSentTime.setText(DateTimeUtils.stringForMessageListDate(messageObj.getSentTime()));

    if (messageObj.getMessagePicture().equals("") || messageObj.getMessagePicture().equals("null")) {
        subViews.messagePicture.setVisibility(View.GONE);
        subViews.PictureReportTextView.setVisibility(View.GONE);
        subViews.messageContainer.setMinimumWidth(0);
        subViews.messageContainer.setMinimumHeight(0);
        if (subViews.messageGifView != null) {
            subViews.messageGifView.setVisibility(View.GONE);
            subViews.messageGifView.loadUrl("about:blank");
        }
    } else {
        subViews.PictureReportTextView.setVisibility(View.VISIBLE);
        subViews.messagePicture.setVisibility(View.VISIBLE);
        loadMessagePicture(messageObj, subViews, false);
    }

    if (messageObj.getFlagged() == 1) {
        subViews.messageText.setText(reportString);
        subViews.messageText.setVisibility(View.VISIBLE);
        subViews.PictureReportTextView.setVisibility(View.GONE);
        if (subViews.messageGifView != null) {
            subViews.messageGifView.setVisibility(View.GONE);
        }
        subViews.messagePicture.setVisibility(View.GONE);
        subViews.messageSentTime.setVisibility(View.GONE);
    } else {
        subViews.messageSentTime.setVisibility(View.VISIBLE);
    }

    subViews.messageRow.setMinimumWidth(0);

    return rowView;
}

和message_list_item.xml文件:

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

<RelativeLayout
    android:id="@+id/message_row"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="true"
    >

    <LinearLayout
        android:id="@+id/message_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginBottom="5dp">
        <ProgressBar
            android:id="@+id/message_picture_progress_bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone"
            />
        <ImageView
            android:id="@+id/message_picture"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxWidth="200dp"
            android:maxHeight="300dp"
            android:visibility="gone"
            />
        <TextView
            android:id="@+id/message_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/message_picture"
            android:layout_weight="1"
            android:textSize="17sp"
            android:textIsSelectable="true"
            android:textColor="@color/black_lighter" />
    </LinearLayout>

    <TextView
        android:id="@+id/message_sent_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_gravity="bottom"
        android:layout_below="@+id/message_container"
        android:layout_alignStart="@+id/message_container"
        android:layout_alignLeft="@+id/message_container"
        android:textSize="10sp"
        android:singleLine="true"
        android:maxLines="1"
        android:gravity="bottom"
        android:textColor="@color/grey"
        android:textStyle="normal" />

    <ImageView
        android:id="@+id/message_state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/message_container"
        android:layout_toRightOf="@+id/message_sent_time"
        android:visibility="gone"
        />

    <TextView
        android:id="@+id/message_picture_report"
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10sp"
        android:layout_alignTop="@+id/message_sent_time"
        android:layout_alignRight="@+id/message_container"
        android:text="Report"/>

</RelativeLayout>

日志: 我的列表有49个对象。 我参加活动了。请注意,它从位置48开始。

for position: 48 - hi
for position: 48 - 43802939
for position: 48 - hi
for position: 48 - 43802939
for position: 47 - hey
for position: 47 - 43791969
for position: 46 - hey
for position: 46 - 43791349
for position: 45 - haha
for position: 45 - 43782981
for position: 44 - ha
for position: 44 - 43782875
for position: 43 - hello
for position: 43 - 43782725
for position: 42 - hello
for position: 42 - 43782132
for position: 41 - name
for position: 41 - 43781951

在滚动上方并立即再次下降时,我会收到49号位置。

for position: 40 - lol
for position: 40 - 43756370
for position: 40 - lol
for position: 40 - 43756370
for position: 49 - hehehe
for position: 49 - 43803671

0 个答案:

没有答案