【问题标题】:how to get position of row and column in table view如何在表格视图中获取行和列的位置
【发布时间】:2016-01-07 11:15:37
【问题描述】:

我是安卓初学者。 我正在尝试获取当前图像的位置,动态行和列,但我不知道该怎么做。下面是我的代码,请帮助我:

MainActivity extends Activity {

    int i,j;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] row = { "ROW1", "ROW2", "Row3"};
        String[] column = { "COLUMN1", "COLUMN2","CO" };
        int rl=row.length; int cl=column.length;

        ScrollView sv = new ScrollView(this);
        TableLayout tableLayout = createTableLayout(row, column,rl, cl);
        HorizontalScrollView hsv = new HorizontalScrollView(this);

        hsv.addView(tableLayout);
        sv.addView(hsv);
        setContentView(sv);

    }

    public void makeCellEmpty(TableLayout tableLayout, int rowIndex, int columnIndex) {
        // get row from table with rowIndex
        TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);

        // get cell from row with columnIndex
        TextView textView = (TextView)tableRow.getChildAt(columnIndex);

        // make it black
        textView.setBackgroundColor(Color.BLACK);
    }
    public void setHeaderTitle(TableLayout tableLayout, int rowIndex, int columnIndex){

        // get row from table with rowIndex
        TableRow tableRow = (TableRow) tableLayout.getChildAt(rowIndex);

        // get cell from row with columnIndex
        TextView textView = (TextView)tableRow.getChildAt(columnIndex);

        textView.setText("Hello");
    }

    private TableLayout createTableLayout(String [] rv, String [] cv, final int rowCount, int columnCount) {
        TableLayout.LayoutParams tableLayoutParams = new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        TableLayout tableLayout = new TableLayout(this);
        tableLayout.setBackgroundResource(R.drawable.bookshelf);

        TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams();
        tableRowParams.setMargins(2, 2, 2, 2);
        tableRowParams.weight = 1;

        for (i = 0; i < rowCount; i++) {

            TableRow tableRow = new TableRow(this);

           tableRow.setBackgroundResource(R.drawable.bookshelf);

            for (j= 0; j < columnCount; j++) {

                final ImageView imageView = new ImageView(this);
                imageView.setImageResource(R.drawable.businessman);

                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(), "position : " + rowCount, Toast.LENGTH_SHORT).show();
                    }
                });

                tableRow.addView(imageView, tableRowParams);

            }

            // 6) add tableRow to tableLayout
            tableLayout.addView(tableRow, tableLayoutParams);
        }

        return tableLayout;
    }
}

提前致谢。

【问题讨论】:

  • 你的位置是什么?或任何其他错误消息?
  • 位置 3 点击每个 Image.so 我想要实际位置
  • 最好使用GridView
  • 如何使用gridview 我想要书架视图里面的数据请给我解决方案
  • 动态为每一个图像视图设置标签和ID,并尝试获取标签imageview.onClick及其适当的数据或位置。

标签: android dynamic android-tablelayout


【解决方案1】:

您可以通过 GridView 实现这一点。请参阅下面的示例代码。

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_layout);
        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                Toast.makeText(TestActivity.this, "" + position,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return 9;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                // if it's not recycled, initialize some attributes
                imageView = new ImageView(TestActivity.this);
                imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } else {
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(R.drawable.emoticons_bear);
            return imageView;
        }
    }

你的布局应该是;

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/gridview"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:columnWidth="90dp"
          android:numColumns="auto_fit"
          android:verticalSpacing="10dp"
          android:horizontalSpacing="10dp"
          android:stretchMode="columnWidth"
          android:gravity="center"
    />

【讨论】:

    【解决方案2】:

    好吧,最好使用 GridView。但如果您只需要 TableLayout,您可以使用“final”在循环中修复您的 i,j 值:

    final int row = i;
    final int col = j;
    imageView.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               Toast.makeText(getApplicationContext(), "row : " + row + "; col : " + col, Toast.LENGTH_SHORT).show();
           }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      • 1970-01-01
      相关资源
      最近更新 更多