简体   繁体   English

CheckView的CheckedTextView自定义布局

[英]CheckedTextView Custom Layout for ListView

I am trying to create a layout for a ListView row which contains a CheckedTextView with a couple of TextViews. 我正在尝试为ListView行创建一个布局,其中包含带有几个TextView的CheckedTextView。 I wrapped these views in a RelativeLayout. 我将这些视图包装在RelativeLayout中。 Running the application resulted in nothing being checked. 运行应用程序导致没有检查任何内容。

I tried looking around for a solution and it appears that if the CheckedTextView is not the top level view of the layout, you might need to create a custom Layout that implements the Checkable interface. 我试图寻找解决方案,看来如果CheckedTextView不是布局的顶级视图,您可能需要创建一个实现Checkable接口的自定义布局。 I tried following the instructions here but with no luck. 我试着遵循这里的说明但没有运气。 In my case, the TextViews I am trying to set text on are always null in my ArrayAdapter so I am obviously not doing it right. 在我的例子中,我试图设置文本的TextViews在我的ArrayAdapter中总是为null,所以我显然没有做对。

Any idea what I am doing wrong? 知道我做错了什么吗? Does anyone have an example that fits my scenario that I could follow? 有没有人有一个适合我可以遵循的情景的例子?

custom_layout.xml 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>

CustomLayout.java CustomLayout.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 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>

CustomArrayAdapter.java CustomArrayAdapter.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");
    }

Try this:- 尝试这个:-

custom_layout.xml: 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: [I got the idea from this link: Android Checkable LinearLayout states CustomLayout.java:[我从这个链接中得到了一个想法: Android Checkable LinearLayout状态

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();
}

}

Hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM