我正在尝试实现一个可检查的线性布局,我已经完成了选中的项目,但是现在 onclick 和 onLongClick 它不起作用了,它应该有什么想法可以实现这种行为。
- 如果未选中 && OnLongClick = 检查
- 如果选中 && onClick = 取消选中
- 如果选中 && onLongClick = 取消选中
- 如果未选中 && onClick = super.performClick()
我的可检查线性布局
public class CheckableLinearLayout extends LinearLayout implements Checkable {
/**
* Interface definition for a callback to be invoked when the checked state
* of this View is changed.
*/
public static interface OnCheckedChangeListener {
/**
* Called when the checked state of a compound button has changed.
*
* @param checkableView
* The view whose state has changed.
* @param isChecked
* The new checked state of checkableView.
*/
void onCheckedChanged(View checkableView, boolean isChecked);
}
private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked };
private boolean mChecked = false;
private OnCheckedChangeListener mOnCheckedChangeListener;
public CheckableLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public boolean isChecked() {
return mChecked;
}
public void setChecked(boolean b) {
if (b != mChecked) {
mChecked = b;
refreshDrawableState();
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
}
}
}
public void toggle() {
setChecked(!mChecked);
}
@Override
public int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
}
/**
* Register a callback to be invoked when the checked state of this view
* changes.
*
* @param listener
* the callback to call on checked state change
*/
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
mOnCheckedChangeListener = listener;
}
@Override
public boolean performLongClick() {
toggle();
return super.performLongClick();
}
@Override
public boolean performClick() {
if (isChecked()) {
toggle();
return false;
} else
return super.performClick();
}
我的状态可绘制 xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/add_schedule_button_checked_pressed" android:state_checked="true" android:state_pressed="true"/>
<!-- <item android:drawable="@drawable/add_schedule_button_checked_focused" -->
<!-- android:state_checked="true" -->
<!-- android:state_focused="true" /> -->
<item android:drawable="@drawable/selected_card_shape_w_shadow_white" android:state_checked="true"/>
<!-- <item android:drawable="@drawable/add_schedule_button_unchecked_pressed" -->
<!-- android:state_pressed="true" /> -->
<!-- <item android:drawable="@drawable/add_schedule_button_unchecked_focused" -->
<!-- android:state_focused="true" /> -->
我的布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/topics_preview_main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/favorites_header_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="2dp"
android:orientation="vertical" >
<TextView
android:id="@+id/favorites_header_title"
style="@style/CardText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:background="@drawable/card"
android:gravity="left"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:text="@string/favorites_header" />
<ui.CheckableLinearLayout
android:id="@+id/favorites_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="4dp"
android:background="@drawable/selector_card_shape_w_shadow_white"
android:clickable="true"
android:longClickable="true"
android:orientation="vertical"
android:paddingBottom="16dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<TextView
android:id="@+id/favorites_title"
style="@style/CardTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="title" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="4dp"
android:background="@color/C_Favorites_Pink" />
<LinearLayout
android:id="@+id/favorites_description_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="4dp" >
<TextView
android:id="@+id/favorites_description"
style="@style/CardText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:ellipsize="end"
android:maxLines="4"
android:text="Lorem ipsum dolor sit amet" />
</LinearLayout>
</ui.CheckableLinearLayout>
</LinearLayout>
我的片段
private void onFavoriteClick(final MCourse courseInfo,
LinearLayout favoritesLayout) {
favoritesLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String courseId = Integer.toString(v.getId());
String courseName = courseInfo.getFullname();
Bundle bundle = new Bundle();
bundle.putString("courseId", courseId);
bundle.putString("courseName", courseName);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
TopicsPreview insideTopicsFrag = new TopicsPreview();
insideTopicsFrag.setArguments(bundle);
fragmentTransaction
.replace(R.id.mainFragment, insideTopicsFrag);
fragmentTransaction.commit();
}
});
}