0

我想知道如何更新視圖/ UI(比方說,做一個TextView無形的)一個ListView的單列內該行點擊因爲每隔時行'的意見也會受到影響。一個很好的例子是打在谷歌Play音樂歌曲,並且均衡動畫將相應地顯示出來。這裏是我的代碼片段:點擊時更新一個ListView的一行中的觀點

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; 
} 

回答

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; 
    } 
} 
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); 

} 
}); 
+0

謝謝,但其它視圖仍將消失。 – DaveNOTDavid

0

添加下面的代碼在你的適配器類

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

修改您的viewholder類

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); 
} 

而最後落實在你的Activity類

class MainActivity extends AppCompatActivity implements YourAdapter.OnItemClickListener{ 
@Override 
public void onClick(View view) { 
    TextView titleText = (TextView) view.findViewById(R.id.song_title); 
     titleText.setVisibility(View.INVISIBLE); 
} 

}

接口
+0

不幸的是,這不符合我重新編碼工作,或者至少 - 檢查出我的更新文章時,你可以! – DaveNOTDavid

+0

更新的代碼似乎正確,嘗試登錄該值INT adapterPosition = getAdapterPosition();在viewholder類 –

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); 

       } 

      } 
     } 
+0

雖然這個作品的onclick,我用簡單的解決方案提出瞭如下所示標示爲最佳答案! – DaveNOTDavid

相關問題