gpt4 book ai didi

android - 更改可滚动 ListView 的背景颜色

转载 作者:行者123 更新时间:2023-11-30 01:43:40 35 4
gpt4 key购买 nike

我有一个可滚动的 ListView ,可以在其中选择多行。选择时,背景颜色变为绿色,如果再次选择,它又变回原来的颜色。

我设法让它工作,但我遇到的问题是当我滚动时,我没有选择的其他行被突出显示为绿色。我认为这是因为 View 正在被重用/回收,但我不确定如何解决该问题。

代码:

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

myDB = new DatabaseHandler(getActivity());
listViewSchools = (ListView)getView().findViewById(R.id.listViewFromDB);


if (myDB.getSchoolCount() != 0){
SchoolsList.addAll(myDB.getAllSchools());
isSelected = new boolean[SchoolsList.size()];
}
populateSchoolsView();


listViewSchools.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
t_school item = SchoolsList.get(position);
if (isSelected[position]) {
v.setBackgroundColor(getResources().getColor(R.color.orig));
isSelected[position] = false;
}
else if (!isSelected[position]) {
v.setBackgroundColor(getResources().getColor(R.color.green));
isSelected[position] = true;
}
schoolAdapter.notifyDataSetChanged();

}
});
}

private void populateSchoolsView() {
schoolAdapter = new SchoolListAdapter();
listViewSchools.setAdapter(schoolAdapter);
schoolAdapter.notifyDataSetChanged();

}

private class SchoolListAdapter extends ArrayAdapter<t_school> {
public SchoolListAdapter() {
super(getActivity(), R.layout.itemschool_layout, SchoolsList);
}

@Override
public View getView(int position, View view, ViewGroup parent) {
View row = view;
ViewHolder holder = new ViewHolder();
if (row == null) {
row = getActivity().getLayoutInflater().inflate(R.layout.itemschool_layout, parent, false);
holder.name = (TextView) row.findViewById(R.id.school_name);
row.setTag(holder);
}
else{
holder = (ViewHolder)row.getTag();
}

t_school currentSchool = SchoolsList.get(position);

if(currentSchool!=null){
holder.name.setText(currentSchool.getInfo());

}

return row;
}
}

public class ViewHolder
{
TextView name;

}

编辑

我尝试实现 getItemViewType() 和 getViewTypeCount(),但我不确定我是否在正确的轨道上。我按照本教程进行引用:http://android.amberfog.com/?p=296但我可以使用更多指导来了解我在这些方法中缺少的内容,以及我是否在 getView() 中正确使用它们。

编辑代码:

public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

myDB = new DatabaseHandler(getActivity());
if(myDB.getSchoolCount()==0)
Toast.makeText(getActivity().getApplicationContext(), "Please Import Data", Toast.LENGTH_LONG).show();
listViewSchools = (ListView)getView().findViewById(R.id.listViewFromDB);

registerForContextMenu(listViewSchools);


listViewSchools.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
longClickedItemIndex = position;
return false;
}
});

if (myDB.getSchoolCount() != 0){
SchoolsList.addAll(myDB.getAllSchools());
isSelected = new boolean[SchoolsList.size()];
}
populateSchoolsView();


listViewSchools.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
t_school item = SchoolsList.get(position);
if (isSelected[position]) {
v.setBackgroundColor(getResources().getColor(R.color.orig));
isSelected[position] = false;
}
else if (!isSelected[position]) {
v.setBackgroundColor(getResources().getColor(R.color.green));
isSelected[position] = true;
}
schoolAdapter.notifyDataSetChanged();

}
});

}

private void populateSchoolsView() {
schoolAdapter = new SchoolListAdapter();
listViewSchools.setAdapter(schoolAdapter);
schoolAdapter.notifyDataSetChanged();

}

private class SchoolListAdapter extends ArrayAdapter<t_school> {
public SchoolListAdapter() {
super(getActivity(), R.layout.itemschool_layout, SchoolsList);
}

@Override
public int getViewTypeCount() {
return 2;
}

@Override
public int getItemViewType(int position) {
return isSelected[position] ? 1 : 0;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
View row = view;
ViewHolder holder;
int type = getItemViewType(position);

if (row == null) {
holder = new ViewHolder();
switch ((type)) {
case 1:
row = getActivity().getLayoutInflater().inflate(R.layout.itemschool_layout, parent, false);
holder.name = (TextView) row.findViewById(R.id.school_name);
break;

case 0:
row = getActivity().getLayoutInflater().inflate(R.layout.itemschool_layout, parent, false);
holder.name = (TextView) row.findViewById(R.id.school_name);
break;

}
row.setTag(holder);
}
else{
holder = (ViewHolder)row.getTag();
}

t_school currentSchool = SchoolsList.get(position);
if(currentSchool!=null){
holder.name.setText(currentSchool.getSchool_info());

}

return row;
}
}

public class ViewHolder
{
TextView name;

}

最佳答案

你过期的是Listview的回收机制。

您更改了 Listitem 的外观,并在向下滚动时再次获得相同的 Item。

您始终可以重置颜色,但最简单的解决方案是提供 2 种项目类型。

@Override
public int getViewTypeCount() {
return 2;
}

@Override
public int getItemViewType(int position) {
return isSelected[position] ? 1 : 0;
}

这样 ListView 就可以为您提供相同类型的回收项目。

关于android - 更改可滚动 ListView 的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34052202/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com