【问题标题】:Android how to detect which tablerow was clickedAndroid如何检测点击了哪个tablerow
【发布时间】:2015-02-03 15:16:17
【问题描述】:

在我的片段布局中,我有一个这样的表格布局:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TableLayout
            android:id="@+id/tableLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

        </TableLayout>
    </LinearLayout>

在这个表格布局中,我以编程方式添加多个表格行,其中包含这样的视图:

for(int i = 0; i < 5;){
    tableLayout.addView(createTableRow((LocationObject)objectList.get(i)), i);
}

-

private View createTableRow(LocationObject locObject) {

    TableRow tr = new TableRow(getActivity());
    View v = LayoutInflater.from(getActivity()).inflate(R.layout.view_layout, tr, false);

    TextView textViewName = (TextView) v.findViewById(R.id.textView_name);
             textViewName.setText(locObject.getTitle());

    TextView textViewProvider = (TextView) v.findViewById(R.id.textView_provider);
             textViewProvider.setText(locObject.getSubTitle());

    return v;
}

在我调用 createTableRow 方法几次并用行填充 tablelayout 之后,我想检测用户何时单击行。 如何为行指定不同的 id,例如第一个获取 id 0,第二个获取 1 等。最后如何检测用户何时单击行?

编辑: 我尝试了 setOnClickListener,但是当我单击视图时它不起作用,logcat 中永远不会显示“可访问”的消息。

private View createTableRow(LocationObject locObject) {

TableRow tr = new TableRow(getActivity());
View v = LayoutInflater.from(getActivity()).inflate(R.layout.view_layout, tr, false);

...
v.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.e("output", "is reachable");
        }

    });

return v;
}

【问题讨论】:

  • 您是否尝试在您的createTableRow 中添加onClickListener
  • @Marcus 是的,但它不起作用。

标签: android android-tablelayout clickable


【解决方案1】:

首先不要忘记i++for(int i = 0; i &lt; 5; i++)

你可以试试这样的:

private View createTableRow(int position) {

    //instead
    //TableRow tr = new TableRow(MainActivity.this);
    //View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_layout, tr, false);

    //try
    View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.view_layout, null, false);

    TextView textViewName = (TextView) v.findViewById(R.id.textView_name);
    textViewName.setText("row "+ position);

    v.setTag(position);
    v.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            int position = (Integer) v.getTag();
            Log.e("output", "is reachable at position "+ position);
        }

    });

    return v;
}

点击输出:

02-04 09:36:13.615    4755-4755/noambaroz.testapplication E/output﹕ is reachable at position 0
02-04 09:36:14.680    4755-4755/noambaroz.testapplication E/output﹕ is reachable at position 2
02-04 09:36:15.310    4755-4755/noambaroz.testapplication E/output﹕ is reachable at position 4

【讨论】:

    猜你喜欢
    • 2022-01-26
    • 1970-01-01
    • 2016-02-08
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 2016-03-28
    相关资源
    最近更新 更多