控制基本适配器以在列表视图中单击时更改图像视图

时间:2016-05-20 12:11:55

标签: android listview android-adapter

我正在使用基本适配器来填充listview中的数据。我需要在适配器类中单击更改图标。问题是我无法控制适配器类中的imageview位置。例如,如果我在音乐文件列表中使用播放/暂停图标,我需要将图标从播放更改为暂停该特定文件。由于基于适配器中位置的imageview控件没有暂停/播放随机文件。任何帮助都将受到赞赏。

修改

Picture of layout used

我添加了所用布局的图片。 BaseAdapter中的布局有2种布局,其中一种用于显示发件人消息,第二种用于显示收到的消息。我们隐藏了一个基于消息的布局。这里的问题是我们没有对发送器/接收器布局进行正确的控制。我希望现在很清楚。

编辑5/21:

请找到适配器类和布局

适配器

package com.app.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.R;
import com.app.handler.DatabaseHandler;
import com.app.materialdesign.CircularImageView;
import com.app.materialdesign.DiscreteSeekBar;
import com.app.model.VoiceMessages;
import com.app.util.PrefManager;
import com.app.util.Utilities;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

public class ChatAdapter extends BaseAdapter implements MediaPlayer.OnCompletionListener {


private Context context = null;
private LayoutInflater inflater = null;
private String location = null;
List<VoiceMessages> voiceMessageList = null;
private DatabaseHandler sqliteHelper = null;
int possitionOfFile = -1;
MediaPlayer mediaPlayer = null;
private Utilities utils = null;
private Handler durationHandler = new Handler();
private PrefManager pref = null;
private double timeElapsed = 0, finalTime = 0;
FileInputStream fis;
private ViewHolderItem viewHolder = null;

public ChatAdapter(Context context, List<VoiceMessages> voiceMessageList) {
    this.context = context;
    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.voiceMessageList = voiceMessageList;
}

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

@Override
public Object getItem(int position) {
    return position;
}

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

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

    View view = convertView;
    if (view == null) {
        view = inflater.inflate(R.layout.chat_adapter, null);
        viewHolder = new ViewHolderItem();
        view.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolderItem) view.getTag();
    }

    viewHolder.textSeparator = (TextView) view.findViewById(R.id.textSeparator);
    viewHolder.txtReceiveName = (TextView) view.findViewById(R.id.txtReceiveName);
    viewHolder.txtSendTime = (TextView) view.findViewById(R.id.txtSendTime);
    viewHolder.seekBarSender = (DiscreteSeekBar) view.findViewById(R.id.seekBarSender);
    viewHolder.imgPlaySender = (ImageView) view.findViewById(R.id.imgPlaySender);
    viewHolder.txtSenderName = (TextView) view.findViewById(R.id.txtSenderName);
    viewHolder.txtReceiveTime = (TextView) view.findViewById(R.id.txtReceiveTime);
    viewHolder.txtAudioRemaingreceiverTime = (TextView) view.findViewById(R.id.txtAudioRemaingreceiverTime);
    viewHolder.txtAudioRemaingSenderTime = (TextView) view.findViewById(R.id.txtAudioRemaingSenderTime);
    viewHolder.dateLayout = (RelativeLayout) view.findViewById(R.id.dateLayout);
    viewHolder.senderLayout = (LinearLayout) view.findViewById(R.id.senderLayout);
    viewHolder.imgPlayReceiver = (ImageView) view.findViewById(R.id.imgPlayReceiver);
    viewHolder.ProgressLayout = (RelativeLayout) view.findViewById(R.id.ProgressLayout);
    viewHolder.receiverMainLayout = (LinearLayout) view.findViewById(R.id.receiverMainLayout);
    viewHolder.senderMainLayout = (LinearLayout) view.findViewById(R.id.senderMainLayout);
    viewHolder.ProgressLayoutReceiver = (RelativeLayout) view.findViewById(R.id.ProgressLayoutReceiver);
    viewHolder.seekBarReceiver = (DiscreteSeekBar) view.findViewById(R.id.progressBarReceiver);
    viewHolder.imgViewProefileImag = (CircularImageView) view.findViewById(R.id.imgViewProefileImag);
    viewHolder.sendContents = (RelativeLayout) view.findViewById(R.id.sendContents);
    viewHolder.senderVoiceIcon = (ImageView) view.findViewById(R.id.senderVoiceIcon);
    viewHolder.receiverContents = (RelativeLayout) view.findViewById(R.id.receiverContents);
    viewHolder.receiverVoiceIcon = (ImageView) view.findViewById(R.id.receiverVoiceIcon);
    viewHolder.imgPauseReceiver = (ImageView) view.findViewById(R.id.imgPauseReceiver);
    viewHolder.imgPauseSender = (ImageView) view.findViewById(R.id.imgPauseSender);
    viewHolder.receiverLayout = (LinearLayout) view.findViewById(R.id.receiverLayout);
    possitionOfFile = -1;
    possitionOfFile = position;
    pref = new PrefManager(context);
    utils = new Utilities();
    mediaPlayer = new MediaPlayer();
    sqliteHelper = new DatabaseHandler(context);
    if (voiceMessageList.get(position).getDate() != null && !voiceMessageList.get(position).getDate().isEmpty() && voiceMessageList.get(position).getDate().length() > 0) {
        viewHolder.textSeparator.setVisibility(View.VISIBLE);
        viewHolder.dateLayout.setVisibility(View.VISIBLE);
        viewHolder.textSeparator.setText(voiceMessageList.get(position).getDate());
    } else {
        viewHolder.dateLayout.setVisibility(View.GONE);
        viewHolder.textSeparator.setVisibility(View.GONE);
    }

    Typeface myTypeface = Typeface.createFromAsset(context.getAssets(), "Roboto-Medium.ttf");

    if (voiceMessageList.get(position).getExchange_action().equalsIgnoreCase("Receive")) {
        if (voiceMessageList.get(position).getStatus().equalsIgnoreCase("False")) {
            viewHolder.imgViewProefileImag.setVisibility(View.VISIBLE);
        } else {
            viewHolder.imgViewProefileImag.setVisibility(View.INVISIBLE);
        }
        viewHolder.receiverMainLayout.setVisibility(View.VISIBLE);
        viewHolder.senderMainLayout.setVisibility(View.GONE);
        viewHolder.txtReceiveTime.setText(voiceMessageList.get(position).getCurrentTime().replace("m", "M"));
        viewHolder.txtReceiveName.setText(voiceMessageList.get(position).getUser_display_name());
        location = voiceMessageList.get(position).getVoice_file_path();
        if (voiceMessageList.get(position).getTime_of_recorded_voice().length() == 1) {
            viewHolder.txtAudioRemaingreceiverTime.setText("0:" + "0" + voiceMessageList.get(position).getTime_of_recorded_voice());
        } else {
            viewHolder.txtAudioRemaingreceiverTime.setText("0:" + voiceMessageList.get(position).getTime_of_recorded_voice());
        }

    } else if (voiceMessageList.get(position).getExchange_action().equalsIgnoreCase("Send")) {
        viewHolder.receiverMainLayout.setVisibility(View.GONE);
        viewHolder.senderMainLayout.setVisibility(View.VISIBLE);
        viewHolder.txtSendTime.setText(voiceMessageList.get(position).getCurrentTime());
        location = voiceMessageList.get(position).getVoice_file_path();
        if (voiceMessageList.get(position).getTime_of_recorded_voice() != null && !voiceMessageList.get(position).getTime_of_recorded_voice().isEmpty()) {
            viewHolder.txtAudioRemaingSenderTime.setText(voiceMessageList.get(position).getTime_of_recorded_voice());
        } else {
            viewHolder.txtAudioRemaingSenderTime.setText("00:00");
        }
    }

    viewHolder.txtSendTime.setTypeface(myTypeface);

    viewHolder.imgPlaySender.setTag(position);
    viewHolder.senderMainLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            v.setTag(position);
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                viewHolder.imgPauseSender.setVisibility(View.GONE);
                viewHolder.imgPlaySender.setVisibility(View.VISIBLE);
                viewHolder.imgPauseSender.requestFocus();
                viewHolder.imgPlaySender.requestFocus();

            } else {
                mediaPlayer.reset();
                mediaPlayer = MediaPlayer.create(context, Uri.parse(voiceMessageList.get(position).getVoice_file_path()));
                mediaPlayer.start();
                int clickedPosition = (int) v.getTag();
                viewHolder.imgPauseSender.requestFocus();
                viewHolder.imgPlaySender.requestFocus();
                if (clickedPosition == position) {
                    viewHolder.imgPauseSender.setVisibility(View.VISIBLE);
                    viewHolder.imgPlaySender.setVisibility(View.GONE);
                }


            }

        }
    });

    return view;
}



@Override
public void onCompletion(MediaPlayer mp) {
    mediaPlayer.release();
    mediaPlayer = null;
}

static class ViewHolderItem {
    private TextView textSeparator = null;
    private TextView txtReceiveName = null;
    private TextView txtSendTime = null;
    private DiscreteSeekBar seekBarSender = null;
    private ImageView imgPlaySender = null;
    private TextView txtSenderName = null;
    private TextView txtReceiveTime = null;
    private TextView txtAudioRemaingreceiverTime = null;
    private TextView txtAudioRemaingSenderTime = null;
    private RelativeLayout dateLayout = null;
    private LinearLayout senderLayout = null;
    private ImageView imgPlayReceiver = null;
    private RelativeLayout ProgressLayout = null;
    private LinearLayout receiverMainLayout = null;
    private LinearLayout senderMainLayout = null;
    private RelativeLayout ProgressLayoutReceiver = null;
    private DiscreteSeekBar seekBarReceiver = null;
    private CircularImageView imgViewProefileImag = null;
    private RelativeLayout sendContents = null;
    private ImageView senderVoiceIcon = null;
    private RelativeLayout receiverContents = null;
    private ImageView receiverVoiceIcon = null;
    private ImageView imgPauseReceiver = null;
    private ImageView imgPauseSender = null;
    private LinearLayout receiverLayout = null;
}
}

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp">


<LinearLayout
    android:id="@+id/senderMainLayout"
    android:layout_width="335dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/receiverMainLayout"
    android:layout_marginLeft="25dp"
    android:orientation="horizontal"
    android:visibility="visible">

    <LinearLayout
        android:id="@+id/senderDummyImgLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <com.app.materialdesign.CircularImageView
            android:id="@+id/imgViewDummySender"
            android:layout_width="70dp"
            android:layout_height="40dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_weight="1"
            android:background="@drawable/attach_contact_pressed"
            android:visibility="invisible"
            app:border="true"
            app:border_color="#eeeeee"
            app:border_width="4dp"
            app:shadow="true" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/senderLayout"
        android:layout_width="match_parent"
        android:layout_height="47dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"
        android:layout_weight="2"
        android:background="@drawable/chat"
        android:gravity="left"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:orientation="horizontal"
            android:weightSum="4.5">

            <TextView
                android:id="@+id/txtSenderName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:layout_marginLeft="25dp"
                android:layout_marginTop="10dp"
                android:text="Midhun"
                android:textSize="13sp"
                android:visibility="invisible" />

            <TextView
                android:id="@+id/txtSendTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end"
                android:layout_marginRight="15dp"
                android:layout_marginTop="10dp"
                android:layout_weight="4.5"
                android:gravity="right"
                android:text="12:30AM"
                android:textColor="#ffffff"
                android:textSize="10sp" />
        </LinearLayout>


        <LinearLayout
            android:id="@+id/sendTimeLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:orientation="vertical">

        </LinearLayout>

        <RelativeLayout
            android:id="@+id/sendContents"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="-30dp">

            <ImageView
                android:id="@+id/senderVoiceIcon"
                android:layout_width="15dp"
                android:layout_height="15dp"
                android:layout_marginBottom="15dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="8dp"
                android:background="@drawable/ic_record_voice_over_white" />

            <RelativeLayout
                android:id="@+id/ProgressLayout"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_alignTop="@+id/senderVoiceIcon"
                android:layout_marginLeft="13dp"
                android:layout_marginStart="13dp"
                android:layout_marginTop="-3dp"
                android:layout_toEndOf="@+id/senderVoiceIcon"
                android:background="@drawable/ic_panorama_fish_eye_white">

                <ImageView
                    android:id="@+id/imgPlaySender"
                    android:layout_width="9.5dp"
                    android:layout_height="9.5dp"
                    android:layout_alignParentEnd="true"
                    android:layout_centerVertical="true"
                    android:layout_marginEnd="4dp"
                    android:background="@drawable/ic_play_blue"
                    android:visibility="visible" />

                <ImageView
                    android:id="@+id/imgPauseSender"
                    android:layout_width="9.5dp"
                    android:layout_height="9.5dp"
                    android:layout_alignParentEnd="true"
                    android:layout_centerVertical="true"
                    android:layout_marginEnd="5dp"
                    android:background="@drawable/fa_pause"
                    android:visibility="gone" />
            </RelativeLayout>


            <com.app.materialdesign.DiscreteSeekBar
                android:id="@+id/seekBarSender"
                android:layout_width="153dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="-10dp"
                android:layout_toEndOf="@+id/ProgressLayout"
                android:layout_toRightOf="@+id/ProgressLayout"
                app:dsb_allowTrackClickToDrag="false"
                app:dsb_indicatorColor="#FFFFFF"
                app:dsb_indicatorFormatter="\ %d"
                app:dsb_indicatorTextAppearance="@style/CustomFloaterTextAppearance"
                app:dsb_progressColor="#FFFFFF"
                app:dsb_rippleColor="@color/BlueButton"
                app:dsb_trackColor="#000000" />

            <TextView
                android:id="@+id/txtAudioRemaingSenderTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/seekBarSender"
                android:layout_gravity="left"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="-10dp"
                android:layout_toEndOf="@+id/ProgressLayout"
                android:layout_toRightOf="@+id/ProgressLayout"
                android:text="0:23"
                android:textColor="@color/VoiceDuration"
                android:textSize="8sp" />
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/senderImageLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"
        android:visibility="gone">

        <com.app.materialdesign.CircularImageView
            android:id="@+id/senderImgView"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_weight="1"
            android:background="@drawable/attach_contact_pressed"
            app:border="true"
            app:border_color="#eeeeee"
            app:border_width="4dp"
            app:shadow="true" />
    </LinearLayout>
</LinearLayout>



</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

您可以使用setTag()和getTag()来获取适配器中项目的点击位置。

例如:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView= layoutInflater.inflate(R.layout.item, null);
    ImageView ivPlay=(ImageView)convertView.findViewById(R.id.imageView1);

    ivPlay.setTag(position); // set position in tag

    ivPlay.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int clickedPosition = (int) v.getTag(); // get position from tag
            // Here you can perform operation based on position.
        }
    });


    return convertView;
}

答案 1 :(得分:0)

只需更改此位置上对象的某些标志,然后调用notifyDatasetChanged()。这将调用重绘项目,然后在getView中检查该标志并相应地绘制图像。 如果您将此项目滚出屏幕并返回,则无法直接在imageView中更改图像。

答案 2 :(得分:0)

嗯,你有很多代码,我写了你需要的任何必要的东西来回答你的问题..介意代码评论

    int playingItemPosition=-1;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        |ViewHolderItem viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolderItem();
            LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.adapter_layout, null);
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.receiverMainLayout = (LinearLayout) convertView.findViewById(R.id.receiverMainLayout);
            viewHolder.imgPauseSender = (ImageView) convertView.findViewById(R.id.imgPauseSender);
            //write all findViewById() here
            viewHolder.imgPlaySender = (ImageView) convertView.findViewById(R.id.imgPlaySender);
            convertView.setTag(viewHolder);
            //setting listener here
            viewHolder.receiverMainLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = (int) v.getTag();
                    playingItemPosition = pos;//new item clicked and stop o
                    notifyDataSetChanged();//refresh views
                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.pause();
                        viewHolder.imgPauseSender.requestFocus();
                        viewHolder.imgPlaySender.requestFocus();
                    } else {
                        mediaPlayer.reset();
                        mediaPlayer = MediaPlayer.create(context, Uri.parse(voiceMessageList.get(playingItemPosition).getVoice_file_path()));
                        mediaPlayer.start();
                        viewHolder.imgPauseSender.requestFocus();
                        viewHolder.imgPlaySender.requestFocus();
                    }
                }
            });
        } else {
            viewHolder = (ViewHolderItem) convertView.getTag();
        }
        //check clicked position and change icon accordingly. 
        if (playingItemPosition != -1) {
            if (position == playingItemPosition) {
                viewHolder.imgPauseSender.setVisibility(View.GONE);
                viewHolder.imgPlaySender.setVisibility(View.VISIBLE);
            } else {
                viewHolder.imgPauseSender.setVisibility(View.VISIBLE);
                viewHolder.imgPlaySender.setVisibility(View.GONE);
            }
        }
        //set values to your items 
        viewHolder.receiverMainLayout.setTag(position);


        return convertView;
    }