在Horizo​​ntalScrollView插入编程意见 [英] Insert views in HorizontalScrollView programatically

查看:251
本文介绍了在Horizo​​ntalScrollView插入编程意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个聊天应用程序,它看起来像这样(虚拟视图):

I'm making a chat app which looks like this(dummy view):

使用了XML的&放大器;活动正是因为它们是

Use the xmls & activity exactly as they are

<?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只能有1个小孩(大部分的LinearLayout)及次必须添加到它。这是我想补充的观点:

I want to add a view to HorizontalScrollView(HSV). I've read that HSV can have only 1 child (mostly LinearLayout) & views must be added to it. This is the view I want to add:

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

}

请注意:我想补充意见的onClick HSV的

NOTE: I want to add views onClick of HSV.

Q-1:我无法触发HSV的onclick或的LinearLayout里面。我想这样做onTouchEvent,但是该事件被触发4-5次的每一个接触。

Q-1 : I am unable to trigger the onClick of HSV or the LinearLayout inside it. I tried doing onTouchEvent but that event is triggered 4-5 times on every touch.

Q-2我写了code插入内部按钮的onClick(只是为了实验)视图。我能够插入该视图只有一次。之后,它抛出一个异常,说:

Q-2 I wrote the code to insert view inside onClick of button(just to experiment). I am able to insert the view only once. After that it throws an exception saying:

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

那么,如何才能再次与放插入的意见;再次??

So how do I insert the views again & again??

推荐答案

Q2:你必须创建新的子

Q2: You have to create new child

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

这是每次点击的事件。是这样的:

on every click event. Something like:

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

这篇关于在Horizo​​ntalScrollView插入编程意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆