更改ListView中所选项目的TextView可见性

时间:2015-03-05 17:18:31

标签: android listview

我有一个播放列表,我将当前播放的歌曲设置为选中,选中并为后台选择器提供焦点,并在列表顶部显示当前歌曲。

现在我还想在选择此项目时显示隐藏的TextView,但不知何故我失败了。 我打赌它很容易,但也许今天不是我的日子; - )

public static void changeUI() {
    position = PlayerConstants.SONG_NUMBER;

    mediaListView.setItemChecked(position, true);
    mediaListView.setSelection(position);

    mediaListView.getChildAt(position).setVisibility(nowPlaying.VISIBLE);

    View v = mediaListView.getChildAt(position);
    if (v != null) {
        v.requestFocus();
    }
}

我尝试获取所选的孩子并设置TextView" nowPlaying"可见但没有任何反应。

*更新,按要求发布完整代码*

public class PlayList{

CustomAdapter customAdapter = null;
static TextView playingSong, nowPlaying;
static ListView mediaListView;
public static String getMode;
static Context context;
private static int position;
private static boolean playlistVisible;

private static final String STATE_SELECTED_SORTING_ITEM = "selected_sorting_item";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.player_activity_playlist);
    context = PlayList.this;

    init();
}

private void init() {
    getViews();
    setListeners();

    if (PlayerConstants.SONGS_LIST.size() <= 0) {
        PlayerConstants.SONGS_LIST = UtilFunctions
                .listOfSongs(getApplicationContext());
    }
    setListItems();
}

private void setListItems() {
    customAdapter = new CustomAdapter(this, R.layout.player_activity_playlist_listitem_song,
            PlayerConstants.SONGS_LIST);
    mediaListView.setAdapter(customAdapter);
    mediaListView.setFastScrollEnabled(true);
}

private void getViews() {
    mediaListView = (ListView) findViewById(R.id.listViewMusic);
    nowPlaying = (TextView) findViewById(R.id.nowPlaying);
}

private void setListeners() {
    mediaListView
    .setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View item,
                int position, long id) {
            Log.d("TAG", "TAG Tapped INOUT(IN)");
            PlayerConstants.SONG_PAUSED = false;
            PlayerConstants.SONG_NUMBER = position;
            boolean isServiceRunning = UtilFunctions
                    .isServiceRunning(SongService.class.getName(),
                            getApplicationContext());
            if (!isServiceRunning) {
                Intent i = new Intent(getApplicationContext(),
                        SongService.class);
                startService(i);
            } else {
                PlayerConstants.SONG_CHANGE_HANDLER
                .sendMessage(PlayerConstants.SONG_CHANGE_HANDLER
                        .obtainMessage());
            }

            // Return to calling activity
            Intent in = new Intent(getApplicationContext(),
                    AudioPlayer.class);
            setResult(100, in);
            // Closing PlayListView
            finish();
        }
    });

}

public static void changeUI() {
    position = PlayerConstants.SONG_NUMBER;

    mediaListView.setItemChecked(position, true);
    mediaListView.setSelection(position);

    mediaListView.getChildAt(position).setVisibility(nowPlaying.VISIBLE);

    View v = mediaListView.getChildAt(position);
    if (v != null) {
        v.requestFocus();
    }
}

}

1 个答案:

答案 0 :(得分:0)

要实现这个目标:  1. listOnItemClickListener for listview  1.通过适配器的对象传递适配器中的位置值(您可以在适配器类中创建方法)  2.调用notifyDatasetchange();  3.在getView中创建一个if else

if(selectedPosition == position(getview position) )
 textview.setVisibility(View.INVISIBLE);
else
textview.setVisibility(View.VISIBLE);

我希望它会有所帮助