简体   繁体   中英

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. I wrapped these views in a 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. 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.

Any idea what I am doing wrong? Does anyone have an example that fits my scenario that I could follow?

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

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>

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:

<?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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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