我昨晚花了大量时间找到关于我的问题的任何解决方案,但我没有,所以它不重复。
我有一个listView和简单的ArrayAdapter。我想每次当我点击项目时它的textView字段变为粗体,而前一个如果它存在则变为正常。这是一个非常简单的问题,但我遇到了很多麻烦。
我的设备可以同时显示13个项目。所以,如果我不滚动一切正常。但是如果我尝试滚动,我会发现一些下一个项目也变得粗体,并且点击事件无法正常工作。在调试中我看到listView只有前13个元素。我知道适配器会异常加载其他人但是如何在这些条件下解决我的问题?
这是我的代码:
ListView listLibrary;
int posPrev;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_info);
listLibrary = findViewById(R.id.listSongs);
listLibrary.setOnItemClickListener(this);
fillList();
posPrev = -1;
}
private void fillList() {
String[] arrList = {"New York", "San Francisco", "Los Angeles", "Dallas", "Houston", "Tucson", "Phoenix", "Denver", "Las Vegas", "Kansas City", "Chicago", "Detroit", "Atlanta", "Miami", "Philadelphia", "Baltimore", "Washington", "Boston", "Pittsburg", "Columbus", "Cincinnati", "Indianapolis", "St. Louis", "Seattle", "Portland", "San Jose", "San Diego", "San Antonio"};
ArrayList<String> cities = new ArrayList<String>();
for (String s : arrList)
cities.add(s);
if (listLibrary != null) {
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, cities);
listLibrary.setAdapter(adapter);
}
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (posPrev != -1) {
((TextView)listLibrary.getChildAt(posPrev)).setTypeface(null, Typeface.NORMAL);
}
((TextView)listLibrary.getChildAt(i)).setTypeface(null, Typeface.BOLD);
posPrev = i;
}
修改 所以你建议我添加自定义适配器。如果删除tvCaption.setTypeface(null,Typeface.NORMAL),我会在列表旁边随机加粗项。实际上,我理解我不知道如何解决它的问题。
ArrayList<String> cities;
Context context;
int resource;
TextView tvCaption;
int posClicked;
public CustomArrayAdapter(@NonNull Context context, int resource, @NonNull List<String> objects) {
super(context, resource, objects);
this.context = context;
this.resource = resource;
cities = (ArrayList) objects;
posClicked = -1;
}
@NonNull
@Override
public View getView(int position, @Nullable View view, @NonNull ViewGroup parent) {
if (view == null) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(resource, null);
}
tvCaption = view.findViewById(R.id.tvCaption);
tvCaption.setText(cities.get(position));
if (posClicked != -1) {
if (posClicked == position)
tvCaption.setTypeface(null, Typeface.BOLD);
} else
tvCaption.setTypeface(null, Typeface.NORMAL);
LinearLayout llCity = view.findViewById(R.id.llCity);
llCity.setTag(position);
llCity.setOnClickListener(this);
return view;
}
@Override
public void onClick(View view) {
posClicked = (int)view.getTag();
notifyDataSetChanged();
}
编辑最后:有工作代码!
在所有编辑之后我得到了这段代码:
的活动:
ListView listLibrary;
CustomArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_info);
listLibrary = findViewById(R.id.listSongs);
listLibrary.setOnItemClickListener(this);
fillList();
}
private void fillList() {
String[] arrList = {"New York", "San Francisco", "Los Angeles", "Dallas", "Houston", "Tucson", "Phoenix", "Denver", "Las Vegas", "Kansas City", "Chicago", "Detroit", "Atlanta", "Miami", "Philadelphia", "Baltimore", "Washington", "Boston", "Pittsburg", "Columbus", "Cincinnati", "Indianapolis", "St. Louis", "Seattle", "Portland", "San Jose", "San Diego", "San Antonio"};
ArrayList<String> cities = new ArrayList<String>();
for (String s : arrList)
cities.add(s);
if (listLibrary != null) {
adapter = new CustomArrayAdapter(this, R.layout.list_custom_item, cities);
listLibrary.setAdapter(adapter);
}
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
adapter.setCurClickedPos(i);
adapter.notifyDataSetChanged();
}
CustomAdapter:
ArrayList<String> cities;
Context context;
int resource;
int curClickedPos;
public CustomArrayAdapter(@NonNull Context context, int resource, @NonNull List<String> objects) {
super(context, resource, objects);
this.context = context;
this.resource = resource;
cities = (ArrayList) objects;
curClickedPos = -1;
}
@NonNull
@Override
public View getView(int position, @Nullable View view, @NonNull ViewGroup parent) {
if (view == null) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(resource, null);
}
TextView tvCaption = view.findViewById(R.id.tvCaption);
tvCaption.setText(cities.get(position));
if (curClickedPos != -1) {
if (curClickedPos == position)
tvCaption.setTypeface(null, Typeface.BOLD);
else
tvCaption.setTypeface(null, Typeface.NORMAL);
} else
tvCaption.setTypeface(null, Typeface.NORMAL);
LinearLayout llCity = view.findViewById(R.id.llCity);
llCity.setTag(position);
return view;
}
public void setCurClickedPos(int curClickedPos) {
this.curClickedPos = curClickedPos;
}
答案 0 :(得分:1)
如果你的getView()函数,你正在重用你的观点。因此,在创建视图时,您必须检查项目是否被单击。当你检查posClicked是否不等于-1时,在if块中你需要添加一个else语句,如下所示:
public View getView(int position, @Nullable View view, @NonNull ViewGroup parent) {
// your logic here
if (posClicked != -1) {
if (posClicked == position)
tvCaption.setTypeface(null, Typeface.BOLD);
else
tvCaption.setTypeface(null, Typeface.NORMAL);
} else
tvCaption.setTypeface(null, Typeface.NORMAL);
}
}
答案 1 :(得分:0)
如果我是对的,您希望在触摸/点击的项目视图中包含粗体文字。
假设 posPrev 代表被触摸项目的索引,
notifyDataSetChanged()
(点击活动后)然后在自定义适配器的getView方法下添加这段代码:
public View getView(int position, View convertView, ViewGroup parent) {
yourTextView.setTypeface(null, Typeface.NORMAL);
if(posPrev == position){
yourTextView.setTypeface(null, Typeface.BOLD);
}
}