在listView中捕获onClick事件,而不是onItemClick

时间:2013-06-29 11:06:48

标签: android android-layout

这是我的代码布局

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#ff0000"
    >

<ListView
        android:id="@+id/parkList"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:divider="@android:color/transparent"
        android:dividerHeight="5.0dp"
        android:textIsSelectable="false">
</ListView>

</LinearLayout>

这里是listview项目的自定义布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"

          android:layout_height="wrap_content"
          android:background="@drawable/smooth_rectangle"
    >

<TableRow
        android:layout_height="70dp"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginLeft="3dp"
        android:layout_marginTop="3dp"
        android:layout_marginRight="3dp"
        android:weightSum="3"
        >

    <android.widget.ImageView
            android:id="@+id/photo1"
            android:layout_width="10dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#000000"
            android:gravity="left"
            android:src="@drawable/ic_launcher"
            android:layout_marginRight="3dp"/>

    <android.widget.ImageView
            android:id="@+id/photo1"
            android:layout_width="10dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#000000"
            android:gravity="center_horizontal"
            android:layout_marginRight="3dp"
            android:src="@drawable/ic_launcher"/>

    <android.widget.ImageView
            android:id="@+id/photo1"
            android:layout_width="10dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#000000"
            android:gravity="right"
            android:src="@drawable/ic_launcher"/>

</TableRow>

<TextView
        android:id="@+id/parkname"
        android:layout_marginTop="3dp"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="Parco"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:textColor="#111111"/>

<TextView
        android:id="@+id/location"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="Acquasparta via Tiberina 48"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:textColor="#111111"/>

<TextView
        android:id="@+id/last_opinion"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="Questo parco e' molto bello"
        android:layout_marginLeft="7dp"
        android:layout_marginRight="7dp"
        android:textColor="#111111"/>

<LinearLayout
        android:layout_marginTop="3dp"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal">

    <TextView
            android:id="@+id/dirty"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="pulizia"
            android:textColor="#111111"/>

    <TextView
            android:id="@+id/noisy"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="silenziosità"
            android:textColor="#111111"/>

    <TextView
            android:id="@+id/green"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="verde"
            android:textColor="#111111"/>

</LinearLayout>

<LinearLayout
        android:layout_marginTop="3dp"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal">

    <TextView
            android:id="@+id/canjog"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="can jogging"
            android:textColor="#111111"/>

    <TextView
            android:id="@+id/withbar"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="there is a bar"
            android:textColor="#111111"/>

</LinearLayout>

<LinearLayout
        android:layout_marginTop="3dp"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:weightSum="3">

    <TextView
            android:id="@+id/comment"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Live Comment"
            android:textColor="#111111"
            android:background="#ddddff"
            android:clickable="true"/>

    <View
            android:layout_height="fill_parent"
            android:layout_width="0.5dp"
            android:background="#333333"
            />

    <TextView
            android:id="@+id/gotomaps"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Naviga"
            android:textColor="#111111"
            android:background="#ddddff"
            android:clickable="true"/>

    <View
            android:layout_height="fill_parent"
            android:layout_width="0.5dp"
            android:background="#333333"
            />

    <TextView
            android:id="@+id/signal"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Segnala"
            android:textColor="#111111"
            android:background="#ddddff"
            android:clickable="true"/>

</LinearLayout>

</LinearLayout>

这是我的arrayadapter

public class ParkListAdapter extends ArrayAdapter<Park> {

private NearMeFragment fragment;

public ParkListAdapter(Context context, int resourceId, Park[] objects, NearMeFragment listner) {
    super(context, resourceId, objects);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.small_park_container, null);
    Park park = getItem(position);
    ((TextView) convertView.findViewById(R.id.parkname)).setText(park.getParkName());
    ((TextView) convertView.findViewById(R.id.location)).setText(park.getLocation());
    ((TextView) convertView.findViewById(R.id.last_opinion)).setText(park.getLastComment());
    ((TextView) convertView.findViewById(R.id.green)).setText(String.valueOf(park.getGreen()));
    ((TextView) convertView.findViewById(R.id.noisy)).setText(String.valueOf(park.getNoisy()));
    ((TextView) convertView.findViewById(R.id.dirty)).setText(String.valueOf(park.getDirty()));
    ((TextView) convertView.findViewById(R.id.canjog)).setText(String.valueOf(park.isJogging()));
    ((TextView) convertView.findViewById(R.id.withbar)).setText(String.valueOf(park.isBar()));
    TextView interactive = (TextView) convertView.findViewById(R.id.signal);
    interactive.setTag(String.valueOf(park.getParkID()));
    interactive.setOnClickListener(fragment);
    interactive = (TextView) convertView.findViewById(R.id.gotomaps);
    interactive.setTag(String.valueOf(park.getLatitude()) + "!" + String.valueOf(park.getLongitude()));
    interactive.setOnClickListener(fragment);
    interactive = (TextView) convertView.findViewById(R.id.comment);
    interactive.setTag(String.valueOf(park.getParkID()));
    interactive.setOnClickListener(fragment);
    return convertView;
}
}

在这里我的NearMeFragment:

public class NearMeFragment extends Fragment implements View.OnClickListener{

private FragmentManager fragmentManager;

@Override
public void onCreate(Bundle savedBundle){
    super.onCreate(savedBundle);
}

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedBundle) {
    View v = inflater.inflate(R.layout.park_list, container, false);
    View header = inflater.inflate(R.layout.sort_by_header, null);
    //TODO remove
    make_test(v, header);

    return v;
}

public void openTest(View view){
    new LiveComment().show(getChildFragmentManager(), "dialog");
}

@Override
public void onClick(View view) {
    Log.i("clickevent",view.toString());
}

private void make_test(View v, View header) {

    ListView listView = (ListView) v.findViewById(R.id.parkList);
    listView.setFocusable(false);
    listView.setFocusableInTouchMode(false);
    listView.setClickable(false);
    listView.setItemsCanFocus(true);
    Park[] list = new Park[3];
    list[0] = new Park("parco a", "acquasparta", "bello", false, false, 1, 2, 3, 1,2,3);
    list[1]=new Park("parco b", "perugia", "bello", false, false, 1, 2, 3, 1, 2, 3);
    list[2]=new Park("parco b", "perugia", "bello", false, false, 1, 2, 3, 1, 2, 3);
    ParkListAdapter adapter = new ParkListAdapter(v.getContext(), R.layout.small_park_container,list, this);
    listView.addHeaderView(header);
    listView.setAdapter(adapter);
}

}

我想要的是捕获在我的自定义布局中的三个TextView上触发的onClick事件,而不是onItemClick。问题是,如果我一直在寻找几个答案,没有人帮助过我。事件没有被捕获。我错过了什么?

1 个答案:

答案 0 :(得分:2)

在您的ParkListAdapter构造函数ParkListAdapter()中,您传递的是侦听器NearMeFragment listner,但您并未在代码中的任何位置使用它。适配器中的NearMeFragment fragment对象应该等于侦听器。所以在构造函数中执行类似的操作并检查

fragment = listner;