Android ListView DPAD导航:为什么我需要两次单击向上按钮才能向上滚动?

时间:2017-01-12 20:12:01

标签: android listview

先谢谢你的帮助!

我使用遥控器导航一个简单的listView,使用DPAD_UP上升和放大DPAD_DOWN下去列表。除了你在下面看到的香草味之外,我没有做任何特别的事情。每次我单击向下按钮时,它都会向下滚动到下一行。但是,当反方向(即点击向上按钮)时,我必须单击它两次以向上滚动一行,除了选择的少数几行始终有效。

每次单击向上按钮时,都会触发OnItemSelectedListener。例如,我在位置10,我按下向上按钮,onItemSelected(pos = 10)被触发。我再次按下向上按钮,现在列表视图向上滚动并触发onItemSelected(pos = 9)。我再说一遍,listView& onItemSelected(pos = 9)等。

使用Jelly Bean,API Level 17,这不是触摸屏。

片段代码:

import android.support.v4.app.Fragment;
import android.widget.ListView;
...
public class MyFragment extends Fragment {
  ListView mListView = null;
  ChListAdapter mChListAdapter = null;
  ...
  @Override onCreateView(...) {
    mListView = (ListView) parentView.findViewById(R.id.chListView);
    mChListAdapter = new ChListAdapter()
    mListView.setAdapter(mChListAdapter);
    mListView.setOnItemSelectedListener(new OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        android.util.Log.e(TAG,"POS: " + position);
      }
    ...
  }
  ...
  @Override onResume(...) {
    mListView.requestFocus();
  }
  ...
  public void setSelectedCh(int chId) { // request from Activity
    mListView.setSelection(<private method to get pos from chId>);
  }
  ...
  class ChListAdapter extends ArrayAdapter<Stuff> {
    public ChListAdapter() {
      super(getActivity(), R.layout.row_ch, R.id.row_ch_name,<list in here>);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      View row = super.getView(position, convertView, parent);

      TextView txt1 = (TextView) row.findViewById(R.id.row_ch_name);
      txt1.setText(...);

      TextView txt2 = (TextView) row.findViewById(R.id.row_ch_number);
      txt2.setText(...);

      ImageView imgView = (ImageView) row.findViewById(R.id.row_ch_image);
      imgView.setImageDrawable(logoBitmapDrawable);

      return row;
    }
  }
  ...
}

FRAGMENT XML:

<LinearLayout...
  <LinearLayout...
    ...
    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_margin="10dp"
      android:layout_weight="100"
      android:baselineAligned="false"
      android:orientation="vertical" >
        <TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:paddingBottom="3dp"
          android:paddingLeft="10dp"
          android:paddingTop="3dp"
          android:text="@string/asdf" />
        <ListView
          android:id="@+id/chListView"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:divider="@android:color/darker_gray"
          android:dividerHeight="1dp" />
    </LinearLayout>
    ...
  </LinearLayout>
</LinearLayout>

LISTVIEW ROW XML(/layout/row_ch.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <ImageView
        android:id="@+id/row_ch_image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:src="@drawable/aaa" />
    <TextView
        android:id="@+id/row_ch_number"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="bbb" />
    <TextView
        android:id="@+id/row_ch_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="2dp"
        android:text="ccc"  />
</LinearLayout>

0 个答案:

没有答案