1

我从这个站点下载了一个自定义表格。我想显示我单击的单元格的值。

TableFixHeaders table = (TableFixHeaders) findViewById(R.id.table_1);
table.setAdapter(new MyAdapter(this));   
table.setOnLongClickListener(new OnLongClickListener(){
        @Override
    public boolean onLongClick(View v) {
        TextView Cl = (TextView) table.getChildAt(1);
        Toast.makeText(this, Cl.getText().toString(), Toast.LENGTH_LONG).show();
                return false;
        }
});

问题是我无法拦截主要活动中的任何事件。

4

3 回答 3

0

我还下载了这个带有滚动列和行的非常好的小部件。而且我还需要在某个单元格上单击得到响应。没来,但我在这里找到了原因:
TableFixHeaders.java, moduleOnTouchEvent(MotionEvent event)开头添加这一行:

super.onTouchEvent(event);  

此调用将确保调用其他触摸方法!所以它也应该适用于长按!当 convertView 第一次膨胀时,
我已将我的点击处理程序添加到模块中的特定视图(单元格) 。GetView(int row, int column, View convertView, ViewGroup parent)然后我能够对不同的单元格(列标题、数据单元格等)使用不同的处理程序。

于 2013-07-29T11:45:28.470 回答
0

下面是详细代码:

      private View getBody(final int row,final int column, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getLayoutInflater().inflate(R.layout.item_table, parent, false);
            }
        /*if(row == 8){
            convertView.setBackgroundResource(R.color.row8); //if row8 need different colour
        }*/else {
                //Change table color using bg_table_col
                convertView.setBackgroundResource(row % 2 == 0 ? R.drawable.bg_table_color1 : R.drawable.bg_table_color2);
            }
            ((TextView) convertView.findViewById(android.R.id.text1)).setText(records.get(row).getItem(column + 1));
            convertView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    detailsearchPaperID = records.get(row).getItem(0);
                    final String S;
                    S = detailsearchPaperID + " : " + records.get(row).getItem(column + 1);
                    Log.d(TAG, S);
                    Toast.makeText(getApplicationContext(), S, Toast.LENGTH_SHORT).show();
                    //...
                    // we will not start intent here but after performing search
                    return true;
                }
            });

            return convertView;
        }

如果您按照上一篇文章中的说明添加侦听器(当 convertView 第一次膨胀时),这将仅适用于第一个视图,然后再执行任何滚动。

即使您执行了滚动,我的代码也可以工作。当然需要添加

    super.onTouchEvent(event);

如前所述

于 2015-06-18T15:46:08.987 回答
0

I'm using this wrapper for InqBarna's library.

This is a code snippet of how to achieve the click events on table body. You can see more ellaborated examples here.

    TableFixHeaderAdapter.ClickListener<List<String>, BasicCellViewGroup> clickListenerBody = new TableFixHeaderAdapter.ClickListener<List<String>, BasicCellViewGroup>() {
        @Override
        public void onClickItem(List<String> array, BasicCellViewGroup viewGroup, int row, int column) {
            viewGroup.vg_root.setBackgroundColor(ContextCompat.getColor(context, R.color.colorYellow));
            Snackbar.make(viewGroup, "Click on " + viewGroup.textView.getText() + " (" + row + "," + column + ")", Snackbar.LENGTH_SHORT).show();
        }
    };

and so on for the rest of the table cells:

    adapter.setClickListenerFirstHeader(clickListenerHeader);
    adapter.setLongClickListenerFirstHeader(longClickListenerHeader);
    adapter.setClickListenerHeader(clickListenerHeader);
    adapter.setLongClickListenerHeader(longClickListenerHeader);
    adapter.setClickListenerFirstBody(clickListenerBody);
    adapter.setLongClickListenerFirstBody(longClickListenerBody);
    adapter.setClickListenerBody(clickListenerBody);
    adapter.setLongClickListenerBody(longClickListenerBody);
    adapter.setClickListenerSection(clickListenerBody);
    adapter.setLongClickListenerSection(longClickListenerBody);

Hope it helps you.

于 2016-03-17T15:34:31.920 回答