代码使用baseadapter使用项目填充listview。点击该项目后,它会更改背景颜色并转到下一个活动。现在我想在第一次点击时更改背景颜色,它应保持选中状态。然后在下一次点击它应该转到下一个活动。是否可以这样做。
活动
nameList = (ListView) findViewById(R.id.list_names);
nameList.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
nameList.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long viewId)
{
TextView PCNtv = (TextView) view.findViewById(R.id.value_Name);
String P_Name = PCNtv.getText().toString();
Intent intents = new Intent(getApplicationContext(), Activity2.class);
intents.putExtra("P_Name", P_Name);
view.setSelected(true);
startActivity(intents);
}
});
在drawable中
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/blue_color"/>
<item android:state_pressed="true" android:drawable="@drawable/white_color"/>
<item android:state_focused="true" android:drawable="@drawable/red_color"/>
</selector>
答案 0 :(得分:0)
使用views selected属性检测第二次单击。首先单击将选择视图,第二个将打开活动。
nameList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long viewId)
{
TextView PCNtv = (TextView) view.findViewById(R.id.value_Name);
String P_Name = PCNtv.getText().toString();
if ( view.isSelected() ) {
Intent intents = new Intent(getApplicationContext(), Activity2.class);
intents.putExtra("P_Name", P_Name);
startActivity(intents);
}
view.setSelected(true);
});
}
<强>更新强>
当用户再次按下视图时,选择将丢失,因此上面的代码不起作用。这是一个涉及跟踪当前所选位置的适配器的解决方案。
点击事件:
nameList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Check to see which item in the adapter is already
// selected, if the same has been selected twice, start new activity
if ( i == ((TestAdapter)adapter).getSelectedIndex()) {
// Start your new activity here.
Log.d("TAG", "second push");
}
((TestAdapter)adapter).setSelectedIndex(i);
}
});
您没有显示适配器的代码,因此这是一个添加了所选索引的简单代码:
public class TestAdapter extends ArrayAdapter<String> {
private final Activity context;
private int mSelection = -1;
public TestAdapter(Activity context, String[] objects) {
super(context, R.layout.text_layout, objects);
this.context = context;
}
// Call this when an item is clicked, this sets the
// internal index for the selected item and notifies the view
// that the data has changes, this will force the view to draw
// allowing it to correctly highlight the selected item
public void setSelectedIndex(int index) {
mSelection = index;
notifyDataSetChanged();
}
// This is needed for the click listener to check to see which
// item is already selected
public int getSelectedIndex() {
return mSelection;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.text_layout, null);
TextView tv = (TextView)rowView.findViewById(R.id.text3);
tv.setText(getItem(position));
// Adds or removes the selection on the view so
// it will display as selected
if ( mSelection == position ) {
tv.setSelected(true);
} else {
tv.setSelected(false);
}
return rowView;
}
}
更新:添加了内嵌评论