Android - 持续点击 ListView(和内存)中突出显示的项目
Android - Keeping clicked on items highlighted in ListView (and in memory)
我有一个应用程序,通过它我可以访问 RESt web 服务并在列表视图 (ListFrragment) 中显示请求。它本质上是一个文章列表。我想要实现的是当用户单击列表视图中的项目(在 ViewPager 中展开文章)时保持突出显示(表示该文章已被阅读)。下次用户重新加载此列表视图时,所有已阅读的项目仍会突出显示。
我已经为每篇文章创建了一个布尔变量 "read"。当用户单击此项时,"read" 变量被分配为 TRUE。然后在自定义适配器(扩展 arrayAdpater)中,我在 getView 中为 "read" 值为 TRUE 的文章分配背景颜色(在本例中为绿色)。这确实有效,但我发现当我开始上下滚动列表视图而不实际单击它时,列表视图中的其他项目也随机变成绿色。我真的被困住了,因为我不明白发生了什么。请问有人可以指教吗?
public void onListItemClick(ListView l, View v, int postion, long id) {
Article a = ((toReadListAdapter) getListAdapter()).getItem(postion);
// set read attribute to TRUE
a.setRead(true);
saveToReadList(toReadList);
// Open via ViewPager
Intent i = new Intent(getActivity(), ReadingList_PagerActivity.class);
startActivity(i);
客户适配器:
// Defining custom adapter
private class toReadListAdapter extends ArrayAdapter<Article> {
public toReadListAdapter(ArrayList<Article> listToRead) {
super(getActivity(), 0, listToRead);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.new_articlelistfragment, null);
}
Article en = getItem(position);
Log.d(TAG, "Showing articles: " + position + "/ pmid: " + en.getId());
Log.e(TAG, "isRead value: " + en.isRead());
if(en.isRead() == true){
convertView.setBackgroundColor(Color.parseColor("#C8E6C9"));
}
......
return convertView;
getView 内部
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.new_articlelistfragment, null);
}
Article en = getItem(position);
if(en.isRead() == true){
convertView.setBackgroundColor(Color.parseColor("#C8E6C9"));
}
else{
// put the default color
convertView.setBackgroundColor( );
}
......
return convertView;
}
您没有应用默认颜色。在获取视图方法中添加else语句
我有一个应用程序,通过它我可以访问 RESt web 服务并在列表视图 (ListFrragment) 中显示请求。它本质上是一个文章列表。我想要实现的是当用户单击列表视图中的项目(在 ViewPager 中展开文章)时保持突出显示(表示该文章已被阅读)。下次用户重新加载此列表视图时,所有已阅读的项目仍会突出显示。
我已经为每篇文章创建了一个布尔变量 "read"。当用户单击此项时,"read" 变量被分配为 TRUE。然后在自定义适配器(扩展 arrayAdpater)中,我在 getView 中为 "read" 值为 TRUE 的文章分配背景颜色(在本例中为绿色)。这确实有效,但我发现当我开始上下滚动列表视图而不实际单击它时,列表视图中的其他项目也随机变成绿色。我真的被困住了,因为我不明白发生了什么。请问有人可以指教吗?
public void onListItemClick(ListView l, View v, int postion, long id) {
Article a = ((toReadListAdapter) getListAdapter()).getItem(postion);
// set read attribute to TRUE
a.setRead(true);
saveToReadList(toReadList);
// Open via ViewPager
Intent i = new Intent(getActivity(), ReadingList_PagerActivity.class);
startActivity(i);
客户适配器:
// Defining custom adapter
private class toReadListAdapter extends ArrayAdapter<Article> {
public toReadListAdapter(ArrayList<Article> listToRead) {
super(getActivity(), 0, listToRead);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.new_articlelistfragment, null);
}
Article en = getItem(position);
Log.d(TAG, "Showing articles: " + position + "/ pmid: " + en.getId());
Log.e(TAG, "isRead value: " + en.isRead());
if(en.isRead() == true){
convertView.setBackgroundColor(Color.parseColor("#C8E6C9"));
}
......
return convertView;
getView 内部
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.new_articlelistfragment, null);
}
Article en = getItem(position);
if(en.isRead() == true){
convertView.setBackgroundColor(Color.parseColor("#C8E6C9"));
}
else{
// put the default color
convertView.setBackgroundColor( );
}
......
return convertView;
}
您没有应用默认颜色。在获取视图方法中添加else语句