如何确定ListView行是否聚焦?

时间:2014-11-14 15:06:08

标签: android listview user-interface focus adapter

我是Android开发的菜鸟,我正在尝试制作一个每行都有VideoView的列表视图。我希望VideoView在行聚焦后开始播放,并在行失焦时停止播放,类似于instagram的行为。我在我的适配器类中的getView方法中的View上设置了OnFocusChangeListener,如post所示。但是,似乎永远不会调用OnFocusChange方法。如何确定哪个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;
}  

1 个答案:

答案 0 :(得分: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));
            }
        }
    });