我有一个奇怪的问题 - 我正在使用带有 ViewSwitcher 的活动,这个 ViewSwitcher 有 aListView
和 aGridView
使用相同的ArrayAdapter{SomeContent}
.
我已经制作了适配器,以便我可以将它们R.layout.value
用于膨胀的内容传递给它们——这就是我管理 ListView / GridView 一致性的方式,因为我想要这两种形式的视图有所不同。
所有这一切都有些完美,只是不是第一次。
不,我第一次在 GRID 显示模式下运行我的应用程序时,我的 gridview 尝试使用错误的 XML,并最终尝试使用(回收?) ListView 的 XML。我知道是因为 GridView 的 XML 没有对复选框的引用,而且我经常看到它们。
但是当我离开这个屏幕(甚至不会说“活动”)并回到它时,一切都完美地膨胀了。列表总是能立即工作,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;
}
至于为什么这么多 if 和检查 - 它必须能够在缺少元素的不同底层 XML 中生存(可定制性),并且不要太担心 onClick / onFocus hacks - 它们是非常需要的解决方法......