【问题标题】:Making a 2D scrollable Table with clickable objects inside制作一个带有可点击对象的 2D 可滚动表格
【发布时间】:2013-03-05 20:40:33
【问题描述】:

我正在尝试制作一个充满项目的表格/网格。到目前为止,我已经按照Fuzzical Logic 的建议实现了GridView。这是image of the result。我可以双向滚动。

我的问题是,通过将新的OnTouchListener 设置为我的GridView(这可以双向滚动),我的项目不再可点击。我曾尝试从我的GridView 的onTouch 方法中调用OnItemClickListener,但我认为它太复杂了。

所以我现在问你,显示可双向滚动的可点击项目表的最佳方法是什么。 此外,项目的数量可以变化很大,列的数量也是如此。可以有“空”单元格。

我非常喜欢 GridView,因为它易于管理,但缺少 2D 滚动。有什么想法吗?

戈达克

【问题讨论】:

    标签: android click android-gridview android-tablelayout android-scroll


    【解决方案1】:

    我通过以下方式实现了一个可点击的表格:

        TableLayout table = (TableLayout) findViewById(R.id.tableReportsList);
        table.setStretchAllColumns(true);  
        table.setShrinkAllColumns(true);  
        final String tag = "tag";
    
        for (Report tempReport : reportsRepository)
        {
            TableRow row = new TableRow(this);
            TableLayout.LayoutParams tableRowParams=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
            tableRowParams.setMargins(0, 10, 0, 10);
            row.setLayoutParams(tableRowParams);
            row.setClickable(true);
            TextView tvName = new TextView(this);
            tvName.setBackgroundDrawable(getResources().getDrawable(R.drawable.table_row_shape));
            tvName.setText(tempReport.getName());
            tvName.setGravity(Gravity.CENTER_HORIZONTAL);
            tvName.setTextColor(getResources().getColor(R.color.my_black));  
            tvName.setTag(tag);
            row.addView(tvName);
            TextView tvPath = new TextView(this);
            tvPath.setBackgroundDrawable(getResources().getDrawable(R.drawable.table_row_shape));
            tvPath.setText(tempReport.getPath());
            tvPath.setGravity(Gravity.CENTER_HORIZONTAL);
            tvPath.setTextColor(getResources().getColor(R.color.my_black));
            row.addView(tvPath);
            map.put(tempReport.getName(), tempReport);
            row.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) 
                {   
                    TextView tvName = (TextView) v.findViewWithTag(tag);
                    Log.d(TAG, "looking for report with name: " + tvName.getText());
                    Report report = (Report)map.get(tvName.getText());
                    Log.d(TAG, "The report found: " + report.toString());
                    Map<String, String> valuesmap = new HashMap<String, String>();  
                    valuesmap.put(GetParametersAsyncTask.REPORTID_PARAM, report.getId());
                    Log.d(TAG, "passed the following id to the asynctask: "+ report.getId());
                    GetParametersAsyncTask asyncTask = new GetParametersAsyncTask(ReportsTableActivity.this, ((SGRaportManagerAppObj)getApplication()).getCurrentUrl());
                    asyncTask.execute(valuesmap);
                }
            });
            table.addView(row);   
        }
    

    而 xml 文件看起来像这样:

    <FrameLayout>
      <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="55dp" >
    
        <TableLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/tableReportsList"
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:shrinkColumns="*"  
            android:stretchColumns="*" >
    
            <TableRow
                android:layout_height="wrap_content"
                android:background="@color/light_gray"
                android:textStyle="bold" >
    
                <TextView
                    android:id="@+id/nameLabel"
                    android:layout_height="wrap_content"
                    android:background="@drawable/table_row_shape"
                    android:gravity="center_horizontal"
                    android:text="@string/report_name"
                    android:textColor="@color/my_black"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:textStyle="bold" />
    
                <TextView
                    android:id="@+id/pathLabel"
                    android:layout_height="wrap_content"
                    android:background="@drawable/table_row_shape"
                    android:gravity="center_horizontal"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:text="@string/report_path"
                    android:textColor="@color/my_black"
                    android:textStyle="bold" />
            </TableRow>
        </TableLayout>
    </ScrollView>
    

    看看你是否觉得它有帮助。

    【讨论】:

    • 感谢您的回复。不幸的是,水平滚动是不可能的。如果行太大,我想要水平滚动。
    • 但这就是它的工作方式,只有在屏幕太大时才会滚动视图。与水平滚动视图相同。你不能滚动一个视图,除非它有可以滚动的地方。
    • 对,因为那是我需要的,你谈到了水平滚动视图,我只是指出效果是一样的,只有当它有滚动的地方时,视图才会滚动。如果您希望能够水平滚动所有 TableLayout,只需使用 Horizo​​ntalScrollView 标签更改 ScrollView 标签。
    • 重点是:我希望能够双向滚动:水平和垂直。
    • 我从未尝试过,但您是否尝试将其放入位于 Horizo​​ntalScrollView 中的 ScrollView 中?
    猜你喜欢
    • 2010-12-31
    • 2013-01-25
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多