- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试基于 Google Play 商店中名为 Timetable 的应用程序创建一个应用程序。在过去的几天里,我一直在努力解决一个看似简单的问题。基本上,我使用 TableLayout 创建了一个网格,我希望一个单元格在触摸时突出显示,然后在再次触摸时切换 Activity 。但是,当我选择另一个单元格时,我根本不知道如何使先前突出显示的单元格停止突出显示。我正在尝试在抽屉导航 Activity 内的 fragment 上完成。
This is my work. As you can see, two cells are highlighted at once and I don't want that.
我尝试将我的单元格从 imageviews 更改为 imagebuttons 和常规按钮,但我得到了相同的结果。我实现了一个选择器 .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));
}
});
}
}
}
}
}
}
最佳答案
I have updated your code, see below..
public class WeekFragment extends Fragment {
//Create a global View instance like below
View previousView;
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));
//Check for the first time for null value
if(previousView!=null&&previousView.equals(b)){
/*You can make cell normal here by adding b.setBackgroundResource(R.drawable.normal);*/
switchActivities();
}
previousView=null;
//Assign new view
previousView=b;
}
});
}
}
}
}
}
}
关于Android:仅突出显示一个单元格 - TableLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51509132/
有没有一种方法可以“标记”对象的属性,使它们在反射中“突出”? 例如: class A { int aa, b; string s1, s2; public int AA
我是一名优秀的程序员,十分优秀!