单击时更新ListView的单行内的视图

时间:2017-03-28 16:46:56

标签: android listview android-arrayadapter android-adapterview

我想知道如何在单击行时在ListView的单行内更新视图/ UI(例如,使TextView不可见),因为每隔一个行& #39;意见也会受到影响。一个很好的例子就是在Google Play音乐中播放歌曲,并相应地显示均衡器动画。这是我的片段:

MainActivity' onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); // Renders only a ListView

    ListView songListView = (ListView) findViewById(R.id.list);
    List<Song> songList = new ArrayList<>();
    for (int i = 1; i <= 200; i++) songList.add(new Song("Song" + i, "Artist" + i));
    SongAdapter songAdapter = new SongAdapter(this, songList);
    songListView.setAdapter(songAdapter);
    songListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
            if (position == view.getId()) {
                TextView titleText = (TextView) view.findViewById(R.id.song_title);
                titleText.setVisibility(View.INVISIBLE);
            }
        }
    });
}

...和我的ArrayAdapter的getView方法和ViewHolder类:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent,
                false);

        holder = new ViewHolder();
        holder.song = (TextView) convertView.findViewById(R.id.song_title);
        holder.artist = (TextView) convertView.findViewById(R.id.song_artist);

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

    convertView.setId(position);

    Song currentSong = getItem(position);

    if (currentSong != null) {
        holder.song.setText(currentSong.getTitle());
        holder.artist.setText(currentSong.getArtist());
    }

    return convertView;
}

private static class ViewHolder {
    TextView song;
    TextView artist;
}

4 个答案:

答案 0 :(得分:1)

在歌曲模型类中再添加一个变量以提高每行的可见性,请尝试以下代码:

class Song{
    private boolean isShowing =false;

    public boolean isShowing() {
        return isShowing;
    }

    public void setShowing(boolean showing) {
        isShowing = showing;
    }
}

SongsAdapter.class

class SongAdapter extends BaseAdapter {
           private Context mContext;
           private List<Song> mSongList;


           public SongAdapter(Context context, List<Song> songs) {
               mContext = context;
               mSongList = songs;
           }

           @Override
           public int getCount() {
               return mSongList.size();
           }

           @Override
           public Object getItem(int i) {
               return null;
           }

           @Override
           public long getItemId(int i) {
               return 0;
           }

           @Override
           public View getView(final int position, View convertView, ViewGroup parent) {
               ViewHolder holder;

               if (convertView == null) {
                   convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent,
                           false);
                   holder = new ViewHolder(convertView);

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

               final Song songModel = mSongList.get(position);
               holder.song.setText(songModel.getSongTitle());
               holder.artist.setText(songModel.getSongArtist());

               if(songModel.isShowing()){
                   holder.song.setVisibility(View.VISIBLE);
               }else{
                   holder.song.setVisibility(View.GONE);
               }
               convertView.setOnClickListener(new View.OnClickListener() {
                   @Override
                   public void onClick(View view) {
                       if(songModel.isShowing()){
                           songModel.setShowing(false);
                       }else{
                           songModel.setShowing(true);
                       }

                       notifyDataSetChanged();

                   }
               });
               return convertView;
           }

           public class ViewHolder{
               TextView song;
               TextView artist;

               public ViewHolder(View itemView) {
                   song = (TextView)itemView.findViewById(R.id.song_title);
                   artist = (TextView)itemView.findViewById(R.id.artist);

               }

           }
       }

答案 1 :(得分:0)

希望这会有所帮助:

holder.song.setId(position);

然后您的代码如下所示:

mSongView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
    Log.d(LOG_TAG, "Song item clicked");
    TextView titleText = (TextView) view.findViewById(position);
    titleText.setVisibility(View.INVISIBLE);

}
});

答案 2 :(得分:0)

在适配器类中添加以下代码

Duration     |  ID  
0h 7m 55sec. |  1
0h 25m 33sec.|  2
10h 5m 5sec. |  3
4h 11m 31sec.|  4

修改您的观察者类

public interface OnItemClickListener {
void onClick(View view);}
private final OnItemClickListener listener;
public YourAdapter(Context mContext, OnItemClickListener listener) {
this.mContext = mContext;
this.listener = listener;}

最后在Activity类中实现接口

class ViewHolder implements View.OnClickListener {

TextView song;
TextView artist;

ViewHolder(View itemView) {
    super(itemView);
    song = (TextView)itemView.findViewById(R.id.txt1);
    artist = (TextView)itemView.findViewById(R.id.txt2);
    itemView.setOnClickListener(this);

}

 @ Override
public void onClick(View v) {
    listener.onClick(v);
}

}

答案 3 :(得分:0)

在得到导师的帮助后,我终于明白了这一点。在适配器中初始化一个int字段以保留列表中先前位置的记录,其工作原理如下:

MainActivity:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView songListView = (ListView) findViewById(R.id.list);
        List<Song> songList = new ArrayList<>();
        for (int i = 1; i <= 200; i++) songList.add(new Song("Song" + i, "Artist" + i));
        final SongAdapter songAdapter = new SongAdapter(this, songList);
        songListView.setAdapter(songAdapter);
        songListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
                songAdapter.setSongPosition(position);
                songAdapter.notifyDataSetChanged();
            }
        });
    }
}

自定义歌曲课程:

public class Song {
    private String mSongTitle, mSongArtist;

    public Song(String songTitle, String songArtist) {
        mSongTitle = songTitle;
        mSongArtist = songArtist;
    }

    public String getSongTitle() {
        return mSongTitle;
    }

    public String getSongArtist() {
        return mSongArtist;
    }
}

自定义适配器:

public class SongAdapter extends ArrayAdapter<Song> {
    private int currentlyPlaying = -1;

    public SongAdapter(Context context, List<Song> songList) {
        super(context, 0, songList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent,
                    false);

            holder = new ViewHolder(convertView);

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

        Song song = getItem(position);
        holder.song.setText(song.getSongTitle());
        holder.artist.setText(song.getSongArtist());

        // Hides the row, so both TextViews. Otherwise, sets each row visible.
        if (position == currentlyPlaying) convertView.setVisibility(View.INVISIBLE);
        else convertView.setVisibility(View.VISIBLE);

        return convertView;
    }

    private class ViewHolder {
        private TextView song;
        private TextView artist;

        private ViewHolder(View itemView) {
            song = (TextView) itemView.findViewById(R.id.song_title);
            artist = (TextView) itemView.findViewById(R.id.artist);
        }
    }

    public void setSongPosition(int position) {
        currentlyPlaying = position;
    }
}