我正在尝试创建一个音频文件预览页面,其中包含文件夹中的listview
个文件。每个row
旁边都有一个play button
,点击后会更改为stop button
。如果单击一行,我现在需要一种将所有其他images
设置为play button
的方法,因为一次只能播放一个audio file
。我尝试迭代列表并从中获取imageview
但没有运气(如下所示):
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
IncidentLogger logger = new IncidentLogger(this.getActivity());
final List<Audio> audioList = logger.getAllAudio();
AudioListViewAdapter adapter = new AudioListViewAdapter(audioList, getActivity());
final ListView listView = (ListView) getActivity().findViewById(R.id.listView);
listView.setAdapter(adapter);
OnItemClickListener onItemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for(int i = 0; i < audioList.size(); i++) {
View v = listView.getAdapter().getView(i, view, parent);
ImageView imageView = (ImageView) v.findViewById(R.id.control);
imageView.setImageResource(R.drawable.ic_action_play);
}
ImageView iv = (ImageView) view.findViewById(R.id.control);
if(iv.getTag() == "1") {
iv.setTag("0");
iv.setImageResource(R.drawable.ic_action_play);
} else {
iv.setTag("1");
iv.setImageResource(R.drawable.ic_action_stop);
}
}
};
listView.setOnItemClickListener(onItemClickListener);
}
答案 0 :(得分:0)
这可以轻松完成,您只需查看用户点击的列表项的位置即可。一个描述完整的演示代码如下...
public class ChangeImageInListDynamically extends Activity {
/**
* Adapter for your data.
*/
ImageAdapter adpAR;
ArrayList<String> itemList;
ListView mListView;
ImageAdapter mAdapter;
/**
* Indicate which item is currently selected.
* Its default selected item is '0' e.g., 1st item.
*/
private int selectedPosition=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
itemList = new ArrayList<String>();
for (int i = 0; i < 11; i++) {
itemList.add("Item - "+i);
}
mListView = (ListView)findViewById(R.id.data_list);
//Initialize your adapter
mAdapter = new ImageAdapter(nChangeImageInListDynamically.this, itemList);
//set adapter to your list.
mListView.setAdapter(mAdapter);
//set list click listener.
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View view, int position, long id) {
//your image that need to change dynamically.
ImageView imageView = (ImageView) view.findViewById(R.id.select);
//position of clicked item.
selectedPosition=position;
//change image of respective image.
imageView.setImageResource(R.drawable.selected);
//notify your adapter to update your list.
adpAR.notifyDataSetChanged();
}
});
}
static class ViewHolder {
TextView title;
ImageView img;
}
/**
* Your ImageAdapter.
*/
public class ImageAdapter extends BaseAdapter {
ArrayList<String> dataList;
Context context;
public ImageAdapter(Context context, ArrayList<String> arrayList) {
this.context = context;
this.dataList = arrayList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return dataList.size();;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final ViewHolder holder;
Log.d("posiiton ImageAdapater : ",""+position);
if (row == null) {
LayoutInflater inflater = ChangeImageInListDynamically.this.getLayoutInflater();
row = inflater.inflate(R.layout.listrow, null);
holder = new ViewHolder();
holder.title = (TextView) row.findViewById(R.id.text);
holder.img = (ImageView) row.findViewById(R.id.select);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
//set titel text.
holder.title.setText(dataList.get(position));
//Change Image of selected item dynamically.
if(selectedPosition == position) {
//Item is selected.
holder.img.setImageResource(R.drawable.selected);
}else{
//Other non-selected items.
holder.img.setImageResource(R.drawable.dis_selected);
}
return row;
}
}
}