ListView项目保持突出显示

时间:2014-01-28 06:48:46

标签: android android-listview

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

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

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

enter image description here

enter image description here

enter image description here

第一张照片显示没有选择;在第二张图片中,我选择了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上传了项目,如果有人想看看的话。

1 个答案:

答案 0 :(得分:1)

在Eclipse中,您可以创建一个Master-Detail Flow,它是您尝试做的一个示例。

为此创建一个“新的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

相关问题