Android LayoutInflater无法在单独的类中使用,也没有发生错误

时间:2019-04-08 08:34:47

标签: android onclicklistener layout-inflater

我为onClickListener提供了一个单独的类,并且我试图通过单击此处的活动代码来扩大视图以更改文本

    playNext.setOnClickListener(new ExternalOnClickListener(this));
    playPrevious.setOnClickListener(new ExternalOnClickListener(this));

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NowPlaying"
android:background="#000">

<ImageView
    android:id="@+id/album_cover"
    style="@style/nowPlaying_background"/>

<include layout="@layout/top_buttons"/>

<TextView
    android:id="@+id/album"
    style="@style/nowPlaying_albumName"
    android:text="album name"/>

<TextView
    android:id="@+id/song"
    android:text="song name"
    style="@style/nowPlaying_songName"/>

<LinearLayout
    android:id="@+id/controllers"
    style="@style/controllers_linearLayout">

    <View
        style="@style/nowPlaying_controllers_view_divider"/>

    <Button
        android:id="@+id/skip_previous"
        style="@style/previous_song_btn"
        android:text="" />

    <Button
        android:id="@+id/play_pause"
        style="@style/play_pause"/>


    <Button
        android:id="@+id/skip_next"
        style="@style/previous_song_btn.next_song_btn"
        android:text=""/>

  </LinearLayout>

</RelativeLayout>

班级

public class ExternalOnClickListener implements View.OnClickListener {

private Context context;
private SharedPreferenceConfig preferenceConfig;
private int songIndex = 0;
private int nextSongIndex = 0;
private int previousSongIndex = 0;
private String songName, curSong;

public ExternalOnClickListener(Context context) {
    this.context=context;
    preferenceConfig = new SharedPreferenceConfig(context);

}

@Override public void onClick(View v) {
    switch (v.getId()){

        case R.id.skip_next:
            int songIndex = 0;
            int nextSongIndex = 0;
            String songName = preferenceConfig.readSongName();
            for(Songs s : MainActivity.songs) {
                boolean resultOfComparison= songName.equals(s.getSong());
                if(resultOfComparison){
                    int indexPlusOne = songIndex+1;
                    if((MainActivity.songs.size()) == indexPlusOne){
                        songIndex = 0;
                        nextSongIndex =0;
                    }
                    else{
                        nextSongIndex = songIndex + 1;
                    }

                    String nextSongName = MainActivity.songs.get(nextSongIndex).getSong();
                    String nextAlbumName = MainActivity.songs.get(nextSongIndex).getAlbum();
                    int nextAlbumCover = MainActivity.songs.get(nextSongIndex).getAlbumCover();

                    LayoutInflater inflater = LayoutInflater.from(context);
                    View vi = inflater.inflate( R.layout.activity_now_playing, null );

                    TextView songTextView = (TextView) vi.findViewById(R.id.song);
                    TextView albumTextView = (TextView) vi.findViewById(R.id.album);
                    ImageView albumCover = (ImageView) vi.findViewById(R.id.album_cover);

                    songTextView.setText(nextSongName);
                    albumTextView.setText(nextAlbumName);
                    albumCover.setImageResource(nextAlbumCover);

                    preferenceConfig.writeSongName(nextSongName);
                    Toast.makeText(context, nextSongName, Toast.LENGTH_SHORT).show();
                    break;
                }
                songIndex++;
            }
            break;
    }
  }
}

祝酒词正确显示,我得到正确的歌曲名称,但文本视图和图像没有变化,也没有出现任何错误。我试图用此行替换充气机代码

LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

但结果仍然相同。我该如何从这样的类正确地增加布局?

2 个答案:

答案 0 :(得分:1)

我认为您的方法是错误的,您不需要infalter,可以在其中定义图片和文字

playNext.setOnClickListener(new ExternalOnClickListener(this)); 然后在您的自定义类中调用,然后将您的元素传递给构造函数,现在您可以在自定义类中获得视图,而无需在单击时定义主题,只需像

那样执行逻辑即可
           songTextView.setText(nextSongName);
            albumTextView.setText(nextAlbumName);
            albumCover.setImageResource(nextAlbumCover);

更新:

您必须像在新的构造函数中那样传递元素:

private TextView songTextView;
public ExternalOnClickListener(Context context,Textview songTextView) {
    this.songTextView=songTextview;
    this.context=context;
    preferenceConfig = new SharedPreferenceConfig(context);

}

答案 1 :(得分:0)

也许这是解决方案。 (因为问题被夸大了,但没有设置为活动的contentView)

  1. 作为活动而不是上下文传递。

  2. 使用它代替充气:

    activity.setContentView(R.layout.activity_now_playing)

  3. vi.findViewById需要更改为

    activity.findViewById(...)