- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有以下代码将 SwitchCompat
与 RecyclerView
一起使用。
列出 SwitchCompat
所在的项目布局:fragment_notification_settings_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="65dp"
android:background="@android:color/white">
<ImageView
android:id="@+id/icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="15dp"
android:src="@drawable/settings_about"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_centerVertical="true"
android:tint="@color/Black"
tools:ignore="ContentDescription"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/icon"
android:layout_toStartOf="@+id/on_off_switch"
android:layout_centerVertical="true"
android:orientation="vertical">
<TextView
android:id="@+id/item_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingStart="15dp"
android:paddingEnd="0dp"
android:text="Kaza Takibi"
android:textSize="18sp"
android:textColor="@color/Black"
tools:ignore="HardcodedText"/>
<TextView
android:id="@+id/item_description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingStart="15dp"
android:paddingEnd="0dp"
android:text="Ezan vakitlerinde namaz kılma uyarısı"
android:textSize="16sp"
android:textColor="@color/DarkGray"
tools:ignore="HardcodedText"/>
</LinearLayout>
<android.support.v7.widget.SwitchCompat
android:id="@+id/on_off_switch"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
fragment 类:
public class NotificationSettingsFragment extends Fragment{
@Bind(R.id.close_button)
ImageView closeButtonImageView;
@Bind(R.id.recycler_view)
RecyclerView recyclerView;
private MainActivity activity;
private Context context;
private Fragment parent;
private NotificationSettingsAdapter adapter;
public NotificationSettingsFragment() {}
public void setParent(Fragment parent) {
this.parent = parent;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = (MainActivity)getActivity();
context = activity.getApplicationContext();
List<NotificationSettingsItem> settingsItems = new ArrayList<>();
settingsItems.add(
new NotificationSettingsItem(
"Tercihler", "",
NotificationSettingsItem.ItemType.ITEM_TYPE_SECTION_HEADER));
settingsItems.add(
new NotificationSettingsItem(
"Kaza Takibi",
"Ezan bildirimlerinde namaz kılma uyarısı",
NotificationSettingsItem.ItemType.ITEM_TYPE_APPLICATION_SETTING));
settingsItems.add(
new NotificationSettingsItem(
"Konumumu Kullan",
"Namaz vakitleri için bulunduğum konumu kullan",
NotificationSettingsItem.ItemType.ITEM_TYPE_APPLICATION_SETTING));
adapter = new NotificationSettingsAdapter(context, settingsItems);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_notification_settings, container, false);
ButterKnife.bind(this, rootView);
closeButtonImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (parent != null) {
FragmentManager fm = activity.getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.top_container, parent);
ft.commit();
}
}
});
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(adapter);
return rootView;
}
private class NotificationSettingHolder extends RecyclerView.ViewHolder {
public TextView itemTitle;
public TextView itemDescription;
public SwitchCompat mSwitch;
public NotificationSettingsItem settingsItem;
public NotificationSettingHolder(View listItemRootView) {
super(listItemRootView);
mSwitch = (SwitchCompat) listItemRootView.findViewById(R.id.on_off_switch);
itemTitle = (TextView) listItemRootView.findViewById(R.id.item_title);
itemDescription = (TextView) listItemRootView.findViewById(R.id.item_description);
}
public void bindSetting(NotificationSettingsItem settingsItem){
this.settingsItem = settingsItem;
if (settingsItem.itemType == NotificationSettingsItem.ItemType.ITEM_TYPE_SECTION_HEADER) {
itemTitle.setText(settingsItem.itemTitle);
}
else if (settingsItem.itemType == NotificationSettingsItem.ItemType.ITEM_TYPE_APPLICATION_SETTING){
itemTitle.setText(settingsItem.itemTitle);
itemDescription.setText(settingsItem.itemDescription);
mSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});
mSwitch.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mSwitch.getParent().requestDisallowInterceptTouchEvent(true);
}
return false;
}
});
}
}
}
private class NotificationSettingsAdapter extends RecyclerView.Adapter<NotificationSettingHolder> {
private List<NotificationSettingsItem> settingsItems;
private LayoutInflater inflater;
public NotificationSettingsAdapter(Context context, List<NotificationSettingsItem> settingsItems) {
inflater = LayoutInflater.from(context);
this.settingsItems = settingsItems;
}
@Override
public NotificationSettingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == NotificationSettingsItem.ItemType.ITEM_TYPE_SECTION_HEADER.ordinal()) {
return new NotificationSettingHolder(
inflater.inflate(
R.layout.fragment_settings_list_section_header,
parent,
false));
}
else if (viewType == NotificationSettingsItem.ItemType.ITEM_TYPE_APPLICATION_SETTING.ordinal()) {
return new NotificationSettingHolder(
inflater.inflate(
R.layout.fragment_notification_settings_item,
parent,
false));
}
return null;
}
@Override
public void onBindViewHolder(NotificationSettingHolder holder, int position) {
holder.bindSetting(settingsItems.get(position));
}
@Override
public int getItemCount() {
return settingsItems.size();
}
@Override
public int getItemViewType(int position) {
NotificationSettingsItem item = settingsItems.get(position);
switch (item.itemType) {
case ITEM_TYPE_APPLICATION_SETTING:
return 1;
case ITEM_TYPE_SECTION_HEADER:
return 0;
default:
return -1;
}
}
}
}
当我尝试运行它时出现此错误:
09-09 17:00:07.842 30287-30287/com.yceo.islamatlasi E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.yceo.islamatlasi, PID: 30287
java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference
at android.text.StaticLayout.<init>(StaticLayout.java:49)
at android.support.v7.widget.SwitchCompat.makeLayout(SwitchCompat.java:606)
at android.support.v7.widget.SwitchCompat.onMeasure(SwitchCompat.java:526)
at android.view.View.measure(View.java:17570)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
at android.view.View.measure(View.java:17570)
at android.support.v7.widget.RecyclerView$LayoutManager.measureChildWithMargins(RecyclerView.java:5446)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1328)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1265)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:522)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1918)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2155)
at android.view.View.layout(View.java:15694)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
at android.view.View.layout(View.java:15694)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15694)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
at android.view.View.layout(View.java:15694)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1067)
at android.view.View.layout(View.java:15694)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15694)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15694)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15694)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15694)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15694)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5891)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(L
我该如何处理?
提前致谢。
最佳答案
SwitchCompat
要求您指定 android:textOn
和 android:textOff
例如,你可以这样改
<android.support.v7.widget.SwitchCompat
android:id="@+id/on_off_switch"
android:textOn="On"
android:textOff="Off"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"/>
关于android - SwitchCompat 在 RecyclerView 中给出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32481856/
我正在尝试使用 SwitchCompat,它显示在 Android Studio 的设计 View 中,但在我的 Galaxy S4(运行 5.0.1)上,它没有显示。这是相关代码: Activity
我的问题可能最好通过视觉方式提出——我想要一个 SwitchCompat切换到他们在 Android 设置应用中的样子: 这是关闭的: 这是在: 但出于某种原因,我的 SwitchCompat 开关在
我需要更改 SwitchCompat 的轨道颜色。我试过了 this ,但它对我没有用。这是我的 XML 文件的代码 这是我的 style.xml 文件 @color/red @c
使用列表和 map 数据演示开发应用程序。要在演示文稿之间切换,应使用带有图像的自定义切换,如下所示: 如何创建这样的自定义switchcompat? 最佳答案 这是一个很好的例子: ORIGINAL
昨天我切换到 Android Studio 2.2 Preview 1,我注意到所有 SwitchCompat View 周围有一些额外的线条,它们看起来变形了。 它的样式只包含宽度/高度参数和垂直中
我正在使用 android.support.v7.widget.SwitchCompat 并且遇到以下问题 我的包含 colorControlActivated 的样式不适用 使用 Android 命
我想在代码中打开或关闭 SwitchCompat 小部件。我的意思是当用户将 SwitchCompat 从 On 更改为 Off 或其他时。我想在代码中做到这一点。我该怎么做? SwitchCompa
我想为 SwitchCompat 应用自定义样式。更改打开和关闭状态的绘图和文本。我怎样才能做到这一点?我找不到任何关于如何做到这一点的例子。我在我的 styles.xml 中尝试了以下内容,但显然我
我正在尝试在我的 fragment 中使用 switchcompat。最小支持的 API 为 14,最大为 21。我正在尝试应用 Material View 来切换所有 pre lollipop an
目前我的 SwitchCompat看起来像下面的图像(顶部),但是我需要在关闭状态下有可见文本。 我将如何实现这一目标? 最佳答案 关于android - 显示以设置在 SwitchCompat
我在 RecyclerView 适配器中仅使用一个 SwitchCompat。我需要在 RecyclerView 上单击的项目的位置,并且当我使用 TextView 而不是 SwitchCompat
我在 ActionBar 中有一个 SwitchCompat 小部件。 当按下后退按钮并“重新打开”应用程序时,SwitchCompat 会丢失状态,从打开变为关闭。 我正在实现前台,但这并不能阻止
这是未经检查的样子: 并检查: checked 明显不符合设计,那么当它被选中时,我怎样才能让它看起来像未选中? 最佳答案 您可以为两个语句使用相同的颜色: switchButton.setO
在 Android 中,是否可以垂直对齐 SwitchCompat ,即将文本放在开关上方而不是开关左侧? 最佳答案 你在找这样的东西吗? 试试这个 关于android - A
我在将 Android.Support.V7.Widget.SwitchCompat 绑定(bind)到我的 View 模型时遇到了麻烦(Mvvmcross 是我正在使用的框架)我做了与在另一个对象上
我正在尝试以编程方式添加 android.support.v7.widget.SwitchCompat,但出现此错误: java.lang.NullPointerException: Attempt
我想在 Android Studio 中使用 SwitchCompat,我把它放在 build.gradle 中: compile 'com.android.support:appcompat-v7:
这是我的代码,对我不起作用。我使用来自 this 的代码示例。 XML- 和代码: mVisibilitySwitchCompat = (SwitchCompat) findViewById(R.i
我想减少 Switch 的宽度。当前解决方案在How to change the size of a Switch Widget和 How to change Width of Android's S
我在顶部有一个主要的服务开/关开关。我希望这个开关大而显眼。 如果我将 textOn 属性更改得更长一些,它在某种程度上是在做我想做的事情,但我希望这种情况发生在更短的 textOn 字符串长度上。
我是一名优秀的程序员,十分优秀!