从Android版本4.4(kitkat)及以上Android版本的listview问题

时间:2015-03-10 10:13:42

标签: android listview baseadapter

我正在开发聊天应用程序。我正面临listview奇怪的行为问题。 我的代码在Android OS 4.4下完美运行。但是当我在4.4 kitkat中运行我的应用程序时,我面临着问题。

请查看以下屏幕截图。text get cut extra space.

假设我的第一个消息长度很大,那么我的第二个消息高度变得相同。

在第二个屏幕截图中,它将显示上一条消息的正确视图。因为我关闭了我的键盘和适配器同时刷新第一个可见视图高度自动增加。

public class ChatAdapter extends BaseAdapter {

    LayoutInflater mInflater;
    ChatActivity chatActivity;
    private DatabaseHandler DatabaseHandler;
    private DatabaseHandler dbHelper;
    private List<HBMessage> mList = new ArrayList<HBMessage>();
    private Context mContext;

    public ChatAdapter(ChatActivity chatActivity) {
        this.chatActivity = chatActivity;
        mContext = chatActivity;
        DatabaseHandler = new DatabaseHandler(mContext);
        dbHelper = new DatabaseHandler(mContext);
    }

    @Override
    public int getCount() {

        return mList.size();
    }

    @Override
    public HBMessage getItem(int position) {
        return mList.get(position);
    }

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

    private class Holder {

        private RelativeLayout mSimpleRecieveLayout, mSimpleSenderLayout, rlImageSendLayout, rlImageReceiveLayout, mRecieveImageLayout;
        private LinearLayout mSentImageLayout;
        private TextView simpleFriendMessage, simpleFriendMessageTime, simpleMyMessage, simpleMyMessageTime, mtxtImageSentTime,
                mtxtImageRecieveTime;
        private ImageView mimgSent, mimgRecieve, mplayordownload, mplayorupload;
        private TextView mMsgStatus, mtxtImageStatus;
        private ProgressBar mSentProgress, mDownloadProgress;
    }

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

        final Holder mHolder;
        if (convertView == null) {
            mHolder = new Holder();
            mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.single_chat_item, null);

            mHolder.mSimpleRecieveLayout = (RelativeLayout) convertView.findViewById(R.id.recive_layout);

            mHolder.mSimpleSenderLayout = (RelativeLayout) convertView.findViewById(R.id.sender_layout);
            mHolder.mSentImageLayout = (LinearLayout) convertView.findViewById(R.id.sentImageLayout);
            mHolder.mRecieveImageLayout = (RelativeLayout) convertView.findViewById(R.id.recieveImageLayout);

            mHolder.simpleFriendMessage = (TextView) convertView.findViewById(R.id.txtMesssageSender);
            mHolder.simpleFriendMessageTime = (TextView) convertView.findViewById(R.id.txtTimeSender);
            mHolder.mtxtImageRecieveTime = (TextView) convertView.findViewById(R.id.txtImageRecieveTime);

            mHolder.simpleMyMessage = (TextView) convertView.findViewById(R.id.txtMesssageRecieve);
            mHolder.simpleMyMessageTime = (TextView) convertView.findViewById(R.id.txtTimeRecieve);
            mHolder.mtxtImageSentTime = (TextView) convertView.findViewById(R.id.txtImageSentTime);

            mHolder.mMsgStatus = (TextView) convertView.findViewById(R.id.txtStatusSender);
            mHolder.mtxtImageStatus = (TextView) convertView.findViewById(R.id.txtImageSentStatus);

            mHolder.mimgSent = (ImageView) convertView.findViewById(R.id.imgSent);
            mHolder.mimgRecieve = (ImageView) convertView.findViewById(R.id.imgRecieve);
            mHolder.mSentProgress = (ProgressBar) convertView.findViewById(R.id.sentProgress);
            mHolder.mplayordownload = (ImageView) convertView.findViewById(R.id.playordownload);
            mHolder.mplayorupload = (ImageView) convertView.findViewById(R.id.playorupload);
            mHolder.mDownloadProgress = (ProgressBar) convertView.findViewById(R.id.downloadProgress);

            mHolder.rlImageSendLayout = (RelativeLayout) convertView.findViewById(R.id.chat_rlSendImage);
            mHolder.rlImageReceiveLayout = (RelativeLayout) convertView.findViewById(R.id.chat_rlReceiveImage);

            int tempWidth = (int) (BaseApplication.SCREEN_WIDTH * 0.28);
            int tempHeight = (int) (BaseApplication.SCREEN_WIDTH * 0.26);

            int imageSize = (int) (tempHeight * 0.84);

            LinearLayout.LayoutParams param1 = (LayoutParams) mHolder.rlImageSendLayout.getLayoutParams();
            param1.width = tempWidth;
            param1.height = tempHeight;
            mHolder.rlImageSendLayout.setLayoutParams(param1);

            RelativeLayout.LayoutParams param2 = (android.widget.RelativeLayout.LayoutParams) mHolder.rlImageReceiveLayout
                    .getLayoutParams();
            param2.width = tempWidth;
            param2.height = tempHeight;
            mHolder.rlImageReceiveLayout.setLayoutParams(param2);

            RelativeLayout.LayoutParams param3 = (android.widget.RelativeLayout.LayoutParams) mHolder.mimgSent.getLayoutParams();
            param3.width = imageSize;
            param3.height = imageSize;
            mHolder.mimgSent.setLayoutParams(param3);

            RelativeLayout.LayoutParams param4 = (android.widget.RelativeLayout.LayoutParams) mHolder.mimgRecieve.getLayoutParams();
            param4.width = imageSize;
            param4.height = imageSize;
            mHolder.mimgRecieve.setLayoutParams(param4);

            convertView.setTag(mHolder);
        } else {
            mHolder = (Holder) convertView.getTag();
        }
        if ((mList.get(position).getFromJID().equalsIgnoreCase(UserDetails.getInstance(mContext).getJabberId() + WSUtils.CHAT_DOMAIN))) {

            setMyLayout(mList.get(position), mHolder, position, convertView);

        } else {

            setOtherLayout(mList.get(position), mHolder, position);

        }

        return convertView;

    }

XML inflater布局

<RelativeLayout
    android:id="@+id/sender_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="end"
    android:clickable="false"
    android:visibility="visible" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:background="@drawable/chat_sender"
        android:orientation="vertical" >

        <com.hb.utils.CustomTextView
            android:id="@+id/txtMesssageSender"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginLeft="@dimen/chatpadding"
            android:layout_marginRight="@dimen/chatpadding"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/color_text"
            android:textSize="@dimen/chattext"
            app:type="common" />

        <com.hb.utils.CustomTextView
            android:id="@+id/txtTimeSender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_marginBottom="@dimen/chat_nagetive_pad"
            android:layout_marginRight="@dimen/chatpadding"
            android:layout_marginEnd="@dimen/chatpadding"
            android:layout_marginTop="2dp"
            android:textColor="@color/graychars"
            android:textSize="@dimen/chattime"
            app:type="common" />

        <com.hb.utils.CustomTextView
            android:id="@+id/txtStatusSender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_marginRight="@dimen/chatpadding"
            android:layout_marginEnd="@dimen/chatpadding"
            android:requiresFadingEdge="vertical"
            android:textColor="@color/color_regular_green"
            android:textSize="@dimen/chattime"
            app:type="common" />
    </LinearLayout>
</RelativeLayout>

<RelativeLayout
    android:id="@+id/recive_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/chat_receiver"
        android:orientation="vertical" >

        <com.hb.utils.CustomTextView
            android:id="@+id/txtMesssageRecieve"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginLeft="@dimen/chatpadding"
            android:layout_marginRight="@dimen/chatpadding"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/color_text"
            android:textSize="@dimen/chattext"
            app:type="common" />

        <com.hb.utils.CustomTextView
            android:id="@+id/txtTimeRecieve"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_marginRight="@dimen/chatpadding"
            android:layout_marginEnd="@dimen/chatpadding"
            android:layout_marginTop="2dp"
            android:requiresFadingEdge="vertical"
            android:textColor="@color/graychars"
            android:textSize="@dimen/chattime"
            app:type="common" />
    </LinearLayout>
</RelativeLayout>

<LinearLayout
    android:id="@+id/sentImageLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/chat_rlSendImage"
        android:layout_width="210px"
        android:layout_height="210px"
        android:layout_gravity="end"
        android:background="@drawable/chat_sender"
        android:gravity="end|center_vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="@dimen/chatpadding" >

            <ImageView
                android:id="@+id/imgSent"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:contentDescription="@string/app_name"
                android:scaleType="centerCrop"
                android:src="@drawable/noimage" />

            <ProgressBar
                android:id="@+id/sentProgress"
                style="?android:attr/progressBarStyleInverse"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:visibility="visible" />

            <ImageView
                android:id="@+id/playorupload"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:src="@drawable/upload"
                android:visibility="gone" />
        </RelativeLayout>
    </RelativeLayout>

    <com.hb.utils.CustomTextView
        android:id="@+id/txtImageSentTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_marginBottom="@dimen/chat_nagetive_pad"
        android:layout_marginRight="@dimen/chatimagetext"
        android:layout_marginEnd="@dimen/chatimagetext"
        android:layout_marginTop="2dp"
        android:textColor="@color/graychars"
        android:textSize="@dimen/chattime"
        app:type="common" />

    <com.hb.utils.CustomTextView
        android:id="@+id/txtImageSentStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_marginRight="@dimen/chatimagetext"
        android:layout_marginEnd="@dimen/chatimagetext"
        android:textColor="@color/color_regular_green"
        android:textSize="@dimen/chattime"
        app:type="common" />
</LinearLayout>

<RelativeLayout
    android:id="@+id/recieveImageLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:id="@+id/chat_rlReceiveImage"
        android:layout_width="210px"
        android:layout_height="210px"
        android:background="@drawable/chat_receiver"
        android:gravity="center_vertical" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="@dimen/chatpadding" >

            <ImageView
                android:id="@+id/imgRecieve"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/app_name"
                android:scaleType="centerCrop"
                android:src="@drawable/noimage" />

            <ProgressBar
                android:id="@+id/downloadProgress"
                style="?android:attr/progressBarStyleInverse"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:visibility="gone" />

            <ImageView
                android:id="@+id/playordownload"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:src="@drawable/download" />
        </RelativeLayout>
    </RelativeLayout>

    <com.hb.utils.CustomTextView
        android:id="@+id/txtImageRecieveTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/chat_rlReceiveImage"
        android:layout_alignEnd="@id/chat_rlReceiveImage"
        android:layout_below="@id/chat_rlReceiveImage"
        android:textColor="@color/graychars"
        android:textSize="@dimen/chattime"
        app:type="common" />
</RelativeLayout>

private synchronized void setOtherLayout(HBMessage hbMessage, final Holder mHolder, final int position) {
        if (hbMessage.getMsgType().equalsIgnoreCase(HBMessage.IMAGE)) {
            mHolder.mSimpleRecieveLayout.setVisibility(View.GONE);
            mHolder.mSimpleSenderLayout.setVisibility(View.GONE);
            mHolder.mSentImageLayout.setVisibility(View.GONE);
            mHolder.mRecieveImageLayout.setVisibility(View.VISIBLE);

            mHolder.mplayordownload.setTag(position);
            mHolder.mimgRecieve.setTag(position);

            String str = hbMessage.getMessage();
            final String seperatedString[] = str.split("##@!@##");

            if (hbMessage.getMediaStatus().equals(HBMessage.NOTDOWNLOADED)) {
                mHolder.mplayordownload.setVisibility(View.VISIBLE);
                mHolder.mDownloadProgress.setVisibility(View.GONE);
            } else if (hbMessage.getMediaStatus().equals(HBMessage.DOWNLOADING)) {
                mHolder.mplayordownload.setVisibility(View.GONE);
                mHolder.mDownloadProgress.setVisibility(View.VISIBLE);
            } else {

                mHolder.mplayordownload.setVisibility(View.GONE);
                mHolder.mDownloadProgress.setVisibility(View.GONE);
            }

            mHolder.mtxtImageRecieveTime.setText(StaticUtils.getChatHistoryDateTime(hbMessage.getMessageTime()));
            chatActivity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mHolder.mimgRecieve.setImageBitmap(getImageFromBase64StringWithTimeStamp(seperatedString[0]));
                }
            });

            mHolder.mplayordownload.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View paramView) {
                    paramView.setVisibility(View.GONE);
                    int pos = (Integer) paramView.getTag();
                    HBMessage message = getItem(pos);
                    final String name[] = message.getMessage().split("##@!@##");
                    if (dbHelper.isImageAvaialble(name[1])) {

                        DatabaseHandler.updateImageStatus(mContext, message.getMessageId(), HBMessage.DOWNLOADED, HBMessage.CHAT_SINGLE);
                        message.setMediaStatus(HBMessage.DOWNLOADED);
                        setItem(message, pos);
                    } else {
                        ((ViewGroup) paramView.getParent()).findViewById(R.id.downloadProgress).setVisibility(View.VISIBLE);
                        message.setMediaStatus(HBMessage.DOWNLOADING);
                        setItem(message, position);
                        chatActivity.callDownloadfileProcess(pos, ((ViewGroup) paramView.getParent()));
                    }
                }
            });

        } else {

            mHolder.mSimpleRecieveLayout.setVisibility(View.VISIBLE);
            mHolder.mSimpleSenderLayout.setVisibility(View.GONE);
            mHolder.mSentImageLayout.setVisibility(View.GONE);
            mHolder.mRecieveImageLayout.setVisibility(View.GONE);

            mHolder.simpleMyMessageTime.setText(StaticUtils.getChatHistoryDateTime(hbMessage.getMessageTime()));
            mHolder.simpleMyMessage.setText(hbMessage.getMessage());
        }
    }

    private synchronized void setMyLayout(HBMessage hbMessage, final Holder mHolder, int position, View view) {

        if (hbMessage.getMsgType().equalsIgnoreCase(HBMessage.IMAGE)) {

            mHolder.mSimpleRecieveLayout.setVisibility(View.GONE);
            mHolder.mSimpleSenderLayout.setVisibility(View.GONE);
            mHolder.mRecieveImageLayout.setVisibility(View.GONE);
            mHolder.mSentImageLayout.setVisibility(View.VISIBLE);

            String str = hbMessage.getMessage();
            final String seperatedString[] = str.split("##@!@##");

            mHolder.mimgSent.setTag(position);
            mHolder.mplayorupload.setTag(position);

            if (hbMessage.getMediaStatus().equals(HBMessage.NOTUPLOADED)) {
                mHolder.mplayorupload.setVisibility(View.VISIBLE);
                mHolder.mSentProgress.setVisibility(View.GONE);
            } else if (hbMessage.getMediaStatus().equals(HBMessage.UPLOADING)) {
                mHolder.mplayorupload.setVisibility(View.GONE);
                mHolder.mSentProgress.setVisibility(View.VISIBLE);
            } else {
                mHolder.mplayorupload.setVisibility(View.GONE);
                mHolder.mSentProgress.setVisibility(View.GONE);
            }
            mHolder.mSentProgress.setTag(view);
            mHolder.mtxtImageSentTime.setText(StaticUtils.getChatHistoryDateTime(hbMessage.getMessageTime()));

            chatActivity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    mHolder.mimgSent.setImageBitmap(getImageFromBase64StringWithTimeStamp(seperatedString[0]));
                }
            });

            mHolder.mtxtImageStatus.setText(hbMessage.getMessageStatus());
            if (hbMessage.getMessageStatus().equals(HBMessage.STATUS_PENDDING)
                    || hbMessage.getMessageStatus().equals(HBMessage.STATUS_FAILED)) {
                mHolder.mtxtImageStatus.setTextColor(mContext.getResources().getColor(R.color.color_pending_red));
            } else if (hbMessage.getMessageStatus().equals(HBMessage.STATUS_DELIVERED)) {
                mHolder.mtxtImageStatus.setTextColor(mContext.getResources().getColor(R.color.color_black));
            } else
                mHolder.mtxtImageStatus.setTextColor(mContext.getResources().getColor(R.color.color_green));

            if (hbMessage.isAutoUpload()) {
                ImageHolder mImageHolder = dbHelper.getImageHolder(seperatedString[1]);
                File mFile = StaticUtils.bitmapToFile(mImageHolder.getBitmap(), mImageHolder.getFileName(), false);
                if (mFile != null) {
                    hbMessage.setAutoUpload(false);
                    mList.set(position, hbMessage);
                    chatActivity.uploadToserver(mFile, mImageHolder.getFileName(), hbMessage, position, view);
                } else {
                    CommonUtils.showSingleBtnDialog("Image Not Available", mContext, null);
                }
            }

            mHolder.mplayorupload.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View paramView) {
                    int pos = (Integer) paramView.getTag();

                    HBMessage message = getItem(pos);
                    final String seperatedString[] = message.getMessage().split("##@!@##");
                    ImageHolder mImageHolder = dbHelper.getImageHolder(seperatedString[1]);
                    File mFile = StaticUtils.bitmapToFile(mImageHolder.getBitmap(), mImageHolder.getFileName(), false);
                    if (mFile != null) {
                        paramView.setVisibility(View.GONE);
                        ProgressBar pb = (ProgressBar) ((ViewGroup) paramView.getParent()).findViewById(R.id.sentProgress);
                        pb.setVisibility(View.VISIBLE);
                        View view = (View) pb.getTag();
                        ((TextView) view.findViewById(R.id.txtImageSentStatus)).setText(HBMessage.STATUS_PENDDING);

                        message.setMediaStatus(HBMessage.UPLOADING);
                        message.setMessageStatus(HBMessage.STATUS_PENDDING);
                        setItem(message, pos);

                        chatActivity.uploadToserver(mFile, mFile.getName(), message, pos, view);
                        DatabaseHandler.updateImageStatus(mContext, message.getMessageId(), HBMessage.UPLOADING, HBMessage.CHAT_SINGLE);
                    } else {
                        CommonUtils.showSingleBtnDialog("Image Not Available", mContext, null);
                    }

                }
            });

        } else {
            mHolder.mSimpleRecieveLayout.setVisibility(View.GONE);
            mHolder.mSimpleSenderLayout.setVisibility(View.VISIBLE);
            mHolder.mSentImageLayout.setVisibility(View.GONE);
            mHolder.mRecieveImageLayout.setVisibility(View.GONE);

            mHolder.simpleFriendMessageTime.setText(StaticUtils.getChatHistoryDateTime(hbMessage.getMessageTime()));
            mHolder.simpleFriendMessage.setText(hbMessage.getMessage());

            mHolder.mMsgStatus.setText(hbMessage.getMessageStatus());
            if (hbMessage.getMessageStatus().equals(HBMessage.STATUS_PENDDING)
                    || hbMessage.getMessageStatus().equals(HBMessage.STATUS_FAILED)) {
                mHolder.mMsgStatus.setTextColor(mContext.getResources().getColor(R.color.color_pending_red));
            } else if (hbMessage.getMessageStatus().equals(HBMessage.STATUS_DELIVERED)) {
                mHolder.mMsgStatus.setTextColor(mContext.getResources().getColor(R.color.color_black));
            } else
                mHolder.mMsgStatus.setTextColor(mContext.getResources().getColor(R.color.color_green));
        }
    }

3 个答案:

答案 0 :(得分:0)

由于视图回收的行为,无论convertView是否为null,都必须设置和取消设置与内容有关的所有内容。您应该首先将与viewHolder绑定无关的代码移动到之后 if-else。 另一个选择是开始使用RecyclerView,它实际上像ListView,它具有改进的回收机制(以及更多)。这是guide

答案 1 :(得分:0)

您需要为所有视图执行一些代码,无论它们是新的还是已回收的。仅对新视图执行viewholder init部分。

if (convertView == null) {
    mHolder = new Holder();
    mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = mInflater.inflate(R.layout.single_chat_item, null);

    mHolder.mSimpleRecieveLayout = (RelativeLayout) convertView.findViewById(R.id.recive_layout);

    mHolder.mSimpleSenderLayout = (RelativeLayout) convertView.findViewById(R.id.sender_layout);
    mHolder.mSentImageLayout = (LinearLayout) convertView.findViewById(R.id.sentImageLayout);
    mHolder.mRecieveImageLayout = (RelativeLayout) convertView.findViewById(R.id.recieveImageLayout);

    mHolder.simpleFriendMessage = (TextView) convertView.findViewById(R.id.txtMesssageSender);
    mHolder.simpleFriendMessageTime = (TextView) convertView.findViewById(R.id.txtTimeSender);
    mHolder.mtxtImageRecieveTime = (TextView) convertView.findViewById(R.id.txtImageRecieveTime);

    mHolder.simpleMyMessage = (TextView) convertView.findViewById(R.id.txtMesssageRecieve);
    mHolder.simpleMyMessageTime = (TextView) convertView.findViewById(R.id.txtTimeRecieve);
    mHolder.mtxtImageSentTime = (TextView) convertView.findViewById(R.id.txtImageSentTime);

    mHolder.mMsgStatus = (TextView) convertView.findViewById(R.id.txtStatusSender);
    mHolder.mtxtImageStatus = (TextView) convertView.findViewById(R.id.txtImageSentStatus);

    mHolder.mimgSent = (ImageView) convertView.findViewById(R.id.imgSent);
    mHolder.mimgRecieve = (ImageView) convertView.findViewById(R.id.imgRecieve);
    mHolder.mSentProgress = (ProgressBar) convertView.findViewById(R.id.sentProgress);
    mHolder.mplayordownload = (ImageView) convertView.findViewById(R.id.playordownload);
    mHolder.mplayorupload = (ImageView) convertView.findViewById(R.id.playorupload);
    mHolder.mDownloadProgress = (ProgressBar) convertView.findViewById(R.id.downloadProgress);

    mHolder.rlImageSendLayout = (RelativeLayout) convertView.findViewById(R.id.chat_rlSendImage);
    mHolder.rlImageReceiveLayout = (RelativeLayout) convertView.findViewById(R.id.chat_rlReceiveImage);
    convertView.setTag(mHolder);
} else {
    mHolder = (Holder) convertView.getTag();
} 
    // This is now executed for all your views.
    int tempWidth = (int) (BaseApplication.SCREEN_WIDTH * 0.28);
    int tempHeight = (int) (BaseApplication.SCREEN_WIDTH * 0.26);

    int imageSize = (int) (tempHeight * 0.84);

    LinearLayout.LayoutParams param1 = (LayoutParams) mHolder.rlImageSendLayout.getLayoutParams();
    param1.width = tempWidth;
    param1.height = tempHeight;
    mHolder.rlImageSendLayout.setLayoutParams(param1);

    RelativeLayout.LayoutParams param2 = (android.widget.RelativeLayout.LayoutParams) mHolder.rlImageReceiveLayout
            .getLayoutParams();
    param2.width = tempWidth;
    param2.height = tempHeight;
    mHolder.rlImageReceiveLayout.setLayoutParams(param2);

    RelativeLayout.LayoutParams param3 = (android.widget.RelativeLayout.LayoutParams) mHolder.mimgSent.getLayoutParams();
    param3.width = imageSize;
    param3.height = imageSize;
    mHolder.mimgSent.setLayoutParams(param3);

    RelativeLayout.LayoutParams param4 = (android.widget.RelativeLayout.LayoutParams) mHolder.mimgRecieve.getLayoutParams();
    param4.width = imageSize;
    param4.height = imageSize;
    mHolder.mimgRecieve.setLayoutParams(param4);

此外,您应该考虑阅读有关在适配器中使用不同视图类型的更多信息(在您的情况下为myLayout和otherLayout)。起点是this

答案 2 :(得分:0)

Finally I have solved this issue by applying stupid thing.Why i am facing issue,that i really don't know and Why its get solved after applying patch that i really don't know.

**My Solution:**

    setMyLayout()
{
    mHolder.mSentMessageLayout
                        .setBackgroundResource(R.drawable.chat_sender);
}    
    setOtherLayout()
{
    mHolder.mRecieveMessageLayout
                        .setBackgroundResource(R.drawable.chat_receiver);
}