有什么方法可以使 ListView 中的特定项目可点击?
Is there any way through which I can make a specific item clickable in a ListView?
我有一个场景,我在 ListView 中显示选项。但是,对于特殊情况,我只需要 ListView 的最后一项即可单击。有什么办法可以实现吗??
适配器代码:
public class OptionAdapter extends ArrayAdapter<OptionsPojo> {
private int mColorResourceId;
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
OptionsPojo optionsPojo = getItem(position);
View listItemView = convertView;
if (listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.option_card, parent,false);
}
View textImgContainer = listItemView.findViewById(R.id.ll_img_text);
View textContainer = listItemView.findViewById(R.id.rl_text);
ImageView icon = listItemView.findViewById(R.id.image_view);
TextView imgOptnText = listItemView.findViewById(R.id.option_text_view);
TextView optionText = listItemView.findViewById(R.id.tv_text);
int color = getContext().getResources().getColor(mColorResourceId);
if(optionsPojo.hasImage())
{
icon.setImageResource(optionsPojo.getmImageResourceId());
textImgContainer.setBackgroundColor(color);
icon.setVisibility(View.VISIBLE);
//setting option with image
if (optionsPojo.getOption().startsWith("<p>"))
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
imgOptnText.setText(Html.fromHtml(optionsPojo.getOption(), Html.FROM_HTML_MODE_COMPACT));
else
imgOptnText.setText(Html.fromHtml(optionsPojo.getOption()));
}else
imgOptnText.setText(optionsPojo.getOption());
}else{
textImgContainer.setVisibility(View.GONE);
textContainer.setVisibility(View.VISIBLE);
textContainer.setBackgroundColor(color);
//setting option without image
if (optionsPojo.getOption().startsWith("<p>"))
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
optionText.setText(Html.fromHtml(optionsPojo.getOption(), Html.FROM_HTML_MODE_COMPACT));
else
optionText.setText(Html.fromHtml(optionsPojo.getOption()));
}else
optionText.setText(optionsPojo.getOption());
}
return listItemView;
}
public OptionAdapter(@NonNull Context context, @NonNull ArrayList<OptionsPojo> options, int colorResourceId) {
super(context,0, options);
mColorResourceId = colorResourceId;
}
Activity 使用列表视图的代码:
OptionAdapter Adapter = new OptionAdapter(this, optionList, R.color.background);
listview.setAdapter(Adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
showDialog(ChosenTestQuestion.this,optionList.get(i).getAnswer());
}
});
}
请检查此代码
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
if(i == (list.size()-1)){
showDialog(ChosenTestQuestion.this,optionList.get(i).getAnswer());
}
}
});
您在 onItemClick 方法中有位置变量。您只需要检查这是否是您的最后一个元素。
我有一个场景,我在 ListView 中显示选项。但是,对于特殊情况,我只需要 ListView 的最后一项即可单击。有什么办法可以实现吗??
适配器代码:
public class OptionAdapter extends ArrayAdapter<OptionsPojo> {
private int mColorResourceId;
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
OptionsPojo optionsPojo = getItem(position);
View listItemView = convertView;
if (listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.option_card, parent,false);
}
View textImgContainer = listItemView.findViewById(R.id.ll_img_text);
View textContainer = listItemView.findViewById(R.id.rl_text);
ImageView icon = listItemView.findViewById(R.id.image_view);
TextView imgOptnText = listItemView.findViewById(R.id.option_text_view);
TextView optionText = listItemView.findViewById(R.id.tv_text);
int color = getContext().getResources().getColor(mColorResourceId);
if(optionsPojo.hasImage())
{
icon.setImageResource(optionsPojo.getmImageResourceId());
textImgContainer.setBackgroundColor(color);
icon.setVisibility(View.VISIBLE);
//setting option with image
if (optionsPojo.getOption().startsWith("<p>"))
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
imgOptnText.setText(Html.fromHtml(optionsPojo.getOption(), Html.FROM_HTML_MODE_COMPACT));
else
imgOptnText.setText(Html.fromHtml(optionsPojo.getOption()));
}else
imgOptnText.setText(optionsPojo.getOption());
}else{
textImgContainer.setVisibility(View.GONE);
textContainer.setVisibility(View.VISIBLE);
textContainer.setBackgroundColor(color);
//setting option without image
if (optionsPojo.getOption().startsWith("<p>"))
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
optionText.setText(Html.fromHtml(optionsPojo.getOption(), Html.FROM_HTML_MODE_COMPACT));
else
optionText.setText(Html.fromHtml(optionsPojo.getOption()));
}else
optionText.setText(optionsPojo.getOption());
}
return listItemView;
}
public OptionAdapter(@NonNull Context context, @NonNull ArrayList<OptionsPojo> options, int colorResourceId) {
super(context,0, options);
mColorResourceId = colorResourceId;
}
Activity 使用列表视图的代码:
OptionAdapter Adapter = new OptionAdapter(this, optionList, R.color.background);
listview.setAdapter(Adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
showDialog(ChosenTestQuestion.this,optionList.get(i).getAnswer());
}
});
}
请检查此代码
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
if(i == (list.size()-1)){
showDialog(ChosenTestQuestion.this,optionList.get(i).getAnswer());
}
}
});
您在 onItemClick 方法中有位置变量。您只需要检查这是否是您的最后一个元素。