为什么ListView onClick不起作用?

时间:2016-04-20 09:37:11

标签: android listview android-fragments android-listfragment

尝试为android listview实现onlick功能,但似乎无法正常工作。我试图通过互联网找到一些解决方案,我发现有人建议改变imageview和textview元素的可聚焦属性。尝试过,它没有用。如果你们可以推荐任何其他选择,请欣赏。

布局item.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”添加到relativelayout但仍然无法正常工作。最后,我尝试使root relativelayout可以点击并从其他元素中删除焦点,这也不起作用。

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

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