2013-06-29 93 views
0

這裏是我的代碼佈局捕捉onClick事件在ListView,不onItemClick

<?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> 

在這裏,我的列表視圖

<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。問題是,如果我一直在尋找幾個答案,沒有人幫助我。事件沒有抓住。我錯過了什麼?

回答

2

在您的ParkListAdapter構造函數ParkListAdapter()中,您傳遞的是偵聽器NearMeFragment listner,但您沒有在代碼中的任何位置使用它。適配器中的NearMeFragment fragment對象應該與偵聽器相同。所以只是做這樣的事情在構造和檢查

fragment = listner; 
+0

哦,我的上帝,我不知道我怎麼能忘記這....我真的很傷心已經浪費了你的時間。對不起 – litiales

+1

不用擔心...會發生:) – Antrromet