android - Android LayoutInflater 不在单独的类中工作,也没有发生错误
问题描述
我有一个单独的 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 );
但还是一样的结果。我怎样才能正确地从这样的类中扩展布局?
解决方案
我认为你的方式是错误的,你不需要 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);
}
推荐阅读
- r - 从指示范围的列中替换按年份命名的有序列中的值
- docker - Kubectl 无法从私有 gcr.io 存储库中提取 docker 映像
- css - CSS伪元素如何改变颜色
- python - 授予 AD 组权限以在 Python 中管理另一个 AD 组
- pdf - 转换为 PDF 时,如何在 Pandoc 中的表格后继续列表缩进?
- ios - 尝试为多个视图控制器设置 Firebase 身份验证时出错
- lisp - 如何并排打印 LISP 列表?
- php - 在 php $_POST 中调用具有唯一名称的文本框
- vue.js - .env 文件加载缓存值
- javascript - jQuery 如果发布日期早于今天,则将其移动到另一个 div