【问题标题】:How to Get Child position for Linear layout如何获取线性布局的子位置
【发布时间】:2015-10-15 06:45:24
【问题描述】:

我有一个 LinearLayout,其中包含几个子 ImageView(将 ImageViews 动态添加到 LinearLayout 水平)。长按子视图(ImageView)时,如何获取该 LinerLayout 的子视图的位置?

添加了 setTag:

 image.setImageBitmap(resizedBitmap);
 image.setTag(fullUrl);



imageViewLinearLayout.setOnLongClickListener(new View.OnLongClickListener(){
     @Override
     public boolean onLongClick(View v) {

          Log.d("Mobi","Path :"+v.getTag());

          return false;
       }
 });

添加设置标签为文件路径但返回v.getTag()为null"?

【问题讨论】:

  • 试试getLeft() / getX()
  • 好的“位置”是什么意思? (x,y) 在屏幕上的位置? (x,y) 在父布局中的位置?子视图数组中的索引?

标签: android


【解决方案1】:

添加这个 test.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/btnAddImage"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add Image" />

    <HorizontalScrollView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1.5"
        android:fadingEdge="none"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/lnrParent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>
    </HorizontalScrollView>

</TableLayout>

还有另一个布局lay_image.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lnrChild"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/btnExample"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50sp"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

这里是编码

setContentView(R.layout.test);

        final LinearLayout LnrLayout = (LinearLayout) findViewById(R.id.lnrParent);

        Button BtnAddImage = (Button) findViewById(R.id.btnAddImage);

        BtnAddImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final LinearLayout LnrChildLayout = (LinearLayout) getLayoutInflater()
                        .inflate(R.layout.lay_image, null);
                LnrChildLayout.setLayoutParams(new LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT));

                LnrLayout.addView(LnrChildLayout);

                int indexValue = LnrLayout.indexOfChild(LnrChildLayout);
                LnrChildLayout.setTag(Integer.toString(indexValue));

                final ImageView ImgView = (ImageView) LnrChildLayout
                        .findViewById(R.id.imgView);

                int ImagePosition = LnrChildLayout.indexOfChild(ImgView);
                ImgView.setTag(Integer.toString(ImagePosition));

                ImgView.setOnLongClickListener(new OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {

                        // get Position
                        String strPosition = (String) v.getTag();
                        Toast.makeText(test.this,
                                "ImageView getTag : >" + strPosition,
                                Toast.LENGTH_SHORT).show();
                        // getId
                        int strId = v.getId();
                        Toast.makeText(test.this,
                                "ImageView getId : >" + strId,
                                Toast.LENGTH_SHORT).show();

                        return true;

                    }
                });

                LnrChildLayout
                        .setOnLongClickListener(new OnLongClickListener() {
                            @Override
                            public boolean onLongClick(View v) {

                                String strPosition = (String) v.getTag();
                                Toast.makeText(test.this, "" + strPosition,
                                        Toast.LENGTH_SHORT).show();

                                return true;
                            }
                        });

                Button BtnExample = (Button) LnrChildLayout
                        .findViewById(R.id.btnExample);
                int ButtonPosition = LnrChildLayout.indexOfChild(BtnExample);
                BtnExample.setTag(Integer.toString(ButtonPosition));

                BtnExample.setOnLongClickListener(new OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {

                        String strPosition = (String) v.getTag();
                        Toast.makeText(test.this,
                                "Button getTag : >" + strPosition,
                                Toast.LENGTH_SHORT).show();

                        int strId = v.getId();
                        Toast.makeText(test.this, "Button getId : >" + strId,
                                Toast.LENGTH_SHORT).show();

                        return true;
                    }
                });

            }
        });

【讨论】:

    【解决方案2】:

    您可以使用 ViewGroup 的indexOfChild 方法。

    我构建了一个小型 sn-p 来检查您的用例(类似)。我将三个按钮动态添加到 LinearLayout 中,然后将 clickHandler 附加到按钮上。您可以使其通用以使其真正有用。

            // Other code
            final LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
            final Button button = new Button(getApplicationContext());
            button.setText("Test");
            final Button button2 = new Button(getApplicationContext());
            button2.setText("Test2");
            final Button button3 = new Button(getApplicationContext());
            button3.setText("Test3");
            layout.addView(button);
            layout.addView(button2);
            layout.addView(button3);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getIndex(layout, button);
                }
            });
    // Other code
    

    在getIndex方法中,代码如下:

    private int getIndex(LinearLayout view, View childView) {
        Log.d("ViewIndex", "View index is" + view.indexOfChild(childView));
        return view.indexOfChild(childView);
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用 imageView.setTag(position) 命令设置 imageView 位置,并在长按时获取它作为 position = (Integer)imageView.getTag()。

      【讨论】:

        【解决方案4】:

        在您的视图上动态添加Long press listener,然后在您的侦听器界面内使用View.getLocationOnScreen() 和/或getLocationInWindow()

        【讨论】:

          【解决方案5】:

          试试这个

          list.setOnLongItemClickListener(new OnItemClickListener() {
          
                  @Override
                  public void onLongItemClick(AdapterView<?> arg0, View viewRow, int pos,
                          long arg3) {
                          int wantedPosition = pos; // Whatever position you're looking for
                          int firstPosition = list.getFirstVisiblePosition() -   list.getHeaderViewsCount(); // This is the same as child #0
                          int wantedChild = wantedPosition - firstPosition;
          ////////////////the wantedChild value , is your position
                  }
              });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-10-09
            • 2014-12-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多