【问题标题】:Insert views in HorizontalScrollView programmatically以编程方式在 Horizo​​ntalScrollView 中插入视图
【发布时间】:2019-01-16 12:07:42
【问题描述】:

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

按原样使用 xml 和活动

<?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)& 必须添加视图。这是我要添加的视图:

<?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 的点击上添加视图。

Q-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.

那么我如何一次又一次地插入视图??

【问题讨论】:

  • 在添加视图之前试试这个 parentview.removeView()
  • 我应该删除哪个视图。这个函数需要一个 View 参数
  • child.getParent() 将为您提供父视图,您应先将其删除。
  • @stan0 : 已经回答了我的这部分问题...无论如何谢谢...请寻找问题1的解决方案

标签: android android-view horizontalscrollview


【解决方案1】:

Q2:你必须创建新的孩子

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

每次点击事件。比如:

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

【讨论】:

  • 正确..谢谢...请也回答第一个问题...这是一个主要障碍
  • 我想不出 Q1 的正确答案。如果我有什么想法,我会编辑我的答案...
  • 谢谢,这给了我更正答案的想法,因为我在 json 中获取数据,我已经在循环之外声明了我的视图,它给了我一个错误,说删除视图()[与上面相同] 但是对于每个 json 对象,必须声明一个视图。[希望它对性能方面的人有所帮助,人们将视图声明和初始化放在外面]
猜你喜欢
  • 1970-01-01
  • 2011-06-15
  • 1970-01-01
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 2015-10-07
  • 1970-01-01
相关资源
最近更新 更多