【问题标题】:Android - Getting Objects in Multiple RootViewAndroid - 在多个 RootView 中获取对象
【发布时间】:2019-02-08 23:42:46
【问题描述】:
public class OccuMechanical extends android.support.v4.app.Fragment {

    private View v_schedule;
    private LinearLayout layout_schedule;
    private ImageButton iv_add_schedule;

    private int i = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_occu_mechanical, container, false);

        layout_schedule = (LinearLayout) v.findViewById(R.id.layout_mech_schedule);

        iv_add_schedule = (ImageButton) v.findViewById(R.id.iv_add_schedule);
        iv_add_schedule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                i++;
                v_schedule = LayoutInflater.from(getActivity()).inflate(R.layout.layout_mech_schedule, null);
                layout_schedule.addView(v_schedule);
                EditText et = (EditText) layout_schedule.getRootView().findViewById(R.id.layout_description);
                et.setText("Test: " + String.valueOf(i));
            }
        });
        return v;
    }
}

如果我的术语在标题中是正确的,但这是我正在做的事情...... 当我单击“添加更多设备”时,它会将布局添加到我的应用程序中 我想获取我在 RootView 中添加的布局中的所有 EditText, 我尝试设置文本来测试它,但第一个 rootView 的第一个 editText 是唯一更新的。上面列出的我的片段中的所有代码。附图是这段代码的结果。

image result

【问题讨论】:

  • btw ,第一行是布局中的默认值。第二行是点击“添加更多设备”按钮后显示的第一个布局
  • 我想建议你使用 RecyclerView 来完成这个任务。每行是项目列表的一个项目。每当用户点击添加更多设备时,您会在项目列表中添加一个新项目,然后使用 notifydatachanged() 或类似函数更新适配器。
  • 您可以在developer.android.com/guide/topics/ui/layout/recyclerview找到更多关于RecyclerView的信息
  • @hjchin 感谢您的建议并提醒我有关 RecyclerView 的信息,我稍后会尝试一下

标签: android rootview


【解决方案1】:

我已经按照 @hjchin 的建议使用 *RecyclerView 完成了这项任务。谢谢!

RecyclerView Tutorial @ Developer.Android

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 2014-12-29
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 2019-12-09
    相关资源
    最近更新 更多