为什么ListView onClick不起作用



试图为android listview实现一个onlick功能,但有些东西似乎不起作用。我试图在互联网上找到一些解决方案,我发现有人建议更改imageview和textview元素的可聚焦属性。尝试了一下,但没有成功。如果你们能推荐其他选择,我将不胜感激。

布局项.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:id="@+id/rootItemId"
                android:clickable="true"
                android:gravity="center_vertical">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable='false'
        android:background="@drawable/frame">
        <ImageView
            android:id="@+id/ivIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:contentDescription="@string/hello_world"
            android:padding="5dp"
            android:focusable='false'
            android:src="@drawable/ic_launcher"
            android:scaleType="fitXY"/>
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:focusable='false'
            android:layout_toRightOf="@id/ivIcon">
            <TextView
                android:id="@+id/tvTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:focusable='false'
                android:textAppearance="@android:style/TextAppearance.Medium"/>
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

列表片段

    public class EventsListFragment extends ListFragment{
    ArrayList<Event> eventsChildren;
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Log.e("RedditListingsClick",position + " " + id);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        eventsChildren = new ArrayList<Event>();
        EventsChildAdapter adapter = new EventsChildAdapter(inflater.getContext(), eventsChildren);
        setListAdapter(adapter);
        return super.onCreateView(inflater, container, savedInstanceState);
    }
}

列表适配器

public class EventsChildAdapter extends ArrayAdapter<Event> {
    Context context;
    RelativeLayout root;
    private static class ViewHolder {
        TextView title;
        ImageView thumbnail;
    }
    public EventsChildAdapter(Context context, ArrayList<Event> eventsChildren) {
        super(context, R.layout.item_listing, eventsChildren);
        this.context = context;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        Event child = getItem(position);
        // Check if an existing view is being reused, otherwise inflate the view
           ViewHolder viewHolder; // view lookup cache stored in tag
           if (convertView == null) {
              viewHolder = new ViewHolder();
              LayoutInflater inflater = LayoutInflater.from(getContext());
              convertView = inflater.inflate(R.layout.item_listing, null);
               //root=(RelativeLayout)convertView.findViewById(R.id.rootItemId);
               viewHolder.title = (TextView) convertView.findViewById(R.id.tvTitle);
               //viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.ivIcon);
              convertView.setTag(viewHolder);
           } else {
               viewHolder = (ViewHolder) convertView.getTag();
           }
           // Populate the data into the template view using the data object
           System.out.println(child.getTitle());
           viewHolder.title.setText(child.getTitle());
            Picasso.with(this.context).load(child.getThumbnailImageURL()).resize(200, 100).into(viewHolder.thumbnail);
        // Return the completed view to render on screen
           return convertView;
    }
}

除此之外,我已经尝试将android:descendantFocusability="blocksDescendants"添加到relativeloout中,但仍然没有成功。最后,我试图使根相对布局可以点击,并从其他元素中删除焦点,但这也不起作用。

你可以试试这个:

public class EventsListFragment extends ListFragment implement OnItemClickListener {
 ...
    @Override
   public void onItemClick(AdapterView<?> parent, View view, int    position,long id) {
  // the child callback here.
 }
}

这在onActivityCreated

setListAdapter(Your adapter);
getListView().setOnItemClickListener(this);

最新更新