获取单击的ViewGroup的当前位置

时间:2016-11-23 22:20:55

标签: android

我从这个链接实现了默认的布局动画: https://developer.android.com/training/animation/layout.html

然后在每一行上我都有textView并编辑textView。当选择textView时,我正在显示timePicker并显示按钮。选择时间并点击按钮后,我需要更新相应的textview。

问题在于我无法获得所点击行的位置。

以下是添加行和选择时间的java代码:

private void addMealItem() {
        // Instantiate a new "row" view.
        final ViewGroup newView = (ViewGroup) LayoutInflater.from(this).inflate(
                R.layout.activity_meal_builder_listview_item, mContainerView, false);


        // Set a click listener for the "X" button in the row that will remove the row.
        newView.findViewById(R.id.delete_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Remove the row from its parent (the container view).
                // Because mContainerView has android:animateLayoutChanges set to true,
                // this removal is automatically animated.
                mContainerView.removeView(newView);

            }
        });

        newView.findViewById(R.id.time).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                timePickerGroupView.setVisibility(View.VISIBLE);

            }
        });

        // Because mContainerView has android:animateLayoutChanges set to true,
        // adding this view is automatically animated.
        mContainerView.addView(newView, mContainerView.getChildCount());

    }

    public void timeSelected (View v) {
        hour = TimePickerView.getCurrentHour();
        minutes = TimePickerView.getCurrentMinute();
        int id = (int) mContainerView.getTag();

        timeTextView.setText(hour + ":" + minutes);
        timePickerGroupView.setVisibility(View.GONE);
    }

我的xml文件是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#191919"
    android:layout_marginBottom="8dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="time"
        android:ems="4"
        android:id="@+id/time"
        android:textColor="@color/white"
        android:text="00:00" />
    <EditText
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:ems="10"
        android:id="@+id/meal"
        android:hint="Meal"
        android:textColorHint="@color/white"
        android:textColor="@color/white"
        android:backgroundTint="@color/PrimaryColor"/>
    <!-- A button that, when pressed, will delete this list item row from its container. -->
    <ImageView android:id="@+id/delete_button"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:src="@drawable/image_ic_remove_circle_outline_white_48dp"
        android:contentDescription="@string/action_remove_item"
        android:layout_gravity="center_vertical"
        android:backgroundTint="@color/PrimaryColor"/>
</LinearLayout>

那么,我怎样才能正确更新@time。 谢谢你的帮助。

0 个答案:

没有答案