以编程方式在Horizo​​ntalScrollView中插入视图 [英] Insert views in HorizontalScrollView programmatically

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

问题描述

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

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

使用xmls&完全保持原状

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

}

注意:我要在HSV的Click上添加视图.

NOTE: I want to add views onClick of HSV.

Q-1:我无法触发HSV或其中的LinearLayout的onClick.我尝试做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我编写了将代码插入到按钮的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??

推荐答案

问题2:您必须创建一个新的孩子

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天全站免登陆