2013-06-26 41 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中使用,反之亦然。

修改後,我就確定重新啓動其先前使用相同的模式,應用程序或強制重啓後新的意見是重新創建適配器和重新分配給我的看法。