小编Joh*_*ive的帖子

无法从RecyclerView.OnScrollListener调用notifyItemInserted()

最近我升级recyclerview-v7:23到了recyclerview-v7:24.2.0.我的应用程序有一个无尽的滚动列表.notifyItemInserted当我将加载视图添加到RecyclerView(null对象表示加载,id 0为空,-1表示页面结束)时,错误消息指向该行,并且它之前工作正常(recyclerview-v7:23)但突然我得到错误像这样,不知怎的,我的加载出现两次,然后当它删除一个,有一个加载仍然可见在顶部.

    W/RecyclerView: Cannot call this method in a scroll callback. Scroll callbacks might be run during a measure & layout pass where you cannot change the RecyclerView data. Any method call that might change the structure of the RecyclerView or the adapter contents should be postponed to the next frame.java.lang.IllegalStateException:  
 at android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:2403)
     at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onItemRangeInserted(RecyclerView.java:4631)
     at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyItemRangeInserted(RecyclerView.java:10469)
     at android.support.v7.widget.RecyclerView$Adapter.notifyItemInserted(RecyclerView.java:6211)
     at com.sketchproject.infogue.fragments.MessageFragment.loadMessages(MessageFragment.java:109)
     at com.sketchproject.infogue.fragments.MessageFragment.access$100(MessageFragment.java:42)
     at com.sketchproject.infogue.fragments.MessageFragment$1.onLoadMore(MessageFragment.java:87)
     at com.sketchproject.infogue.modules.EndlessRecyclerViewScrollListener.onScrolled(EndlessRecyclerViewScrollListener.java:74)
Run Code Online (Sandbox Code Playgroud)

当我删除部分错误(添加加载)它再次正常工作,我不知道为什么,更新版本的recyclerview阻止将数据添加到适配器太快或有回调函数,当"测量和布局"时触发recyclerview完了,这是我的代码

private void loadArticles(final int …
Run Code Online (Sandbox Code Playgroud)

android recycler-adapter android-recyclerview

44
推荐指数
2
解决办法
2万
查看次数

具有空默认选定项目的微调器

我正在尝试使用默认的空选定项创建一个微调器,但它显示了微调器选项中的第一个项.如果我将空值添加到我的字符串(这是spinner中的选择源),则在打开微调器后显示空行.我该怎么办?这是我正在使用的代码:

  String[] ch = {"Session1", "Session2", "Session3"};
  Spinner sp = (Spinner)findViewById(R.id.spinner1);
  TextView sess_name = findViewById(R.id.sessname);
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,ch);
  sp.setAdapter(adapter);

  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

  sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener({
      @Override
      public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
          int index = arg0.getSelectedItemPosition();
          sess_name.setText(ch[index]);

          Toast.makeText(getBaseContext(), "You have selected item : " + ch[index], Toast.LENGTH_SHORT).show();
      }
Run Code Online (Sandbox Code Playgroud)

java android android-spinner

9
推荐指数
3
解决办法
3万
查看次数

Shortcut to generate "Created by.." template in Android Studio?

I have a project with a lot of classes in which I need to change the Created by template.

/**
 * Created by johnnyfivedev on 19.07.16.
 */
Run Code Online (Sandbox Code Playgroud)

Since there are a lot of such classes, copy and paste not an option. Rather I want to remove that generated template and regenerate it. Is there any default shortcut for doing this? If not, then how do I create one?

android intellij-idea android-studio

4
推荐指数
1
解决办法
856
查看次数

DateTime选择器FragmentManager异常

我已将GitHub中的以下库集成到我的项目中:

https://github.com/jjobes/SlideDateTimePicker

它已被很好地集成,但在执行时不起作用.
我创建了以下FragmetActivity,其中只有一个启动库的按钮.单击该按钮时,它会崩溃.活动设置为具有对话主题.

<activity
            android:name=".AlertDateTimePickerParentActivity"
            android:screenOrientation="landscape"
            android:theme="@style/Theme.AppCompat.Dialog"
            android:excludeFromRecents="true"/>
Run Code Online (Sandbox Code Playgroud)

FragmetActivity:

/**
 * Created by MatthewW on 27/04/2018.
 */

import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.github.jjobes.slidedatetimepicker.SlideDateTimeListener;
import com.github.jjobes.slidedatetimepicker.SlideDateTimePicker;


public class AlertDateTimePickerParentActivity extends FragmentActivity{


    private static final String TAG = AlertDateTimePickerParentActivity.class.getSimpleName();
    private SimpleDateFormat mFormatter = new SimpleDateFormat("MMMM dd yyyy hh:mm aa");
    private Button mButtonSetStartDate;



    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.alerdtparentactivitylayout);
        this.setFinishOnTouchOutside(false); //stops this activity being destroyed …
Run Code Online (Sandbox Code Playgroud)

android datepicker android-fragments android-fragmentactivity

4
推荐指数
1
解决办法
378
查看次数

设置EditText的最大长度

我想在EditText中设置1个字符的长度,但只是在条件的一部分.有一种方法可以通过XML设置最大长度,但这将适用于整个条件.如何以编程方式设置最大长度?

android maxlength

3
推荐指数
2
解决办法
1万
查看次数

如何将Android Studio 2.2 rc中的字体从粗体更改为普通

我下载了Android Studio 2.2rc,在这个版本中,默认代码样式看起来比较大胆(例如)2.1.我可以返回正常的字体样式并同时使用2.2rc吗?(您可以在此图片上看到字体的差异,但设置相同)

2.1

2.2

android android-studio

3
推荐指数
1
解决办法
340
查看次数

如何在 Kotlin 中的数据类上实现空对象模式?

我有一个 Kotlin 数据类:

data class PaymentAccount(
    val accountId: Int,
    val accountNumber: String,
    val title: String
)
Run Code Online (Sandbox Code Playgroud)

这就是我在 Java 中所做的:

创建一个抽象类:

public abstract class PaymentAccount {

    protected int accountId;
    protected String accountNumber;
    protected String title;

    public PaymentAccount(int accountId,
                          String accountNumber,
                          String title) {
        this.accountId = accountId;
        this.accountNumber = accountNumber;
        this.title = title;
    }
}
Run Code Online (Sandbox Code Playgroud)

创建空对象并扩展抽象类:

public class NullPaymentAccount extends PaymentAccount {

    public NullPaymentAccount() {
        super(-1,
                "Invalid account number",
                "Invalid title");
    }
}
Run Code Online (Sandbox Code Playgroud)

创建一个真实的对象并扩展抽象类:

public class RealPaymentAccount extends PaymentAccount {

    public RealPaymentAccount(int …
Run Code Online (Sandbox Code Playgroud)

design-patterns null-object-pattern kotlin

1
推荐指数
2
解决办法
3819
查看次数