从Android中的GestureOverlayView获取孩子

时间:2011-08-09 14:48:21

标签: android view android-layout

我正在尝试将TextView作为GestureOverlayView的子元素。

这是我的视图xml代码:

<RelativeLayout android:layout_width="fill_parent" android:id="@+id/relativeLayout1" android:layout_height="wrap_content">
    <android.gesture.GestureOverlayView android:layout_height="wrap_content" android:id="@+id/gestureOverlayView1" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:layout_alignParentLeft="true">
        <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:id="@+id/linearLayout2" android:layout_height="wrap_content">
            <LinearLayout android:orientation="horizontal" android:weightSum="1" android:layout_width="260dp" android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:baselineAligned="false" android:layout_weight="0.32">
                <CheckBox android:id="@+id/sListCheckbox"  android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.32"></CheckBox>
                <TextView android:text="TextView" android:id="@+id/sListText" android:layout_width="212dp" android:ems="15" android:layout_gravity="center" android:textAppearance="?android:attr/textAppearanceLarge" android:singleLine="true" android:layout_height="wrap_content" android:layout_weight="0.32"></TextView>
            </LinearLayout>
            <ImageView android:paddingRight="0dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="0.32" android:id="@+id/imageView1" android:src="@drawable/searchbutton"></ImageView>
        </LinearLayout>
    </android.gesture.GestureOverlayView>
</RelativeLayout>

我正试图像这样到达TextView,但我在childText上得到nullPointerException

adapterLine.gestureOverlayView.setOnTouchListener(new View.OnTouchListener() {
   @Override
  public boolean onTouch(View v, MotionEvent event) {

 if (gestureDetector.onTouchEvent(event)) {

    RelativeLayout llt = (RelativeLayout) v.getParent();
    TextView childText = (TextView) llt.getChildAt(4);

2 个答案:

答案 0 :(得分:1)

您正在调用RelativeLayout llt =(RelativeLayout)v.getParent(); - &GT;你的变量对于LinearLayout说llt, 但是你得到了RelativeLayout?试试这个:

LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
TextView childText = (TextView)ll.getChildAt(1); // it may be 2 depending on if the root is 0 or not, cant remember

其实为什么不这样做:

TextView childText = (TextView)findViewById(R.id.sListText);

答案 1 :(得分:0)

我用这个

解决了我的问题
RelativeLayout  llt = (RelativeLayout) v.getParent();
GestureOverlayView gow = (GestureOverlayView) llt.getChildAt(0);
LinearLayout ll1 = (LinearLayout) gow.getChildAt(0);
LinearLayout ll2 = (LinearLayout) ll1.getChildAt(0);
TextView childText = (TextView) ll2.getChildAt(1);

我无法使用

TextView childText = (TextView)findViewById(R.id.sListText);

这是因为我在列表视图中从我的动作事件监听器获取v视图,而且我不确切知道返回哪个textview。