列表视图 performItemClick 不起作用

List view performItemClick doesn't work

我尝试在片段首次启动时手动调用 onItemClick,但它的行为方式与在 onCreateView() 方法中调用 expected.It 的方式不同,如下所示:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        handler=new DBHandler(getActivity().getApplicationContext(),null,null,1);
        JustaMap=new HashMap<String,List<MediaMetadata>>();
        app = new Common();
        rootView = inflater.inflate(R.layout.fragment_create_playlist, container, false);
        musicProvider = new MusicProvider();
        SongTitles = new ArrayList<String>();
        PlayList = new ArrayList<MediaMetadata>();
        musicProvider.buildSongFromDevice();
        SongList = (List<MediaMetadata>) musicProvider.getAllMusics();
        Iterator<MediaMetadata> iterator = SongList.iterator();
        while (iterator.hasNext())
            SongTitles.add(iterator.next().getDescription().getTitle().toString());

        listView = (ListView) rootView.findViewById(R.id.listView);
        editText = (EditText) rootView.findViewById(R.id.PlaylistName);
        button = (Button) rootView.findViewById(R.id.submit);
        adapter = new CustomAdapter(getActivity().getApplicationContext(), SongTitles);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

                MediaMetadata SongSelected = SongList.get(position);
                view.setSelected(true);
                if (view.getBackground() == null) {
                    view.setBackgroundResource(R.color.light_grey);
                    if (!Arrays.asList(PlayList).contains(SongSelected))
                        PlayList.add(SongSelected);
                } else {
                    view.setBackgroundResource(0);
                    PlayList.remove(SongSelected);
                }

            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // createPlaylist();
            }
        });


        listView.performItemClick(
                listView.getAdapter().getView(2,null,null),2,listView.getAdapter().getItemId(2));

        return rootView;
    }

基本上,当项目被点击时,它的背景应该变成灰色,但当片段启动时它仍然是白色的。我已经使用调试器单步执行,并且 onItemClick() 仍然被调用,但背景没有改变。但是,如果我对列表项执行实际单击,它实际上会起作用并且颜色会按预期变化。似乎视图的背景在调用 performItemClick() 之后被设置回 null,但在实际点击发生时却没有。为什么会这样?

您可以跟踪当前所选元素的位置:

OnItemClickListener listViewOnItemClick = new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
        mSelectedItem = position;
        mAdapter.notifyDataSetChanged();
    }
};

并覆盖适配器的 getView 方法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    final View view = View.inflate(context, R.layout.item_list, null);

    if (position == mSelectedItem) {
        // set your color
    }

    return view;
}

首先,您可以在可绘制文件夹 drawable/list_item_selector.xml

中创建选择器 xml 文件,如下所示
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_activated="true">
      <shape android:shape="rectangle">
        <solid android:color="#333333" />
        <padding android:left="5dp" android:right="5dp" />
      </shape></item>
    <item><shape android:shape="rectangle">
            <solid android:color="#222222" />
        </shape></item>

</selector>

然后在您的列表视图中将背景指定为

android:background="@drawable/list_item_selector"

尝试替换,

listView.performItemClick(
                listView.getAdapter().getView(2,null,null),2,listView.getAdapter().getItemId(2));

listView.requestFocusFromTouch();
listView.setSelection(position);

 listView.performItemClick(
                listView.getAdapter().getView(2,null,null),2,2);

此外,在您的 performItemClick 之前的这两行代码会产生选中列表项的错觉。他们还确保适当的项目出现在屏幕上。

listView.getAdapter().getView(your position, null, null).performClick();

Toast.makeText(this, ""+ listView.performItemClick(
                                         listView.getAdapter().getView(your position, null, null), your position, your position) , Toast.LENGTH_SHORT).show(); 

这段代码对我有用