有什么办法可以让我在ListView中点击特定的项目吗?

时间:2018-04-13 09:40:12

标签: android listview events onitemclicklistener selected

我有一个场景,我在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;
}

使用listview的活动代码:

      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());
            }
        });
    }

2 个答案:

答案 0 :(得分:1)

请查看此代码

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()); 
                }
            }
        });

答案 1 :(得分:1)

onItemClick方法中有位置变量。你只需要检查这是否是你的最后一个元素。