Android:仅突出显示一个单元格-TableLayout

时间:2018-07-25 00:03:24

标签: android android-tablelayout

我一直试图在Google Play商店中基于时间表创建应用程序。在过去的几天里,我一直在努力解决一个看起来很简单的问题。基本上,我使用TableLayout创建了一个网格,我希望在触摸时突出显示一个单元格,然后在再次触摸时切换活动。但是,我根本不知道如何在选择另一个单元格时使先前突出显示的单元格停止突出显示。我正在尝试完成导航抽屉活动中的一个片段。

This is how Timetable does it. When a cell is clicked, it is highlighted. When another cell is touched, the previous cell is no longer highlighted. When a highlighted cell is tapped, or a cell that is in the same row as a highlighted cell is tapped, the activity switches.

This is my work. As you can see, two cells are highlighted at once and I don't want that.

我尝试将单元格从图像视图更改为图像按钮和常规按钮,但结果却相同。我实现了一个选择器.xml文件,该单元将在我触摸它时保持突出显示状态,或者将执行我的工作屏幕截图中显示的操作。如果有人可以帮助我,那将是惊人的。我是否需要考虑使用GridLayout?

这是fragment_week.xml (简称为节省空间)

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

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:stretchColumns="*">

        <TableRow
            android:id="@+id/titleRow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_gravity="center"
            android:orientation="horizontal"
            android:padding="2dp">

            <TextView
                android:layout_width="10dp"
                android:text="" />

            <TextView
                android:layout_width="20dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="Sun"
                android:textAppearance="@android:style/TextAppearance.Material.Subhead"
                android:textSize="15sp"
                tools:targetApi="lollipop" />

            <TextView
                android:layout_width="20dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:text=" Mon"
                android:textAppearance="@android:style/TextAppearance.Material.Subhead"
                android:textSize="15sp"
                tools:targetApi="lollipop" />

            <TextView
                android:layout_width="20dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:text=" Tue"
                android:textAppearance="@android:style/TextAppearance.Material.Subhead"
                android:textSize="15sp"
                tools:targetApi="lollipop" />

            <TextView
                android:layout_width="20dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:text=" Wed"
                android:textAppearance="@android:style/TextAppearance.Material.Subhead"
                android:textSize="15sp"
                tools:targetApi="lollipop" />

            <TextView
                android:layout_width="20dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:text=" Thu"
                android:textAppearance="@android:style/TextAppearance.Material.Subhead"
                android:textSize="15sp"
                tools:targetApi="lollipop" />

            <TextView
                android:layout_width="20dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:text=" Fri"
                android:textAppearance="@android:style/TextAppearance.Material.Subhead"
                android:textSize="15sp"
                tools:targetApi="lollipop" />

            <TextView
                android:layout_width="20dp"
                android:layout_gravity="center"
                android:gravity="center"
                android:text=" Sat"
                android:textAppearance="@android:style/TextAppearance.Material.Subhead"
                android:textSize="15sp"
                tools:targetApi="lollipop" />
        </TableRow>

    </TableLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
        android:scrollbars="none">

        <TableLayout
            android:id="@+id/timeTable"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:stretchColumns="1,2,3,4,5,6,7">


            <TableRow
                android:id="@+id/sevenAM"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/cell_shape">

                <TextView
                    android:layout_width="45dp"
                    android:layout_height="match_parent"
                    android:background="@drawable/cell_shape"
                    android:gravity="end"
                    android:paddingRight="8dp"
                    android:paddingTop="2dp"
                    android:text="7:00\nAM"
                    android:textAppearance="@android:style/TextAppearance.Material.Subhead"
                    android:textSize="11sp"
                    tools:targetApi="lollipop" />

                <ImageView
                    android:layout_height="45dp"
                    android:layout_width="1dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:layout_width="1dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:layout_width="1dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:layout_width="1dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:layout_width="1dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:layout_width="1dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:layout_width="1dp"
                    android:background="@drawable/cell_shape" />
            </TableRow>


            <TableRow
                android:id="@+id/eightAM"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/cell_shape">

                <TextView
                    android:layout_width="45dp"
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape"
                    android:gravity="end"
                    android:paddingRight="8dp"
                    android:paddingTop="2dp"
                    android:text="8:00\nAM"
                    android:textAlignment="textEnd"
                    android:textAppearance="@android:style/TextAppearance.Material.Subhead"
                    android:textSize="11sp"
                    tools:targetApi="lollipop" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />
            </TableRow>

            <TableRow
                android:id="@+id/nineAM"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/cell_shape">

                <TextView
                    android:layout_width="45dp"
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape"
                    android:gravity="end"
                    android:paddingRight="8dp"
                    android:paddingTop="2dp"
                    android:text="9:00\nAM"
                    android:textAlignment="textEnd"
                    android:textAppearance="@android:style/TextAppearance.Material.Subhead"
                    android:textSize="11sp"
                    tools:targetApi="lollipop" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />
            </TableRow>

            <TableRow
                android:id="@+id/tenAM"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/cell_shape">

                <TextView
                    android:layout_width="45dp"
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape"
                    android:gravity="end"
                    android:paddingRight="8dp"
                    android:paddingTop="2dp"
                    android:text="10:00\nAM"
                    android:textAlignment="textEnd"
                    android:textAppearance="@android:style/TextAppearance.Material.Subhead"
                    android:textSize="11sp"
                    tools:targetApi="lollipop" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />
            </TableRow>

            <TableRow
                android:id="@+id/elevenAM"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/cell_shape">

                <TextView
                    android:layout_width="45dp"
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape"
                    android:gravity="end"
                    android:paddingRight="8dp"
                    android:paddingTop="2dp"
                    android:text="11:00\nAM"
                    android:textAlignment="textEnd"
                    android:textAppearance="@android:style/TextAppearance.Material.Subhead"
                    android:textSize="11sp"
                    tools:targetApi="lollipop" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />
            </TableRow>


            <TableRow
                android:id="@+id/twelvePM"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/cell_shape">

                <TextView
                    android:layout_width="45dp"
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape"
                    android:gravity="end"
                    android:paddingRight="8dp"
                    android:paddingTop="2dp"
                    android:text="12:00\nPM"
                    android:textAlignment="textEnd"
                    android:textAppearance="@android:style/TextAppearance.Material.Subhead"
                    android:textSize="11sp"
                    tools:targetApi="lollipop" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />

                <ImageView
                    android:layout_height="45dp"
                    android:background="@drawable/cell_shape" />
            </TableRow>
        </TableLayout>
    </ScrollView>
</FrameLayout>

这是cell_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <stroke android:width="0.05dp"  android:color="#19aaa9a9"/>
</shape>

这是cell_hightlight.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:shape="rectangle" >
            <solid android:color="@color/colorAccent"/>
        </shape>
    </item>

    <item android:drawable="@drawable/plus" />
</layer-list>

这是WeekFragment.java

package com.dmelton.classScheduler;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;

public class WeekFragment extends Fragment {

    public WeekFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_week, container, false);
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        TableLayout timeTable = getActivity().findViewById(R.id.timeTable);
        int count = timeTable.getChildCount();
        for (int i = 0; i < count; i++) {
            View v = timeTable.getChildAt(i);
            if (v instanceof TableRow) {
                TableRow row = (TableRow) v;
                int rowCount = row.getChildCount();
                for (int r = 0; r < rowCount; r++) {
                    final View v2 = row.getChildAt(r);
                    if (v2 instanceof ImageView) {
                        final ImageView b = (ImageView) v2;

                    b.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            b.setBackground(getResources().getDrawable(R.drawable.cell_highlight));
                        }
                    });
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

|      bill_id |      bill_desc |
|--------------|----------------|
| 2018AA000001 | Telephone Bill |
| 2018AA000002 |     Water Bill |