gpt4 book ai didi

java - Listview多重复 Action ?

转载 作者:行者123 更新时间:2023-12-01 14:38:29 25 4
gpt4 key购买 nike

我的 ListView 中有重复的操作问题我在互联网上做了很多研究都失败了我的问题是当我点击项目编号1它展开时,当我向下滚动分页附加更多项目时我发现项目编号10也被扩展,另一个例子,如果我切换项目编号 2,则编号 2 和编号 12 项目都会一起切换!!

如何解决这个重复问题?

这是 ListView 类

public class Ringtones_Listview extends ArrayAdapter<Object> {

int resource;
String response;
Context context;
private LayoutInflater mInflater;
MediaPlayer mp = null;
ImageView play_clicked;
View vv = null;
String lang = Locale.getDefault().getDisplayLanguage();
ArrayList ob;
Integer clickedposition;

public Ringtones_Listview(Context context, int resource, ArrayList objects) {
super(context, resource,objects);

this.context = context;
this.ob = objects;
this.resource = resource;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return ob.size();
}

public JSONObject getItem(JSONObject position) {
return position;
}

public long getItemId(int position) {
return position;
}
static class ViewHolder {

ImageView play ;
ImageView download ;
TextView rtitle;
TextView size;
TextView downloads;
TextView personname;
TextView date;
RatingBar ratingsmall;
ImageView ratebutton;
long tonid;
TextView voters;
LinearLayout more;
}

@SuppressWarnings("deprecation")
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
//Get the current location object
JSONObject r = (JSONObject) getItem(position);

//Inflate the view
if(convertView == null)
{

convertView = mInflater.inflate(R.layout.ringtone_bit, null);
holder = new ViewHolder();
convertView.setTag(holder);


holder.play = ( ImageView ) convertView.findViewById(R.id.play);
holder.download = ( ImageView ) convertView.findViewById(R.id.download);
holder.ratebutton = ( ImageView ) convertView.findViewById(R.id.ratebutton);
holder.rtitle = (TextView) convertView.findViewById(R.id.rtitle);
holder.size = (TextView) convertView.findViewById(R.id.size);
holder.downloads = (TextView) convertView.findViewById(R.id.downloads);
holder.voters = (TextView) convertView.findViewById(R.id.voters);
holder.personname = (TextView) convertView.findViewById(R.id.personname);
holder.date = (TextView) convertView.findViewById(R.id.date);
holder.ratingsmall = (RatingBar) convertView.findViewById(R.id.ratingsmall);
holder.more = ( LinearLayout ) convertView.findViewById(R.id.more);

}
else
{
holder = (ViewHolder) convertView.getTag();
}

//hide More information
holder.more.setVisibility(View.GONE);

try {
Date date_g = new Date(r.getLong("timestamp") * 1000);
holder.date.setText(date_g.toLocaleString());
} catch (JSONException e2) {}

//set person name
try {
String client_name = ( r.getString("personname").equals( "null" ) == true ) ? "ghost" : r.getString("personname");
holder.personname.setText(client_name);
} catch (JSONException e2) {}

//set total votars and vote avarage
try {
float z = (float) r.getInt("rate");
holder.voters.setText(" ( "+ r.getLong("voters") +" ) / " + z);
} catch (JSONException e2) {}
//set rating bar
try {
float z = (float) r.getInt("rate");
holder.ratingsmall.setRating(z);
} catch (JSONException e2) {}
//set ringtone Name as defualt device language
try {
String name = ( lang.equals( "English" ) == true ) ? r.getString("en_name") : r.getString("ar_name");
holder.rtitle.setText(name);
} catch (JSONException e2) {}

//ringtone file size
try {
holder.size.setText(r.getString("size"));
} catch (JSONException e2) {}

//set downloads
try {
holder.downloads.setText(String.valueOf( r.getLong("downloads") ));
} catch (JSONException e2) {}

//set ringtone ID toneid
try {
holder.tonid = r.getLong("toneid");
holder.download.setTag(r.getLong("toneid"));
holder.ratebutton.setTag(r.getLong("toneid"));
holder.play.setId((int) r.getLong("toneid"));
convertView.setId((int) r.getLong("toneid"));

} catch (JSONException e1) {}

//set download stram url to play icon
try {
holder.play.setTag(r.getString("stream_url"));
} catch (JSONException e) {}

//add play listener test Ringtone before download it
holder.play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {

clickedposition=position;
boolean playit = false;

v.setMinimumHeight(200);

//stop Media player dont play any thing
if ( mp != null )
{
mp.stop();
mp.release();
mp = null;
}

//check for last clicked item
if ( vv != null )
{
//set last played item to play icon
vv.setBackgroundResource(R.drawable.play);

//check if last played is not smae clicked item
if( v.getTag().toString().equals( vv.getTag().toString() ) == false )
{
playit = true;
}else
//its same item dont play stop here
{
vv = null;
}
}else
//nothing played yet play it
{
playit = true;
}


if ( playit )
{

vv = v;

v.setBackgroundResource(R.drawable.preparing_player);


AnimationDrawable frameAnimation = (AnimationDrawable) v.getBackground();
frameAnimation.start();


String stramUrl = String.valueOf(v.getTag());


try {

mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(stramUrl);
mp.prepareAsync();


} catch (Exception e) {}


//preparing straming
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

@Override
public void onPrepared(MediaPlayer mps) {

v.setBackgroundResource(R.drawable.played);
mps.start();
}
});

mp.setOnCompletionListener( new MediaPlayer.OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mp) {
v.setBackgroundResource(R.drawable.play);
}
});

}




}
});




return convertView;
}

}

最佳答案

ListView 出于性能原因将回收 View ,它每次都会使用您的第一个膨胀 View ,因此当您展开它和向下滚动时,其他 View 也会使用相同的布局,您将能够看到扩展的 View 。

编辑:

设置

vv.setBackgroundResource(R.drawable.play);//当您没有在 getView() 中单击播放按钮时的默认图像

我认为一次只会播放一首歌曲,因此需要一个 int 来跟踪已单击的 Imageview

像这样

整数点击位置;将其作为一个字段,然后

在单击play ImageView 时的getView()中,将其位置设置为clickedposition。

clickedposition=position;

在 getview 中这样检查

if(clickedposition!=null)
{
v.setBackgroundResource(R.drawable.played);
}

编辑:

int checkposition=-1;

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

holder.play.setBackgroundResource(R.drawable.play);
if(checkposition!=-1)
{
holder.play.setBackgroundResource(R.drawable.played);
}


return convertView;
}

关于java - Listview多重复 Action ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16240751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com