我应该如何获得ANDROID中动态视图的位置

Posted

技术标签:

【中文标题】我应该如何获得ANDROID中动态视图的位置【英文标题】:How should I will get the position of the dynamic view in ANDROID 【发布时间】:2015-11-03 09:45:09 【问题描述】:

下面是我动态创建的视图,我添加了两行,但我无法获得该特定行的位置,所以我可以得到它。我用一个视图创建了四个文本视图,但我怎样才能获得该特定视图的位置?

点击一个按钮,我添加了下面提到的代码,

private LinearLayout llAddMember;
llAddMember = (LinearLayout) findViewById(R.id.llAddMember);
AddFamilyMemberModel familyMemberModel = new AddFamilyMemberModel();
                familyMemberModel.setStrMemberName(strMemberName);
                familyMemberModel.setStrGender(strGender);
                familyMemberModel.setStrMemberRelation(strMemberRelation);
                familyMemberModel.setStrMemberAge(strMemberAge);
                arrAddFamilyMember.add(familyMemberModel);

                LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                llAddMember.setBackgroundColor(getResources().getColor(android.R.color.white));

                TextView txtResMemberName = new TextView(RegistrationPatientActivity.this);
                txtResMemberName.setLayoutParams(lparams);
                txtResMemberName.setText("Member Name: " +familyMemberModel.getStrMemberName());
                txtResMemberName.setTypeface(Typeface.createFromAsset(getAssets(),"Roboto-Regular.ttf"));
                txtResMemberName.setTextSize(14);
                txtResMemberName.setTextColor(Color.parseColor("#9C9C9C"));
                txtResMemberName.setSingleLine(true);
                llAddMember.addView(txtResMemberName);
                lparams.setMargins(50, 5, 40, 0);

                View view1 = new View(RegistrationPatientActivity.this);
                LayoutParams lparamsView1 = new LayoutParams(LayoutParams.WRAP_CONTENT, 2);
                view1.setLayoutParams(lparamsView1);
                view1.setBackgroundColor(Color.parseColor("#DEDDDA"));
                llAddMember.addView(view1);
                lparamsView1.setMargins(50, 10, 0, 0);

                TextView txtResGender = new TextView(RegistrationPatientActivity.this);
                txtResGender.setLayoutParams(lparams);
                txtResGender.setText("Member Gender: " +familyMemberModel.getStrGender());
                txtResGender.setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf"));
                txtResGender.setTextSize(14);
                txtResGender.setTextColor(Color.parseColor("#9C9C9C"));
                llAddMember.addView(txtResGender);
                lparams.setMargins(50, 10, 0, 0);

                View view2 = new View(RegistrationPatientActivity.this);
                LayoutParams lparamsView2 = new LayoutParams(LayoutParams.WRAP_CONTENT, 2);
                view2.setLayoutParams(lparamsView2);
                view2.setBackgroundColor(Color.parseColor("#DEDDDA"));
                llAddMember.addView(view2);
                lparamsView2.setMargins(50, 10, 0, 0);

                TextView txtResMemberRelation = new TextView(RegistrationPatientActivity.this);
                txtResMemberRelation.setLayoutParams(lparams);
                txtResMemberRelation.setText("Member Relation: " +familyMemberModel.getStrMemberRelation());
                txtResMemberRelation.setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf"));
                txtResMemberRelation.setTextSize(14);
                txtResMemberRelation.setTextColor(Color.parseColor("#9C9C9C"));
                llAddMember.addView(txtResMemberRelation);
                lparams.setMargins(50, 10, 0, 0);

                View view3 = new View(RegistrationPatientActivity.this);
                LayoutParams lparamsView3 = new LayoutParams(LayoutParams.WRAP_CONTENT, 2);
                view3.setLayoutParams(lparamsView3);
                view3.setBackgroundColor(Color.parseColor("#DEDDDA"));
                llAddMember.addView(view3);
                lparamsView3.setMargins(50, 10, 0, 0);

                TextView txtResMemberAge = new TextView(RegistrationPatientActivity.this);
                txtResMemberAge.setLayoutParams(lparams);
                txtResMemberAge.setText("Member Age: " +familyMemberModel.getStrMemberAge());
                txtResMemberAge.setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf"));
                txtResMemberAge.setTextSize(14);
                txtResMemberAge.setTextColor(Color.parseColor("#9C9C9C"));
                llAddMember.addView(txtResMemberAge);
                lparams.setMargins(50, 10, 0, 0);

                View view = new View(RegistrationPatientActivity.this);
                LayoutParams lparamsView = new LayoutParams(LayoutParams.WRAP_CONTENT, 4);
                view.setLayoutParams(lparamsView);
                view.setBackgroundColor(Color.parseColor("#006D67"));
                llAddMember.addView(view);
                lparamsView.setMargins(0, 10, 0, 0);

【问题讨论】:

当我得到你正在使用 LinearLayout 管理视图上方时,我是对的吗? 是的,我就是这么做的。 【参考方案1】:

您正在为此使用列表视图,对吗?

在您的活动/片段中...

listview.setOnItemClickListener(new OnItemClickListener() 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
        // do stuff ('position' is the position of the item you've clicked on)
    
);

【讨论】:

但是他没有使用ListView 不,我没有使用任何列表视图,因为我在上面添加了我的代码,为此我使用动态文本视图来实现它,但无法获得该视图的位置。跨度> 所以您要向某些父容器添加视图?如果是这样,您可能希望在每个视图上设置标签,然后在单击时获取标签。不知道为什么要这样做,为此使用 ListView 和 xml 布局是否更有意义(我不明白为什么要以编程方式创建视图) 实际上我有一个滚动视图,在里面我采用了列表视图,但是当我添加行时,将添加唯一一行,如果假设我应该开始添加第二行,它不会该布局不可见。 你已经把一个简单的布局问题变成了一个复杂的问题;)。包含 ListView 的 ScrollView 只是自找麻烦。通过实现一个 ListView(或者更好的 RecycleView)并通过一个简单的 xml 布局来扩展你的行,你会为自己省去很多麻烦。这也将轻松解决您的“获取位置”问题;)【参考方案2】:

如果你只有四个视图,你可以在创建它的时候设置带有位置的标签。像这样:

txtResMemberName.setTag(1); 

然后您可以通过以下方式获得位置:

int pos = (Integer) txtResMemberName.getTag();

【讨论】:

但是我需要整行的位置,这就是我现在有两行的位置。 您也可以按照我的建议将标签设置为 llAddMember。 是的,我已经这样做了,但无法获得职位。【参考方案3】:

正如我们上面所讨论的,您使用 LinearLayout 管理此视图,这不是正确的方法,您需要使用 Android 默认提供的 RecyclerView 或 ListView 来实现此任务。我建议你通过这个Link

【讨论】:

【参考方案4】:

我对上述问题得出结论,并将其实施到我的项目中,对我来说效果很好。

我创建了一个主线性布局,为此我将这些视图添加到该布局中,最后我将新创建的视图添加到我的主线性布局中,希望它能正常工作。

AddFamilyMemberModel familyMemberModel = new AddFamilyMemberModel();
                familyMemberModel.setStrMemberName(strMemberName);
                familyMemberModel.setStrGender(strGender);
                familyMemberModel.setStrMemberRelation(strMemberRelation);
                familyMemberModel.setStrMemberAge(strMemberAge);
                arrAddFamilyMember.add(familyMemberModel);

                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                LinearLayout linearLayout = new LinearLayout(getApplicationContext());
                linearLayout.setLayoutParams(layoutParams);
                linearLayout.setOrientation(LinearLayout.VERTICAL);

                LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                linearLayout.setBackgroundColor(getResources().getColor(android.R.color.white));

                TextView txtResMemberName = new TextView(RegistrationPatientActivity.this);
                txtResMemberName.setLayoutParams(lparams);
                txtResMemberName.setText("Member Name: " +familyMemberModel.getStrMemberName());
                txtResMemberName.setTypeface(Typeface.createFromAsset(getAssets(),"Roboto-Regular.ttf"));
                txtResMemberName.setTextSize(14);
                txtResMemberName.setTextColor(Color.parseColor("#9C9C9C"));
                txtResMemberName.setSingleLine(true);
                linearLayout.addView(txtResMemberName);
                lparams.setMargins(50, 5, 40, 0);

                View view1 = new View(RegistrationPatientActivity.this);
                LayoutParams lparamsView1 = new LayoutParams(LayoutParams.WRAP_CONTENT, 2);
                view1.setLayoutParams(lparamsView1);
                view1.setBackgroundColor(Color.parseColor("#DEDDDA"));
                linearLayout.addView(view1);
                lparamsView1.setMargins(50, 10, 0, 0);

                TextView txtResGender = new TextView(RegistrationPatientActivity.this);
                txtResGender.setLayoutParams(lparams);
                txtResGender.setText("Member Gender: " +familyMemberModel.getStrGender());
                txtResGender.setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf"));
                txtResGender.setTextSize(14);
                txtResGender.setTextColor(Color.parseColor("#9C9C9C"));
                linearLayout.addView(txtResGender);
                lparams.setMargins(50, 10, 0, 0);

                View view2 = new View(RegistrationPatientActivity.this);
                LayoutParams lparamsView2 = new LayoutParams(LayoutParams.WRAP_CONTENT, 2);
                view2.setLayoutParams(lparamsView2);
                view2.setBackgroundColor(Color.parseColor("#DEDDDA"));
                linearLayout.addView(view2);
                lparamsView2.setMargins(50, 10, 0, 0);

                TextView txtResMemberRelation = new TextView(RegistrationPatientActivity.this);
                txtResMemberRelation.setLayoutParams(lparams);
                txtResMemberRelation.setText("Member Relation: " +familyMemberModel.getStrMemberRelation());
                txtResMemberRelation.setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf"));
                txtResMemberRelation.setTextSize(14);
                txtResMemberRelation.setTextColor(Color.parseColor("#9C9C9C"));
                linearLayout.addView(txtResMemberRelation);
                lparams.setMargins(50, 10, 0, 0);

                View view3 = new View(RegistrationPatientActivity.this);
                LayoutParams lparamsView3 = new LayoutParams(LayoutParams.WRAP_CONTENT, 2);
                view3.setLayoutParams(lparamsView3);
                view3.setBackgroundColor(Color.parseColor("#DEDDDA"));
                linearLayout.addView(view3);
                lparamsView3.setMargins(50, 10, 0, 0);

                TextView txtResMemberAge = new TextView(RegistrationPatientActivity.this);
                txtResMemberAge.setLayoutParams(lparams);
                txtResMemberAge.setText("Member Age: " +familyMemberModel.getStrMemberAge());
                txtResMemberAge.setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Regular.ttf"));
                txtResMemberAge.setTextSize(14);
                txtResMemberAge.setTextColor(Color.parseColor("#9C9C9C"));
                linearLayout.addView(txtResMemberAge);
                lparams.setMargins(50, 10, 0, 0);

                View view = new View(RegistrationPatientActivity.this);
                LayoutParams lparamsView = new LayoutParams(LayoutParams.WRAP_CONTENT, 4);
                view.setLayoutParams(lparamsView);
                view.setBackgroundColor(Color.parseColor("#006D67"));
                linearLayout.addView(view);
                lparamsView.setMargins(0, 10, 0, 0);

                int pos = arrAddFamilyMember.size()-1;
                linearLayout.setTag(pos);
                linearLayout.setOnClickListener(new OnClickListener() 

                    @Override
                    public void onClick(View v) 
                        // TODO Auto-generated method stub
                        int position  = Integer.parseInt(v.getTag().toString());
                        AlertDialogUtility.SHOW_TOAST(RegistrationPatientActivity.this, "Position: "+position);
                    
                );

                llAddMember.addView(linearLayout);

【讨论】:

以上是关于我应该如何获得ANDROID中动态视图的位置的主要内容,如果未能解决你的问题,请参考以下文章

Android 中用于显示已旅行位置的动态编程

Android在动态视图中仅获得一次动画效果

如何使用 ios 中的约束使视图位置动态化?

Android - 带有自定义 BaseAdapter 的 Gridview,在位置获得点击视图

我如何获得我在 android 中的最后一个已知位置?

在 Android 中使用 Spinner Dependable 创建动态视图