小编Mic*_*551的帖子

StaggeredGridLayoutManager重新排序项目

我的项目有不同的布局,有时我通过拖动布局中的项目在RecyclerView上创建一个间隙或间隙.当我滚动回到RecyclerView的顶部时,一些项目被重新排序并且空隙被它们填满.此处捕获行为:

滚动到顶部时自动重新排序

填补空白是可以的.当项目在拖动期间改变位置时也可以.问题在于滚动到顶部.滚动结束时发生重新排序 - 我没有触摸屏幕.

这就是我如何创建回收和布局管理器

    recyclerView = (RecyclerView)layout.findViewById(R.id.recycler_view);

    stagaggeredGridLayoutManager =
        new StaggeredGridLayoutManager(
            getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE ? 3 : 2, StaggeredGridLayoutManager.VERTICAL);

    recyclerView.setLayoutManager(stagaggeredGridLayoutManager);

    stagaggeredList = getListItemData();

    StatisticsGridViewAdapter rcAdapter = new StatisticsGridViewAdapter(this.getActivity(), stagaggeredList, recyclerView);

    ItemTouchHelper.Callback callback = new StatisticsTouchHelperCallback(rcAdapter);
    touchHelper = new ItemTouchHelper(callback);
    touchHelper.attachToRecyclerView(recyclerView);

    recyclerView.setAdapter(rcAdapter);
Run Code Online (Sandbox Code Playgroud)

ItemTouchHelper类

public class StatisticsTouchHelperCallback extends ItemTouchHelper.Callback {

    private final ItemTouchHelperAdapter touchHelperAdapter;


    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

    @Override
    public boolean isItemViewSwipeEnabled() {
        return false;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlags = …
Run Code Online (Sandbox Code Playgroud)

android android-layout staggeredgridlayout android-recyclerview

15
推荐指数
1
解决办法
1817
查看次数

协程内部的通用泛型参数不起作用

我正在创建http json客户端。我将Volley与协程结合使用。我想创建通用的HTTP客户端,以便可以在任何地方使用它。

我创建了通用扩展方法来将JSON字符串解析为对象。

inline fun <reified T>String.jsonToObject(exclusionStrategy: ExclusionStrategy? = null) : T {
val builder = GsonBuilder()

if(exclusionStrategy != null){
    builder.setExclusionStrategies(exclusionStrategy)
}

return builder.create().fromJson(this, object: TypeToken<T>() {}.type)
Run Code Online (Sandbox Code Playgroud)

}

问题是,当我调用此方法时,无法得到预期的结果。第一次通话会给出正确的结果。对象已初始化。但是第二次调用(我使用传递给方法的通用参数)以异常“ LinkedTreeMap无法转换为令牌”结束。

    protected inline fun <reified T>sendRequestAsync(endpoint: String, data: Any?, method: Int, token: Token?): Deferred<T> {
    return ioScope.async {
        suspendCoroutine<T> { continuation ->
            val jsonObjectRequest = HttpClient.createJsonObjectRequest(
                endpoint,
                data?.toJsonString(),
                method,
                Response.Listener {
                    //this call is successful and object is initialized
                    val parsedObject : HttpResponse<Token> = it.toString().jsonToObject()

                    //this call is not successful and …
Run Code Online (Sandbox Code Playgroud)

java generics coroutine kotlin

6
推荐指数
1
解决办法
211
查看次数

Android Studio 与模拟器失去连接

最近我在设置 Android Studio 和模拟器时遇到了问题。我将 Windows 和 Nvidia 驱动程序更新到最新版本,很幸运能够使其再次部分“工作”。我仍然遇到与 Android Studio 捆绑在一起的 Android 模拟器的问题。

问题

当我长按控制模拟器时,我遇到了模拟器崩溃的问题。例如:

  • 拖动项目
  • 滚动
  • 旋转相机

在这些情况下,Android Studio“关闭”模拟器并显示“No Running Emulators”。我不认为模拟器本身已经崩溃,因为在我从模拟器窗口中丢失日志后,我可以在 Logcat 中看到几秒钟的日志,但过了一会儿模拟器就断开了连接。

有时出现此问题后,我无法在模拟器中拖动/滑动,只能单击。如果我不拖动/滑动模拟器似乎运行稳定

这是一些日志。问题发生于2022-02-18 15:48:28,863

2022-02-18 15:38:56,436 [ 310476]   INFO - manager.EmulatorProcessHandler - Emulator: C:\Users\Nonko\AppData\Local\Android\Sdk\emulator\qemu\windows-x86_64\qemu-system-x86_64.exe: icmpv6 ICMP6_ECHO_REQUEST failed 
2022-02-18 15:40:15,045 [ 389085]   WARN - com.intellij.util.xmlb.Binding - no accessors for com.intellij.openapi.updateSettings.impl.pluginsAdvertisement.PluginAdvertiserExtensionsStateService$State 
2022-02-18 15:40:15,045 [ 389085]   WARN - com.intellij.util.xmlb.Binding - no accessors for com.intellij.openapi.updateSettings.impl.pluginsAdvertisement.PluginAdvertiserExtensionsStateService$State 
2022-02-18 15:48:07,459 [ 861499]   INFO - manager.EmulatorProcessHandler - Emulator: WARNING | getAttribLocation: Program attrib …
Run Code Online (Sandbox Code Playgroud)

android android-emulator android-studio

5
推荐指数
1
解决办法
2081
查看次数

Android自定义编辑文本值被另一个自定义编辑文本更改

介绍

在我的一个项目中,我尝试创建带有标题和一些自定义验证的自定义 EditText。当我通过屏幕旋转和活动娱乐来测试此自定义视图时,我遇到了一个奇怪的问题。

有什么问题

休闲前

当应用程序启动时,所有编辑文本都具有从活动静态设置的正确值。如下图所示:

在此输入图像描述

休闲后

当我旋转屏幕或重新创建活动后,EditText 的值将变得混乱。CustomEditText 值设置为 XML 中上次编辑文本的值。简单(基本 Android EditText)编辑文本值正常设置。

在此输入图像描述

代码

我从出现此问题的项目中复制了代码。

主要活动

class MainActivity : AppCompatActivity() {

     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)

         first_custom_edit_text.header = "First header"
         first_custom_edit_text.setText("First text")

         third_custom_edit_text.header = "Third header"
         third_custom_edit_text.setText("Third text")

         first_simple_edit_text.setText("First simple - Not affected")

         second_custom_edit_text.header = "Second header"
         second_custom_edit_text.setText("Second text")

         second_simple_edit_text.setText("Second simple - Not affected")
     }
}
Run Code Online (Sandbox Code Playgroud)

自定义编辑文本

class CustomEditText : LinearLayout {
    fun setText(value: String?){
        this.input_edit_text.text = Editable.Factory.getInstance().newEditable(value ?: "")
    }

    fun getText(): String {
        return this.input_edit_text.text.toString() …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-view kotlin

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