2013-06-26 39 views
-1

我有一个奇怪的问题 - 我正在使用ViewSwitcher的活动,这个ViewSwitcher有一个ListViewGridView,它们使用相同的ArrayAdapter{SomeContent}GridView的不一致性

我已经制作了适配器,以便我们可以将它们传递给R.layout.value用于膨胀 - 这是我在ListView/GridView之间管理一致性的方式,因为我希望在这两种形式中看到不同的视图。

而这一切工作起来有些完美,只是不适合第一次。

没有,第一次,我跑我的网格显示模式的应用程序,我的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; 
} 

至于为什么这么多的如果和检查 - 它能够与缺少的元素(可定制)生存不同的底层XML,并没有担心的onClick /聚焦状态太多的黑客 - 他们“再急需解决方法....

回答

0

寻找到我的意见是如何回收后,我来到了,我是运行的应用程序在不同的视图模式相比上次左结论在 - 这引起了GRID的观点是保存并在LIST中使用,反之亦然。

修改后,我就确定重新启动其先前使用相同的模式,应用程序或强制重启后新的意见是重新创建适配器和重新分配给我的看法。