简体   繁体   English

TextView Onclick在列表视图中

[英]TextView Onclick Inside of a listview

I have list view and inside of it there's a text view that also contains a clickable. 我有列表视图,并且在其中有一个文本视图,其中也包含一个可点击的视图。 What should I do? 我该怎么办? Where should I put it? 我应该放在哪里?

voucherList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        if (Integer.parseInt(vouchers.get(position).Points) < 0) {
            redeemAlert(getActivity(), "", "Would you like to redeeem this voucher now?  Vouchers can only be redeemed once.  Please make sure you are in the counter area of a Healthy Options store.",
                    vouchers.get(position).DocumentNo, vouchers.get(position).ExpirationDate, "\u20B1 " + vouchers.get(position).Points + " points", vouchers.get(position).Entry_No, vouchers.get(position).Details);
        } else {
            redeemAlert(getActivity(), "", "Would you like to redeeem this voucher now?  Vouchers can only be redeemed once.  Please make sure you are in the counter area of Baking Therapy. The voucher will expire 1 minute after redeemed.",
                    vouchers.get(position).DocumentNo, vouchers.get(position).ExpirationDate, " ", vouchers.get(position).Entry_No, vouchers.get(position).Details);
            //getView().findViewById(R.id.redemptionCode).setVisibility(View.GONE);
                            }

click this link. 点击此链接。 this is what i want to do 这就是我想做的

You can create a custom ArrayAdapter for your ListView where you set the TextView to have a click listener. 您可以为ListView创建自定义ArrayAdapter ,在其中将TextView设置为具有单击侦听器。

public class CustomAdapter extends ArrayAdapter<String> {


    public CustomAdapter(Context context, int textViewResourceId, List<String> objects) {
        super(context, textViewResourceId, objects);

    }
    static class ViewHolder {
        TextView text;
        TextView clickableText;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        String color = getItem(position);

        View rowView = convertView;

        if (rowView == null) {
            LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
            rowView = inflater.inflate(R.layout.list_view_row, parent, false);
            CustomAdapter.ViewHolder h = new CustomAdapter.ViewHolder();

            h.text = (TextView) rowView.findViewById(R.id.item_text);
            h.clickableText = (TextView) rowView.findViewById(R.id.clickable_text);
            rowView.setTag(h);
        }

        CustomAdapter.ViewHolder h = (CustomAdapter.ViewHolder) rowView.getTag();

        h.text.setText(color);
        h.clickableText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // add your functionality here

                // ...

            }
        });

        return rowView;
    }

}

You then add your ListView just like you normally would in your onCreate() or other methods of the app lifecycle you needed it to be in. 然后,您就可以像通常在onCreate()或应用程序生命周期中所需的其他方法中添加ListView一样。

// ArrayList<String> list

  CustomAdapter adapter = new CustomAdapter(this, R.layout.list_view_row, list);
  ListView listview = (ListView) findViewById(R.id.your_list);
  listview.setAdapter(adapter);

Edit: Set android:descendantFocusability="blocksDescendants" in the <LinearLayout> or <RelativeLayout> from the xml file for your custom listview row. 编辑:从xml文件的<LinearLayout><RelativeLayout>为您的自定义列表视图行设置android:descendantFocusability="blocksDescendants"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM