Insert views in HorizontalScrollView programmatically

别来无恙 提交于 2019-12-25 02:41:15

问题


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

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>

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>

This is my activity:

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

}

NOTE: I want to add views onClick of HSV.

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


回答1:


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);
}


来源:https://stackoverflow.com/questions/21827608/insert-views-in-horizontalscrollview-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!