以编程方式在Horizo​​ntalScrollView中插入视图

时间:2014-02-17 11:17:23

标签: android android-view horizontalscrollview

我正在制作一个看起来像这样的虚拟视图的聊天应用程序:

使用xmls&活动与他们完全一样

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

<HorizontalScrollView
    android:id="@+id/chat_message_HSV"
    android:layout_width="fill_parent"
    android:layout_height="75dp"
    android:layout_alignParentBottom="true"
    android:layout_toLeftOf="@+id/send"
    android:clickable="true" >

    <LinearLayout
        android:id="@+id/LL_inside_HSV"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="horizontal" />
</HorizontalScrollView>

<ImageButton
    android:id="@+id/send"
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:src="@drawable/ic_launcher" />

</RelativeLayout>

我想为Horizo​​ntalScrollView(HSV)添加一个视图。我读过 HSV只能有一个孩子(主要是LinearLayout)&amp;必须添加视图。这是我要添加的视图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="75dp"
android:layout_height="75dp"
android:orientation="vertical"
android:background="#454545" >

<ImageView
    android:id="@+id/profilePicture"
    android:layout_width="60dp"
    android:layout_height="60dp" />

<AutoCompleteTextView
    android:layout_width="75dp"
    android:layout_height="wrap_content" />

</LinearLayout>

这是我的活动:

public class MainActivity extends Activity {
ImageButton sendIB;
HorizontalScrollView chatMessageHSV;
LinearLayout lLinsideHSV;

View child;
LayoutInflater inflater;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    inflater = (LayoutInflater) this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    child = inflater.inflate(R.layout.word_element, null, false);

    sendIB = (ImageButton) findViewById(R.id.send);
    chatMessageHSV = (HorizontalScrollView) findViewById(R.id.chat_message_HSV);
    lLinsideHSV = (LinearLayout) findViewById(R.id.LL_inside_HSV);

    chatMessageHSV.setOnClickListener(new OnClickListener() {
// this never gets triggered
        @Override
        public void onClick(View arg0) {
            Toast.makeText(MainActivity.this, "hsv", Toast.LENGTH_SHORT)
                    .show();
        }
    });

    lLinsideHSV.setOnClickListener(new OnClickListener() {
// this never gets triggered
        @Override
        public void onClick(View arg0) {
            Toast.makeText(MainActivity.this, "LL", Toast.LENGTH_SHORT)
                    .show();
        }
    });

    sendIB.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            lLinsideHSV.addView(child);
        }
    });
}// end of onCreate

}

注意:我想在HSV上添加点击视图。

问题1:我无法触发其中的HSV onClick或LinearLayout。我尝试了onTouchEvent,但每次触摸都会触发该事件4-5次。

Q-2我编写了代码来插入onClick按钮内的视图(仅用于实验)。我只能插入一次视图。之后它抛出了一个异常说:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

那么如何再次插入视图&amp;再次??

1 个答案:

答案 0 :(得分:2)

Q2:你必须创建新的孩子

child = inflater.inflate(R.layout.word_element, null, false);

每次点击活动。类似的东西:

@Override
onClick() {
    View child = inflater.inflate(...);
    lLinsideHSV.addView(child);
}