我正在尝试在我的片段首次启动时手动调用onItemClick,但它的行为方式并不像预期的那样。它在onCreateView()方法中调用如下:
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,但实际点击发生时却没有。为什么会这样?
答案 0 :(得分:1)
您可以跟踪当前所选元素的位置:
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文件夹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"
答案 1 :(得分:1)
尝试更换,
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之前的这两行代码会产生选择列表项的错觉。他们还确保屏幕上显示相应的项目。
答案 2 :(得分:0)
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();
此代码对我有用