CheckView的CheckedTextView自定义布局
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CheckView的CheckedTextView自定义布局相关的知识,希望对你有一定的参考价值。
我正在尝试为ListView行创建一个布局,其中包含带有几个TextView的CheckedTextView。我将这些视图包装在RelativeLayout中。运行应用程序导致没有检查任何内容。
我试图寻找解决方案,看来如果CheckedTextView不是布局的顶级视图,您可能需要创建一个实现Checkable接口的自定义布局。我尝试按照here的说明,但没有运气。在我的例子中,我试图设置文本的TextViews在我的ArrayAdapter中总是为null,所以我显然没有做对。
知道我做错了什么吗?有没有人有一个适合我可以遵循的情景的例子?
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<LinearLayout
android:id="@+id/checkedTextViewLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:layout_margin="8dp">
<CheckedTextView
android:id="@+id/checkedTextView"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" />
</LinearLayout>
<LinearLayout
android:id="@+id/headingLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="@id/checkedTextViewLayout"
android:layout_alignParentStart="true"
android:layout_margin="8dp"
android:layout_toStartOf="@id/checkedTextViewLayout"
android:orientation="vertical">
<TextView
android:id="@+id/heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="@+id/subheading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
custom layout.Java
public class CustomLayout extends RelativeLayout implements Checkable {
public CustomLayout(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initializeViews(context, attrs);
}
private void initializeViews(Context context, AttributeSet attrs) {
LayoutInflater.from(context).inflate(R.layout.custom_layout, this);
checkedTextView = this.findViewById(R.id.checkedTextView);
heading = this.findViewById(R.id.heading);
subheading = this.findViewById(R.id.subheading);
}...
}
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<com.example.android.example.CustomLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/custom_layout_id">
</com.example.android.example.CustomLayout>
custom array adapter.Java
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_row, parent, false);
viewHolder.customeLayout = convertView.findViewById(R.id.custom_layout_id);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (data != null) {
viewHolder.customLayout.getHeading().setText("Order # " + data.getOrderNumber());
viewHolder.customLayout.getSubheading().setText("Subheading");
}
答案
试试这个:-
custom_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
android:padding="8dp">
<CheckedTextView
android:id="@+id/checkedTextView"
android:layout_width="wrap_content"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:layout_margin="8dp"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:textAppearance="?android:attr/textAppearanceListItemSmall" />
<LinearLayout
android:id="@+id/headingLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_margin="8dp"
android:layout_toStartOf="@id/checkedTextView"
android:orientation="vertical">
<TextView
android:id="@+id/heading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="@+id/subheading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
</RelativeLayout>
CustomLayout.java:[我从这个链接得到了想法:Android Checkable LinearLayout states
public class CustomLayout extends RelativeLayout implements Checkable {
private CheckedTextView checkedTextView;
private TextView heading;
private TextView subheading;
public CustomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
setClickable(true);
initializeViews(context, attrs);
}
private void initializeViews(Context context, AttributeSet attrs) {
LayoutInflater.from(context).inflate(R.layout.custom_layout, this);
checkedTextView = this.findViewById(R.id.checkedTextView);
heading = this.findViewById(R.id.heading);
subheading = this.findViewById(R.id.subheading);
}
public TextView getHeading() {
return heading;
}
public TextView getSubheading() {
return subheading;
}
@Override
public void setChecked(boolean b) {
checkedTextView.setChecked(b);
}
@Override
public boolean isChecked() {
return checkedTextView.isChecked();
}
@Override
public void toggle() {
checkedTextView.setChecked(!checkedTextView.isChecked());
}
@Override
public boolean performClick() {
// Toast.makeText(getContext(),"click",Toast.LENGTH_SHORT).show();
toggle();
return super.performClick();
}
}
希望有所帮助!
以上是关于CheckView的CheckedTextView自定义布局的主要内容,如果未能解决你的问题,请参考以下文章
在 ListView 中滚动时 CheckedTextView 消失
以编程方式选择时,CheckedTextView 未绘制复选标记
Android基础到进阶UI CheckedTextView 使用+实例