【问题标题】:Add multiple custom LinearLayouts with Buttons to ListView item将多个带有按钮的自定义 LinearLayouts 添加到 ListView 项
【发布时间】:2017-10-06 09:42:45
【问题描述】:

我有一个与this one 相关的问题。

有人询问以编程方式将多个自定义 LinearLayouts 添加到 ListView 项,这几乎是我需要做的。在这个问题中,作者在 LinearLayout 中有 TextViews。我有一个 Button(和其他一些),当我单击此 Button 时,我想通过单击 Button 在此下添加一个新的 LinearLayout。

如何获取当前的 LinearLayout 位置,以便在其下添加新的 LinearLayout?现在我在列表底部添加新的 LinearLayout。

【问题讨论】:

    标签: android android-layout listview button layout-inflater


    【解决方案1】:

    你可以这样做

    yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
            //if you want get linearLayout at current position
            LinearLayout LINTOP = ((LinearLayout) parent.getChildAt(0));
            addLayout(LINTOP);
        }
    });
    

    在 LINTOP 下添加这样的新布局

    public void addLayout(LinearLayout parent){
        LinearLayout newLayout= new LinearLayout(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        parent.addView(newLayout, 0, params);
    }
    

    【讨论】:

    • .getChildAt 要求方法限定符以及如何在此方法下添加新的 LinearLayout?
    • 你在用这个 LinearLayout LINTOP = ((LinearLayout) parent.getChildAt(0));
    • 在这个下或者你想添加它的孩子
    • 我想要新的LinearLayout,只需点击一下按钮。
    • LinearLayout底部还在添加
    【解决方案2】:

    首先,请使用RecyclerView而不是ListView

    您有一个名为getAdapterPosition() 的方法,它返回交互发生的位置。所以在 ViewHolder 中,如果你有类似的东西

    linearLayoutView.setOnClickListener { Log.d("Position:"+getAdapterPostion()); }

    这会给你这个职位。 (顺便说一句,上面的代码是在 Kotlin 中的,所以看起来可能有点不同)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 2015-01-12
      相关资源
      最近更新 更多