2012-01-16 60 views
0

我嘗試從代碼動態創建的ListView:動態的RelativeLayout和點擊

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    ViewHolder holder; 
    if (v == null) { 
     LayoutInflater vi = 
      (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.grid_item, null); 

     holder = new ViewHolder(); 
     holder.one = (TextView) v.findViewById(R.id.one); 
     holder.two= (TextView) v.findViewById(R.id.two); 
     holder.three= (TextView) v.findViewById(R.id.three); 
     holder.four= (TextView) v.findViewById(R.id.four); 
     holder.five= (TextView) v.findViewById(R.id.five); 
     v.setTag(holder); 
    } 
    else 
     holder=(ViewHolder)v.getTag(); 

    final Custom custom = entries.get(position); 
    if (custom != null) { 
     holder.one .setText(custom.getOne()); 
     holder.two.setText(custom.getTwo()); 
     holder.three.setText(custom.getThree()); 
     holder.four.setText(custom.getFour()); 
     holder.five.setText(custom.getFive()); 
    } 
    return v; 
} 

grid_item.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="0dp" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" > 

     <CheckBox 
      android:id="@+id/checkBox1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="" 
      android:layout_marginRight="4dp" /> 

     <View android:id="@+id/separator" 
      android:background="#cccccc" 
      android:layout_width="1dip" 
      android:layout_height="45dip" 
      android:layout_marginRight="4dp"/> 

    </LinearLayout> 

    <TextView 
     android:id="@+id/one" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/linearLayout1" 
     android:layout_toLeftOf="@+id/linearLayout2" 
     android:text="" 
     android:textSize="25dp"/> 

    <TextView 
     android:id="@+id/two" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/alarmTitle" 
     android:layout_toRightOf="@+id/linearLayout1" 
     android:layout_toLeftOf="@+id/linearLayout2" 
     android:text="" 
     android:textSize="16dp" /> 

    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="3dp" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/three" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:textSize="12dp" /> 

     <TextView 
      android:id="@+id/four" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:textSize="12dp" /> 

     <TextView 
      android:id="@+id/five" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:textSize="12dp" /> 

     <TextView 
      android:id="@+id/six" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:textSize="12dp" /> 
    </LinearLayout> 

</RelativeLayout> 

我想在ListView項,在我的例子是RelativeLayout的,可點擊。 我想:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:clickable="true"> 

和/或:

 v.setClickable(true); 
     v.setEnabled(true); 
     v.setFocusable(true); 

但是,這是行不通的。

我在哪裏犯錯?對我的作品 -

回答

1

您可以點擊監聽器添加到您的v鑑於getView()方法

v.setOnClickListener(new OnClickListener(){ 
    public void onClick(View v) { 
    }.... 

這應該工作內返回。內部調用的方法應該移到其他地方。不知道您是否想在適配器中實現View.OnClickListener,但是我認爲這取決於您。

乾杯