我有一个奇怪的问题 - 我正在使用ViewSwitcher的活动,这个ViewSwitcher有ListView
和GridView
使用相同的ArrayAdapter{SomeContent}
。
我已经制作了适配器,以便我可以传递它们用来膨胀的R.layout.value
- 这就是我在ListView / GridView中管理一致性的方式,因为我希望在这两种形式中有一些不同的视图。
所有这些都有点完美,只是不是第一次。
不,我第一次在GRID显示模式下运行我的应用程序时,我的gridview尝试使用错误的XML,并最终尝试使用(回收?)ListView的XML。我知道,因为GridView的XML没有复选框的引用,我不断地看到它们。
但是当我离开这个屏幕(甚至不会说'活动')并回到它时,一切都完全膨胀了。 List总是能够解决问题,GridViews~80%只能在第一次工作。
有什么想法吗?
以下是一些可以帮助您入门的代码。 适配器:
public class AudioAdapter extends ArrayAdapter<MusicContent>
{
private Context context;
private ImageView albumArt;
private TextView songName;
private TextView artistName;
private TextView albumName;
private TextView genre;
private TextView duration;
private int viewToUse;
private CheckBox checkbox;
private OnItemClickListener clickListener;
private OnItemSelectedListener focusListener;
private List<MusicContent> content = new ArrayList<MusicContent>();
public AudioAdapter(Context context, int textViewResourceId, List<MusicContent> objects,
OnItemClickListener clickListener, OnItemSelectedListener focusListener)
{
super(context, textViewResourceId, objects);
this.context = context;
this.content = objects;
this.clickListener = clickListener;
this.focusListener = focusListener;
this.viewToUse = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
/**
* Removed following optimization on purpose due to dynamically using
* different layouts which may result in wrong view being recycled for
* use
*/
//removing it fixed nothing really
if (row == null)
{
// ROW INFLATION
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(viewToUse, parent, false);
}
// initiate helpers for onClick hack
final AdapterView fParent = (AdapterView) parent;
final View fView = row;
final int fInt = position;
final long fLong = row.getId();
// Get item
MusicContent item = getItem(position);
if (item == null)
return row;
RelativeLayout root = (RelativeLayout) row.findViewById(R.id.list_cell_layout);
// perform a series of checks to maintain customizability
albumArt = (ImageView) row.findViewById(R.id.list_cell_image);
if (albumArt != null)
{
if (item.hasAlbumArt()) {
albumArt.setImageBitmap(item.getAlbumBitmap(context));
}
else
albumArt.setImageDrawable(context.getResources().getDrawable(
R.drawable.ic_music_album));
}
LinearLayout checkLL = (LinearLayout) row.findViewById(R.id.list_cell_music_info);
if (checkLL != null)
{
// display some song info
songName = (TextView) checkLL.findViewById(R.id.list_cell_title);
if (songName != null)
{
songName.setText(item.getDisplayName());
songName.setVisibility(View.VISIBLE);
}
// attach artificial OnItemClick and OnItemSelected listeners
if (clickListener != null)
{
OnClickListener cross = new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("SHARK", "internal onClick from adapter!!" + fView);
clickListener.onItemClick(fParent, fView, fInt, fLong);
}
};
checkLL.setOnClickListener(cross);
}
if (focusListener != null)
{
OnFocusChangeListener cross = new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
focusListener.onItemSelected(fParent, fView, fInt, fLong);
}
};
checkLL.setOnFocusChangeListener(cross);
}
checkLL = (LinearLayout) row.findViewById(R.id.list_cell_artist_info);
if (checkLL != null)
{
// display artist info too
artistName = (TextView) checkLL.findViewById(R.id.list_cell_artist_name);
if (artistName != null)
artistName.setText(item.getArtist());
albumName = (TextView) checkLL.findViewById(R.id.list_cell_album);
if (albumName != null)
albumName.setText(item.getAlbum());
duration = (TextView) checkLL.findViewById(R.id.list_cell_duration);
if (duration != null)
duration.setText(item.getDurationString());
genre = (TextView) checkLL.findViewById(R.id.list_cell_genre);
if (genre != null)
genre.setText(item.getGenre());
// block focus on descendants
checkLL.setDescendantFocusability(RelativeLayout.FOCUS_BLOCK_DESCENDANTS);
// attach artificial listeners
if (clickListener != null)
{
OnClickListener cross = new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("SHARK", "internal onClick from adapter!!" + fView);
clickListener.onItemClick(fParent, fView, fInt, fLong);
}
};
checkLL.setOnClickListener(cross);
}
if (focusListener != null)
{
OnFocusChangeListener cross = new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
focusListener.onItemSelected(fParent, fView, fInt, fLong);
}
};
checkLL.setOnFocusChangeListener(cross);
}
}
// FrameLayout checkFL = (FrameLayout)
// row.findViewById(R.id.endoflineinfo);
checkLL = (LinearLayout) row.findViewById(R.id.endoflineinfo);
if (checkLL != null)
{
checkbox = (CheckBox) checkLL.findViewById(R.id.in_playlist);
if (checkbox != null)
{
checkbox.setVisibility(View.VISIBLE);
if (item.getPlaylist() != null)
checkbox.setChecked(!item.getPlaylist().isEmpty());
}
checkLL.setDescendantFocusability(RelativeLayout.FOCUS_BLOCK_DESCENDANTS);
if (clickListener != null)
{
OnClickListener cross = new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("SHARK", "internal onClick from adapter!!" + fView);
clickListener.onItemClick(fParent, fView, fInt, fLong);
}
};
checkLL.setOnClickListener(cross);
}
if (focusListener != null)
{
OnFocusChangeListener cross = new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
focusListener.onItemSelected(fParent, fView, fInt, fLong);
}
};
checkLL.setOnFocusChangeListener(cross);
}
}
}
// magic happens where we bind an OnItemClick call to OnClick
root.setDescendantFocusability(RelativeLayout.FOCUS_BLOCK_DESCENDANTS);
if (clickListener != null)
{
OnClickListener cross = new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("SHARK", "internal onClick from adapter!!");
clickListener.onItemClick(fParent, fView, fInt, fLong);
}
};
// assign this listener
root.setOnClickListener(cross);
}
if (focusListener != null)
{
OnFocusChangeListener cross = new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
focusListener.onItemSelected(fParent, fView, fInt, fLong);
}
};
root.setOnFocusChangeListener(cross);
}
return row;
}
至于为什么这么多的ifs和支票 - 它必须能够存活不同的基础XML,缺少元素(可定制性),并且不要太担心onClick / onFocus黑客 - 它们是非常需要的解决方法。 ..
答案 0 :(得分:0)
在研究了我的观点如何被回收后,我得出的结论是我在不同的视图模式下运行应用程序而不是最后一次 - 这导致GRID视图被保存并在LIST中使用,或者反之亦然。
在修改之后,我确保以与之前使用的模式相同的模式重新启动应用程序,或者在重新启动时强制新视图重新创建适配器并将其重新分配给我的视图。