我有一个收音机应用程序,当我点击广播电台的一个单元格时,我需要在该单元格中显示一个播放图标并隐藏之前的ImageView。
例如,我点击 Radio 1 ,它开始流式播放并在右侧显示一个播放图标,如下所示:
然后,当我点击另一个单元格时,该播放图标必须隐藏,并且必须显示在所选单元格中。
这是我的(不完整)代码:
// MARK: - QUERY STATIONS ---------------------------------------------------------------
void queryStations(String genre) {
Configs.showPD("Loading Stations...", Stations.this);
final List<String> stationColors = new ArrayList<>(Arrays.asList(Configs.stationColors));
ParseQuery<ParseObject> query = ParseQuery.getQuery(Configs.STATIONS_CLASS_NAME);
query.whereEqualTo(Configs.STATIONS_GENRE, genre);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException error) {
if (error == null) {
stationsArray = objects;
Configs.hidePD();
// CUSTOM LIST ADAPTER
class ListAdapter extends BaseAdapter {
private Context context;
private ListAdapter(Context context, List<ParseObject> objects) {
super();
this.context = context;
}
// CONFIGURE CELL
@Override
public View getView(final int position, View cell, ViewGroup parent) {
if (cell == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
assert inflater != null;
cell = inflater.inflate(R.layout.cell_station, null);
}
final View finalCell = cell;
...
final ImageView playingIcon = finalCell.findViewById(R.id.csPlayingImage);
playingIcon.setTag(position);
Log.i("log-", "P.ICON TAG: " + playingIcon.getTag());
// Play selected radio station
sNametxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("log-", "CELL TAPPED!");
stationIndex = position;
playRadioStation();
playingIcon.setVisibility(View.VISIBLE); // HERE'S WHERE I SHOW THE PLAYING ICON
}
});
}
}
正如您在我的代码中看到的那样, playingIcon 会正确显示,但是当我点击下一个单元格时,第一个播放图标显然不会被隐藏。
如何让它不可见并仅显示所选的 playingIcon ImageView?
非常感谢!
答案 0 :(得分:2)
试试这个。它应该工作:
class ListAdapter extends BaseAdapter {
...
private int stationIndex = -1; //if needed
...
@Override
public View getView(final int position, View cell, ViewGroup parent) {
...
final ViewGroup _parent = parent; //for access in inner class
sNametxt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("log-", "CELL TAPPED!");
playRadioStation();
playingIcon.setVisibility(View.VISIBLE);
if (stationIndex >= 0) {
if (stationIndex != position) {
//get playingIcon of the the cell with position==stationIndex:
_parent.getChildAt(stationIndex).findViewById(R.id.csPlayingImage).setVisibility(View.GONE);
}
}
stationIndex = position;
}
});
...
}
...
}
答案 1 :(得分:1)
在 sNametxt.setOnClickListener 中尝试此操作:
if(playingIcon.getTag() == stationIndex){
playingIcon.setVisibility(View.VISIBLE);
} else {
playingIcon.setVisibility(View.GONE);
}
这只会让你在点击的地方播放可见的内容。 而另一个将是GONE。