创建一个带有可选行的 ListView/单击时更改 ListView 行的背景颜色 [英] Create a ListView with selectable rows/change background color of ListView rows when clicked
问题描述
我正在尝试创建一个带有可选项目的 ListView
.我希望能够单击 ListView
中的一个项目并让该项目在列表中更改颜色,然后继续对行中的数据执行其他操作.
I'm trying to create a ListView
with selectable items. I want to be able to click on an item in the ListView
and have the item change color in the list, and then go on and do something else with the data from the row.
我使用的是 SimpleAdapter
.
我怎么做,当我点击一行时,它变成不同的颜色,然后当我点击不同的行时,选择新行并更改为新颜色,旧行更改恢复正常?
How do I make it so that when I tap on a row, it turns a different color, and then when I tap on a different row, the new row is selected and changed to a new color, and the old row changes back to normal?
到目前为止,这是我的代码.DBTools
类包含我想在 ListView
中显示的所有数据组织和处理.getAllReceivers()
方法返回包含我所有数据的 HashMap
的 ArrayList
.
Here is my code so far. The DBTools
class is has all of the data that I want to be displayed in my ListView
organized and taken care of. The getAllReceivers()
method returns an ArrayList
of HashMap<String, String>
s that have all of my data.
MainActivity.java:
MainActivity.java:
public class MainActivity extends ListActivity {
DBTools dbTools = new DBTools(this);
ArrayList<HashMap<String, String>> receiverList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().hide();
setContentView(R.layout.activity_main);
receiverList = dbTools.getAllReceivers();
dbTools.close();
ListView listView = getListView();
if(receiverList.size() != 0) {
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,receiverList, R.layout.receiver_entry, new String[] {"receiverId","receiverName", "fullPath"}, new int[] {R.id.receiverId, R.id.receiverName, R.id.fullPath});
setListAdapter(adapter);
}
}
}
activity_main.xml:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black" >
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="My List" />
</TableRow>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:id="@android:id/list" />
</TableLayout>
receiver_entry.xml
receiver_entry.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tableRow" >
<TextView
android:id="@+id/receiverId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="@+id/receiverName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Robotronics" />
<TextView
android:id="@+id/fullPath"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123.45.678.910:8088/robtrox/find" />
</TableRow>
推荐答案
解决方案
这个问题的解决方法很简单.我们需要向我们的 ListView
添加一个 OnItemClickListener
来监听点击并做出相应的响应.
Solution
The solution to this problem is very simple. We need to add an OnItemClickListener
to our ListView
to listen for clicks and respond accordingly.
因此,在 onCreate()
方法中,一旦您确定数据集不为空,您将要覆盖 onItemClick()
方法来监听点击并改变颜色.您还需要跟踪您为后续步骤选择的项目,因此在类的顶部添加 public int selectionId = -1;
.此外,您需要让 ListAdapter
知道您通过调用 ((SimpleAdapter) getListAdapter()).notifyDataSetChanged()
.
So, in the onCreate()
method, once you've made sure that you set of data isn't empty, you're going to want to Override the onItemClick()
method to listen for the click and change the color. You're also going to want to keep track of which item you selected for the later steps, so add public int selectionId = -1;
at the top of your class. Furthermore, you'll need to let the ListAdapter
know that you changed something by calling ((SimpleAdapter) getListAdapter()).notifyDataSetChanged()
.
if(receiverList.size() != 0) {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int index, long id) {
view.setBackgroundColor(Color.RED);
TextView receiverIdTextView = (TextView) view.findViewById(R.id.receiverId);
selectionId = Integer.valueOf(receiverIdTextView.getText().toString());
((SimpleAdapter) getListAdapter()).notifyDataSetChanged();
}
});
SimpleAdapter adapter = getNewAdapter();
setListAdapter(adapter);
}
太好了!现在我们有一个工作系统可以改变你点击的行的颜色.但我们还没有完成.我们需要确保之前的选择变回正常颜色.
Great! Now we have a working system that will change the color of the row that you tap. But we're not done yet. We need to make sure that the previous selection changes back to the normal color.
为此,我们将使用覆盖 SimpleAdapter
的 getView()
方法,该方法每次 ListView
转到绘制其中显示的项目.
For this, we are going to use override the SimpleAdapter
's getView()
method, which is called everytime the ListView
goes to draw the items being displayed in it.
它实际上只显示它需要的项目——你可以看到的项目.它不会呈现屏幕上方或下方的内容.因此,如果您在 ListView
中有 200 个项目,则一次仅渲染 5 或 6 个,具体取决于您的屏幕大小和项目的大小.
It only actually displays the items it needs to - the ones that you can see. It does not render the ones above or below your screen. So if you have 200 items in a ListView
, only 5 or 6, depending on the size of your screen and the size of the items, are being rendered at a time.
要覆盖 getView()
方法,请转到初始化 adapter
的位置并将代码更改为:
To override the getView()
method, go up to where you initialize the adapter
and change the code to this:
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,receiverList, R.layout.receiver_entry, new String[] { "receiverId","receiverName", "fullPath"}, new int[] {R.id.receiverId, R.id.receiverName, R.id.fullPath}) {
@Override
public View getView (int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView receiverIdTextView = (TextView) view.findViewById(R.id.receiverId);
if(receiverIdTextView.getText().toString().equals(String.valueOf(selectionId))) {
view.setBackgroundColor(Color.RED);
} else {
view.setBackgroundColor(Color.WHITE);
}
return view;
}
};
每次绘制一行时,由于 getView()
将被调用,ListView
将检查当前的 view
具有您选择的行的 id.如果没有,它会将背景颜色更改为白色.如果是,它会将背景颜色更改为红色.
Every time one of the rows is drawn, since the getView()
will get called, the ListView
will check if the current view
has the id of row you selected. If it doesn't, it'll change the background color to white. If it does, it'll change the background color to red.
瞧!而已!现在,当您单击 ListView
中的项目时,您将背景颜色设置为红色.
And voila! That's it! Now you are setting the background color to red when you click on an item in the ListView
.
MainActivity.java:
MainActivity.java:
public class MainActivity extends ListActivity {
DBTools dbTools = new DBTools(this);
ArrayList<HashMap<String, String>> receiverList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().hide();
setContentView(R.layout.activity_main);
receiverList = dbTools.getAllReceivers();
dbTools.close();
ListView listView = getListView();
if(receiverList.size() != 0) {
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int index, long id) {
view.setBackgroundColor(Color.RED);
TextView receiverIdTextView = (TextView) view.findViewById(R.id.receiverId);
selectionId = Integer.valueOf(receiverIdTextView.getText().toString());
((SimpleAdapter) getListAdapter()).notifyDataSetChanged();
}
});
SimpleAdapter adapter = new SimpleAdapter(MainActivity.this,receiverList, R.layout.receiver_entry, new String[] { "receiverId","receiverName", "fullPath"}, new int[] {R.id.receiverId, R.id.receiverName, R.id.fullPath}) {
@Override
public View getView (int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView receiverIdTextView = (TextView) view.findViewById(R.id.receiverId);
if(receiverIdTextView.getText().toString().equals(String.valueOf(selectionId))) {
view.setBackgroundColor(Color.RED);
} else {
view.setBackgroundColor(Color.WHITE);
}
return view;
}
};
setListAdapter(adapter);
}
}
}
activity_main.xml:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black" >
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="My List" />
</TableRow>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:id="@android:id/list" />
</TableLayout>
receiver_entry.xml
receiver_entry.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tableRow" >
<TextView
android:id="@+id/receiverId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="@+id/receiverName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Robotronics" />
<TextView
android:id="@+id/fullPath"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123.45.678.910:8088/robtrox/find" />
</TableRow>
这篇关于创建一个带有可选行的 ListView/单击时更改 ListView 行的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!