2014-11-14 63 views
2

我是一個android開發noob,我想做一個listview,每行有一個VideoView。我希望VideoView一旦行焦點就開始播放,當行失焦時停止播放,類似於Instagram的做法。按照post的建議,我已在我的適配器類的getView方法內的View上設置了OnFocusChangeListener。但是,OnFocusChange方法似乎永遠不會被調用。我怎樣才能確定哪個ListView行是焦點?任何幫助解決這個問題都非常感謝。如何確定ListView行是否集中?

我的代碼

public View getView(int position, View convertView, ViewGroup parent) { 
    View vi=convertView; 
    song = new HashMap<String, String>(); 
    ViewHolder holder; 
    if(convertView==null){ 
     holder = new ViewHolder(); 
     vi = inflater.inflate(R.layout.allmedialist_row, null); 

     holder.clubname = (TextView)vi.findViewById(R.id.clubname); // title 
     holder.dates = (TextView)vi.findViewById(R.id.artist); // artist name 
     holder.vidimg=(ImageView)vi.findViewById(R.id.list_image); 
     holder.clubimg =(ImageView)vi.findViewById(R.id.clubimg); // thumb image 
     holder.playimg =(ImageView)vi.findViewById(R.id.playimg); // thumb image 
     holder.username = (TextView)vi.findViewById(R.id.username); // artist name 
     holder.video_screen = (VideoView)vi.findViewById(R.id.VideoView1); // artist name 
     holder.header = (RelativeLayout)vi.findViewById(R.id.header); 
     holder.header.setVisibility(View.GONE); 
     holder.video_screen.setTag(position); 
     holder.username.setTag(position);  

     vi.setTag(holder); 
    // vi.setFocusable(true); 
    // vi.setFocusableInTouchMode(true); 

    } else { 

     holder = (ViewHolder) vi.getTag(); 
    } 

    song = data.get(position); 

    vi.setOnFocusChangeListener(new OnFocusChangeListener() { 

     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if(hasFocus) { 

       Log.e("HAS FOCUS", "true"); 

      }else { 

       Log.e("NO FOCUS", "true"); 

      } 

     } 
    }); 
    vi.setFocusableInTouchMode(true); 


    return vi; 
} 
+0

你的意思是在日誌中只顯示NO FOCUS顯示不是HAS FOCUS? – 2014-11-14 15:39:35

+0

@MohammedAli既不顯示日誌消息。 – DollaBill 2014-11-14 15:46:06

+0

意味着OnFocusChangeListener()不被調用? – 2014-11-14 15:47:37

回答

0

看看這可以幫助你。基本上,一個帶onScroll方法的setOnScrollListener可以訪問可見的listview行,並通過getTag找到行的位置(在此之前,您需要像使用用戶名TextView一樣使用setTag)。

同樣,TheRedFox提到你需要找到平均職位。並可能從listview的可見行中找到中間行。

 mListView.setOnScrollListener(new OnScrollListener() { 

     @Override 
     public void onScrollStateChanged(AbsListView arg0, int arg1) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onScroll(AbsListView view, int firstVisibleItem, 
       int visibleItemCount, int totalItemCount) { 

      int sumOfPosition = 0; 

      // TODO Auto-generated method stub 
      for (int i = mListView.getFirstVisiblePosition(); i <= mListView 
        .getLastVisiblePosition(); i++) { 

       Log.e(TAG, "i = " + i); 
       Log.e(TAG, "mListView.getFirstVisiblePosition() = " 
         + mListView.getFirstVisiblePosition()); 

       View childView = mListView.getChildAt(i 
         - mListView.getFirstVisiblePosition()); 

       if (childView != null) { 
        Log.e(TAG, "childView != null"); 
        TextView mTextView = (TextView) childView 
          .findViewById(R.id.username); 

        Log.e(TAG, "tag position = " + mTextView.getTag()); 
        sumOfPosition = sumOfPosition 
          + Integer.parseInt(mTextView.getTag() 
            .toString()); 
       } else { 
        Log.e(TAG, "childView = null"); 
       } 
      } 
      if (visibleItemCount != 0) { 
       Log.e(TAG, "Possible focused row position = " 
         + (int) (sumOfPosition/visibleItemCount)); 
      } 
     } 
    }); 
相關問題