简体   繁体   中英

Can I use OnItemClickListener for ListView with OnClickListener for items?

Is it possible to use OnItemClickListener for ListView and OnClickListener for their items together? This is not nesessary, but in my case it more comfortable to separate functionality beetween Adapter (onClick) and Activity (onItemClick). And when I try to use these listeners together, only OnClickListener works.

All what I do is:

public class ProfilePassengersListActivity extends ConnectActivity
{
    protected ListView lv;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.another_passengers_list_screen);
        anotherPassengersListView = (ListView)findViewById(R.id.anotherPassengersListListViewPassengers);
        lv.setOnItemSelectedListener(openInfoTraveler);
    }
    private android.widget.AdapterView.OnItemClickListener openInfoTraveler = new OnItemClickListener() 
    {
          public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) 
          {
              Log.i(LOGTAG, "in onclick");
          }
    };
}

And at adapter:

public class ProfilePassengersListAdapter extends ArrayAdapter<String> {
...
@Override
    public View getView(final int position, View convertView, final ViewGroup parent){
        convertView = super.getView(position, convertView, parent);
        final String currObj = list.get(position);
        if(convertView.findViewById(R.id.item_delete_confirmation) == null)
        {
            RelativeLayout container = new RelativeLayout(ctx);
            LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View child = inflater.inflate(R.layout.item_delete_confirmation, null);
            convertView.setTag("main");
            container.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, convertView.getHeight()));
            container.addView(convertView);
            container.addView(child);
            convertView = container;
        }
        if(!preparedToDelete.contains(list.get(position)))
        {
            convertView.findViewWithTag("main").setVisibility(View.VISIBLE);
            convertView.findViewById(R.id.item_delete_confirmation).setVisibility(View.INVISIBLE);
        }
        else
        {
            convertView.findViewWithTag("main").setVisibility(View.GONE);
            convertView.findViewById(R.id.item_delete_confirmation).setVisibility(View.VISIBLE);
            //Log.i("wtf", "prepared to delete: " + currObj);
        }
        final View itemView = convertView;
        /*convertView.setFocusable(false);
        convertView.setFocusableInTouchMode(false);
        convertView.setClickable(true);*/
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.i(LOGTAG, "onClick");
            }
        });
        return convertView;
}

Use this method:

lv.setOnItemClickListener(openInfoTraveler);

instead of this:

lv.setOnItemSelectedListener(openInfoTraveler);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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