【问题标题】:ListView item stays highlightedListView 项目保持突出显示
【发布时间】:2014-01-28 06:48:45
【问题描述】:

我正在开发一个使用两个片段的应用程序。其中一个片段使用 ListView 来显示项目,当单击时,第二个片段会自行更新(它是一个字典应用程序)。 在 ListView 中选择一个项目后,我希望它突出显示。我通过在 XML 文件中为 ListView 项使用以下属性来实现这一点。

android:choiceMode="singleChoice"
android:listSelector="@android:color/darker_gray"

就项目的选择而言,这可以正常工作。但问题是当我滚动列表时,即使所选项目不在列表中,选择有时也会停留在列表的底部或顶部(视情况而定)。这是解释我的问题的屏幕截图。

第一张图片显示没有选择;在第二张图片中,我选择了 101,因此更新了第二个片段。在第三张图片中,即使没有显示 101,ListView 的一部分也被突出显示。

有什么问题?

谢谢。

编辑 这是 OnItemClick() 方法。

public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {

    String selectedWord = words.get(arg2); //words in an ArrayList which holds all the words displayed in this fragment.

    Meaning meaningFragment = (Meaning) getSupportFragmentManager().
                                         findFragmentById(R.id.fragmentMeaning);
    meaningFragment.searchMeaning(selectedWord); //this method updates the other fragment.
}

编辑:

这个问题还没有解决。如果有人想看看,我已将项目上传到github。

【问题讨论】:

  • 能否提供onItemClick()方法。
  • 您是否尝试过查看 Eclipse 提供的示例。有一个左窗格更新右窗格详细信息的主详细信息示例,它工作正常。
  • 向我们展示您的 getView 方法
  • @LenaBru:目前我没有使用自定义适配器,我正在为 ListView 使用默认的 ArrayAdapter。
  • @HpTerm:你能告诉我你说的是哪个 API 的示例吗?我将研究示例项目。

标签: android android-listview


【解决方案1】:

在 Eclipse 中,您可以创建一个主从流,这是您尝试做的一个示例。

为此创建一个“新的 Android 应用程序”,在第三个或第四个称为“创建活动”的屏幕中,为您提供三种可能性,选择称为“主/详细信息”的一种。

这并没有为您的问题提供答案,而只是将您指向一个工作示例,您可以从中进行详细说明。

现在为了回答你的问题,我查看了你的代码并发现了错误。

这是onCreateView() 的工作代码。我注释掉了错误的代码行。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View v = inflater.inflate(R.layout.fragment_one, container);

    listView = (ListView) v.findViewById(R.id.listView);
    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    //listView.setAdapter(new ArrayAdapter<String>(getActivity(),
    //          android.R.layout.simple_dropdown_item_1line, array));
    //listView.setSelector(android.R.color.holo_blue_dark);

    listView.setAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_activated_1, array));

    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            listView.setItemChecked(position, true);
            FragmentTwo frag = (FragmentTwo) getActivity()
                                             .getFragmentManager()
                                             .findFragmentById(R.id.fragmentTwo);
            frag.updateTextView(array[position]);
        }
    });

    return v;
}

在您的代码中,以下 2 行是错误的

listView.setAdapter(new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_dropdown_item_1line, array));
listView.setSelector(android.R.color.holo_blue_dark);

您在这里所做的是设置simple_dropdown_item_1line,这不是您想要的,您手动设置selector。

您必须使用simple_list_item_activated_1,而不是手动设置选择器。

listView.setAdapter(new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_activated_1, array));
//listView.setSelector(android.R.color.holo_blue_dark);

如果您想要自定义选择器颜色,您可以通过复制 simple_list_item_activated_1 并根据需要对其进行编辑并调用编辑后的布局来创建自定义选择器。 不过我建议使用默认颜色,以便在 Android 决定更改其配色方案的那一天,您将适应新的配色方案。

总之,要让ListView.CHOICE_MODE_SINGLE 工作,您必须使用simple_list_item_activated_1 而不是手动设置selector。

【讨论】:

    猜你喜欢
    • 2013-04-17
    • 2016-01-09
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    相关资源
    最近更新 更多