在 ListView 的选定项目中显示复选标记
Show check mark in selected item of ListView
我正在尝试创建一个语言选择屏幕。我有一个 ListView,我正在使用自定义适配器 class。选择一个项目后,我想在该项目中显示一个刻度线。我的 ListView 有一个 TextView` 和一个 ImageView。如何实现?
这是我已经尝试过的方法。
感谢任何回答和建议,提前致谢
public class customAdapter extends BaseAdapter {
String[] languages ;
Context mContext;
boolean isChecked = false;
public customAdapter(String[] languages, Context
mContext){
this.languages = languages;
this.mContext = mContext;
}
@Override
public int getCount() {
return languages.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View rowView=
inflater.inflate(
R.layout.content_lang_list,parent,false);
TextView lang_text = (TextView)
rowView.findViewById(R.id.text_select_lang);
final ImageView image_tick = (ImageView)
rowView.findViewById(R.id.image_tick);
lang_text.setText(languages[position]);
image_tick.setImageResource(R.drawable.ic_checked);
return rowView;
}
public class ChangeLang extends AppCompatActivity {
private ListView languageList;
String[] languages = {"English","Portuguese","Arabic"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cahnge_lang);
languageList = (ListView)
findViewById(R.id.language_list);
final customAdapter mAdapter = new customAdapter(
languages,this);
languageList.setAdapter(mAdapter);
languageList.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
String selected = ((TextView)
view.findViewById(R.id.text_select_lang))
.getText().toString();
Object itemObject =parent.getAdapter()
.getItem(position);
LanguageSelect selectedLang =
(LanguageSelect)itemObject;
}
});
}
}
在被点击的图片项中使用setVisibility(View.VISIBLE);
!即:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView= inflater.inflate(R.layout.content_lang_list,parent,false);
TextView lang_text = (TextView) rowView.findViewById(R.id.text_select_lang);
final ImageView image_tick = (ImageView) rowView.findViewById(R.id.image_tick);
lang_text.setText(languages[position]);
image_tick.setImageResource(R.drawable.ic_checked);
image_tick.setVisibility(View.VISIBLE); //Show image
return rowView;
}
作为一种语言 selection screen,我假设您一次只需要 select 一个项目。您已经添加了一个布尔成员 isChecked;
但似乎没有在任何地方使用。
第 1 步:
这里你需要的是一个 int 成员——比如说,selectedItemPosition
,它的初始值设置为 -1。
换句话说,替换
boolean isChecked = false;
和
int selectedItemPosition = -1;
第 2 步:
将此添加到 onItemClick()
selectedItemPosition = ((selectedItemPosition!=position)?position:-1);
notifyDataSetChanged();
第 3 步:
将此添加到您的 getView()
image_tick.setVisibility((selectedItemPosition=position)?View.VISIBLE:View.GONE);
小提示:
- 同时使用
ListViews
,follow view holder pattern 优化性能。
- 使用
convertView
s 回收视图。
- 您可以在 XML 中将
image_tick.setImageResource(R.drawable.ic_checked);
替换为 android:src="R.drawable.ic_checked"
。
我正在尝试创建一个语言选择屏幕。我有一个 ListView,我正在使用自定义适配器 class。选择一个项目后,我想在该项目中显示一个刻度线。我的 ListView 有一个 TextView` 和一个 ImageView。如何实现?
这是我已经尝试过的方法。
感谢任何回答和建议,提前致谢
public class customAdapter extends BaseAdapter {
String[] languages ;
Context mContext;
boolean isChecked = false;
public customAdapter(String[] languages, Context
mContext){
this.languages = languages;
this.mContext = mContext;
}
@Override
public int getCount() {
return languages.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View rowView=
inflater.inflate(
R.layout.content_lang_list,parent,false);
TextView lang_text = (TextView)
rowView.findViewById(R.id.text_select_lang);
final ImageView image_tick = (ImageView)
rowView.findViewById(R.id.image_tick);
lang_text.setText(languages[position]);
image_tick.setImageResource(R.drawable.ic_checked);
return rowView;
}
public class ChangeLang extends AppCompatActivity {
private ListView languageList;
String[] languages = {"English","Portuguese","Arabic"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cahnge_lang);
languageList = (ListView)
findViewById(R.id.language_list);
final customAdapter mAdapter = new customAdapter(
languages,this);
languageList.setAdapter(mAdapter);
languageList.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
String selected = ((TextView)
view.findViewById(R.id.text_select_lang))
.getText().toString();
Object itemObject =parent.getAdapter()
.getItem(position);
LanguageSelect selectedLang =
(LanguageSelect)itemObject;
}
});
}
}
在被点击的图片项中使用setVisibility(View.VISIBLE);
!即:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView= inflater.inflate(R.layout.content_lang_list,parent,false);
TextView lang_text = (TextView) rowView.findViewById(R.id.text_select_lang);
final ImageView image_tick = (ImageView) rowView.findViewById(R.id.image_tick);
lang_text.setText(languages[position]);
image_tick.setImageResource(R.drawable.ic_checked);
image_tick.setVisibility(View.VISIBLE); //Show image
return rowView;
}
作为一种语言 selection screen,我假设您一次只需要 select 一个项目。您已经添加了一个布尔成员 isChecked;
但似乎没有在任何地方使用。
第 1 步:
这里你需要的是一个 int 成员——比如说,selectedItemPosition
,它的初始值设置为 -1。
换句话说,替换
boolean isChecked = false;
和
int selectedItemPosition = -1;
第 2 步:
将此添加到 onItemClick()
selectedItemPosition = ((selectedItemPosition!=position)?position:-1);
notifyDataSetChanged();
第 3 步:
将此添加到您的 getView()
image_tick.setVisibility((selectedItemPosition=position)?View.VISIBLE:View.GONE);
小提示:
- 同时使用
ListViews
,follow view holder pattern 优化性能。 - 使用
convertView
s 回收视图。 - 您可以在 XML 中将
image_tick.setImageResource(R.drawable.ic_checked);
替换为android:src="R.drawable.ic_checked"
。