Android ListView 中的 SeekBar 问题
Android SeekBar Issue in ListView
我在列表视图中使用搜索栏时遇到问题,点击按钮时搜索栏的搜索开始更新,但当我们在列表中有多个搜索栏时会出现问题
假设我的列表中有三个搜索栏,如果我单击第二个或第三个搜索栏,第一个(在列表中)搜索栏的搜索开始始终更新。
请帮我解决这个问题。
下面是我的代码:
xml 中的列表视图:
<ListView
android:id="@+id/lstMessages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:transcriptMode="alwaysScroll"
android:layout_above="@+id/relSendMessage"
android:layout_below="@+id/relRecipient"
android:background="@drawable/bg_shadow"
android:padding="0dip"
android:divider="@null"
android:dividerHeight="0dp" android:stackFromBottom="true" tools:listitem="@layout/message_left"/
Adapter Class Code :
public class ChatAdapter extends BaseAdapter implements OnCompletionListener{
Context context;
TextView txtmsg;
ImageView imgSender ,playaudio;
TextView txtSender;
TextView txtDate;
VideoView videosender;
SeekBar seekbar;
MediaPlayer mp;
private boolean bVideoIsBeingTouched = false;
private Handler mHandler = new Handler();
ArrayList<HashMap<String, String>> data;
public ChatAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
// TODO Auto-generated constructor stub
this.context = context;
data = arraylist;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public View getView( int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
HashMap<String, String> resultp = new HashMap<String, String>();
resultp = data.get(position);
if (convertView == null) {
int res = 0;
if(resultp.get(Tab2.CHAT_MSG).equals("/AUDIO/")&&resultp.get(Tab2.CHAT_TYPE).equals("outgoing"))
{
final String path=resultp.get(Tab2.PATH);
res = R.layout.audioleft;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(res, parent, false);
txtSender = (TextView) convertView.findViewById(R.id.txtSender);
playaudio = (ImageView) convertView.findViewById(R.id.imageView1);
seekbar = (SeekBar) convertView.findViewById(R.id.songProgressBar);
playaudio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
getActivity().setVolumeControlStream(android.media.AudioManager.STREAM_MUSIC);
mp = new MediaPlayer();
mp.setOnCompletionListener(ChatAdapter.this);
try {
playaudio.setBackgroundResource(R.drawable.img_btn_pause);
mp.setDataSource(path);
mp.prepare();
mp.start();
seekbar.setProgress(0);
seekbar.setMax(100);
// Updating progress bar
updateProgressBar();
} catch (Exception e) {
e.printStackTrace();
}
}
});
txtDate = (TextView) convertView.findViewById(R.id.txtDate);
}
}
else
{
}
return convertView;
}
}
public void updateProgressBar() {
mHandler.postDelayed(mUpdateTimeTask, 100);
}
/**
* Background Runnable thread
* */
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
long totalDuration = mp.getDuration();
long currentDuration = mp.getCurrentPosition();
// Displaying Total Duration time
// Displaying time completed playing
);
// Updating progress bar
int progress = (int)(getProgressPercentage(currentDuration, totalDuration));
//Log.d("Progress", ""+progress);
seekbar.setProgress(progress);
// Running this thread after 100 milliseconds
mHandler.postDelayed(this, 100);
}
};
audioleft.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="wrap_content"
android:padding="10dp">
<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_marginRight="10dp">
<TextView
android:id="@+id/txtSender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Sender Ipsum"
android:textSize="16sp"
android:singleLine="true"
android:layout_alignParentLeft="true"
android:paddingLeft="10dp"
android:paddingBottom="6dp"
android:textColor="@android:color/black" android:textStyle="bold"/>
<TextView
android:id="@+id/txtDate"
android:layout_alignParentRight="true"
android:textColor="@color/sinch_purple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="2dp"
android:paddingBottom="6dp"
android:textSize="12sp"
android:text="14:54"
android:layout_alignBottom="@+id/txtSender"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtSender"
android:layout_alignParentLeft="true"
android:padding="12dp"
>
<SeekBar
android:id="@+id/songProgressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="20dp"
android:thumb="@drawable/sthumb"
android:progressDrawable="@drawable/seekbar_progress"
/>
<!-- android:thumb="@drawable/seek_handler" -->
<ImageView
android:id="@+id/imageView1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignBottom="@+id/songProgressBar"
android:layout_alignParentLeft="true"
android:layout_marginLeft="17dp"
android:background="@drawable/img_btn_play" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
每次在 getView() 中扩充新的监听器时,您都必须创建新的监听器并将其影响到搜索栏
seekbar.setOnXXXListener(new XXXListener(){
public void onSeekXXX(){
//do your thing about this seekbar...
}
)};
编辑:
您真的应该考虑使用 ViewHolder 模式:External link
实际上问题出在我的计时器上,完成后它不会停止 我在每次搜索栏完成后停止计时器,问题就解决了。
我在列表视图中使用搜索栏时遇到问题,点击按钮时搜索栏的搜索开始更新,但当我们在列表中有多个搜索栏时会出现问题 假设我的列表中有三个搜索栏,如果我单击第二个或第三个搜索栏,第一个(在列表中)搜索栏的搜索开始始终更新。
请帮我解决这个问题。
下面是我的代码:
xml 中的列表视图:
<ListView
android:id="@+id/lstMessages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:transcriptMode="alwaysScroll"
android:layout_above="@+id/relSendMessage"
android:layout_below="@+id/relRecipient"
android:background="@drawable/bg_shadow"
android:padding="0dip"
android:divider="@null"
android:dividerHeight="0dp" android:stackFromBottom="true" tools:listitem="@layout/message_left"/
Adapter Class Code :
public class ChatAdapter extends BaseAdapter implements OnCompletionListener{
Context context;
TextView txtmsg;
ImageView imgSender ,playaudio;
TextView txtSender;
TextView txtDate;
VideoView videosender;
SeekBar seekbar;
MediaPlayer mp;
private boolean bVideoIsBeingTouched = false;
private Handler mHandler = new Handler();
ArrayList<HashMap<String, String>> data;
public ChatAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
// TODO Auto-generated constructor stub
this.context = context;
data = arraylist;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public View getView( int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
HashMap<String, String> resultp = new HashMap<String, String>();
resultp = data.get(position);
if (convertView == null) {
int res = 0;
if(resultp.get(Tab2.CHAT_MSG).equals("/AUDIO/")&&resultp.get(Tab2.CHAT_TYPE).equals("outgoing"))
{
final String path=resultp.get(Tab2.PATH);
res = R.layout.audioleft;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(res, parent, false);
txtSender = (TextView) convertView.findViewById(R.id.txtSender);
playaudio = (ImageView) convertView.findViewById(R.id.imageView1);
seekbar = (SeekBar) convertView.findViewById(R.id.songProgressBar);
playaudio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
getActivity().setVolumeControlStream(android.media.AudioManager.STREAM_MUSIC);
mp = new MediaPlayer();
mp.setOnCompletionListener(ChatAdapter.this);
try {
playaudio.setBackgroundResource(R.drawable.img_btn_pause);
mp.setDataSource(path);
mp.prepare();
mp.start();
seekbar.setProgress(0);
seekbar.setMax(100);
// Updating progress bar
updateProgressBar();
} catch (Exception e) {
e.printStackTrace();
}
}
});
txtDate = (TextView) convertView.findViewById(R.id.txtDate);
}
}
else
{
}
return convertView;
}
}
public void updateProgressBar() {
mHandler.postDelayed(mUpdateTimeTask, 100);
}
/**
* Background Runnable thread
* */
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
long totalDuration = mp.getDuration();
long currentDuration = mp.getCurrentPosition();
// Displaying Total Duration time
// Displaying time completed playing
);
// Updating progress bar
int progress = (int)(getProgressPercentage(currentDuration, totalDuration));
//Log.d("Progress", ""+progress);
seekbar.setProgress(progress);
// Running this thread after 100 milliseconds
mHandler.postDelayed(this, 100);
}
};
audioleft.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="wrap_content"
android:padding="10dp">
<RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_marginRight="10dp">
<TextView
android:id="@+id/txtSender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Sender Ipsum"
android:textSize="16sp"
android:singleLine="true"
android:layout_alignParentLeft="true"
android:paddingLeft="10dp"
android:paddingBottom="6dp"
android:textColor="@android:color/black" android:textStyle="bold"/>
<TextView
android:id="@+id/txtDate"
android:layout_alignParentRight="true"
android:textColor="@color/sinch_purple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="2dp"
android:paddingBottom="6dp"
android:textSize="12sp"
android:text="14:54"
android:layout_alignBottom="@+id/txtSender"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/txtSender"
android:layout_alignParentLeft="true"
android:padding="12dp"
>
<SeekBar
android:id="@+id/songProgressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="20dp"
android:thumb="@drawable/sthumb"
android:progressDrawable="@drawable/seekbar_progress"
/>
<!-- android:thumb="@drawable/seek_handler" -->
<ImageView
android:id="@+id/imageView1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignBottom="@+id/songProgressBar"
android:layout_alignParentLeft="true"
android:layout_marginLeft="17dp"
android:background="@drawable/img_btn_play" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
每次在 getView() 中扩充新的监听器时,您都必须创建新的监听器并将其影响到搜索栏
seekbar.setOnXXXListener(new XXXListener(){
public void onSeekXXX(){
//do your thing about this seekbar...
}
)};
编辑:
您真的应该考虑使用 ViewHolder 模式:External link
实际上问题出在我的计时器上,完成后它不会停止 我在每次搜索栏完成后停止计时器,问题就解决了。