小编the*_*ger的帖子

在NestedScrollView中使用RecyclerView为所有项调用onBindView

我有两个RecyclerViews垂直放置在一个LinearLayout.我需要让它们都可以滚动,这就是我放入LinearLayout内部的原因NestedScrollView

这是我的布局文件.

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/featured_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/all_topic_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

另外,我在Java代码中禁用嵌套滚动.

disableNestedScrolling(findViewById(R.id.all_topic_list));
disableNestedScrolling(findViewById(R.id.featured_list));
Run Code Online (Sandbox Code Playgroud)

我的RecylerView库版本是26.1.0

这完全正常,但随后onBindViewHolder调用列表中的所有项目的方法.理想情况下,只应为列表中的可见项调用它.

我认为这个问题正在发生的事情,因为我给wrap_contentRecyclerView.关于这个问题的很多答案都表明这个问题在v23.2.1中得到了解决,但我已经在使用v26.1.0了.如何解决这个问题?

android android-recyclerview

17
推荐指数
1
解决办法
2108
查看次数

在Android中捕获网络数据包?

我正在开发一个项目,我需要捕获传入/传出的数据包并将它们存储在pcap文件中.

Android提供VpnService了这个目的,它已经在API级别14中添加了.尽管在SO上似乎有很多关于此的问题,但令人惊讶的是它的工作实例却非常少.我尝试使用添加在样本中的ToyVpn,但我无法使其工作.然后我发现了这个例子.

VpnService示例

该示例总结了以下步骤中的捕获.

  1. 创建一个TUN界面(我仍然不确定TUN是什么,但在互联网上冲浪说它是在设备上模拟网络层).
  2. 通过TUN获取传入数据包和传出数据包的文件描述符.
  3. 将这些数据包转发到实际的服务器.(不确定这里的服务器是什么?每个传出的数据包都有一个requestUrl,所以服务器在这里意味着将请求委托给requestUrl服务器.或者服务器意味着在AWS上的某个地方创建自己的服务器并重定向那里的所有流量,反过来将流量重定向到实际目的地).
  4. 从服务器获取响应.
  5. 在TUN的帮助下,将此响应再次传递给应用程序的预期组件.

我使用下面的代码创建了一个TUN.我给出了上述教程中给出的地址.不确定,如果有正确的值.以及如何确定这个地址.

 Builder builder = new Builder();
 ParcelFileDescriptor mInterface = builder.setSession("MyVPNService")
          .addAddress("192.168.0.1", 24)
          .addDnsServer("8.8.8.8")
          .addRoute("0.0.0.0", 0).establish();
Run Code Online (Sandbox Code Playgroud)

接下来我得到了文件描述符,并打开了隧道.

     FileInputStream in = new FileInputStream(
              mInterface.getFileDescriptor());
   DatagramChannel tunnel = DatagramChannel.open();
          // I have created a EC2 instance on AWS, and gave the ip Address and port of that server. Not sure if this is the correct method.

          tunnel.connect(new InetSocketAddress("54.254.187.207", 5000));
          //d. Protect this socket, so package send …
Run Code Online (Sandbox Code Playgroud)

sockets vpn networking android

16
推荐指数
1
解决办法
1735
查看次数

Collections.unmodifiableList(list)是否需要锁定?

我有一个productList名为的文件维护Products.java

private List<String> productList = Collections.synchronizedList(new ArrayList());
Run Code Online (Sandbox Code Playgroud)

现在创建一个同步列表,将确保添加/删除等操作将具有隐式锁定,并且我不需要显式锁定这些操作.

我有一个函数暴露,它返回unmodifiableList此列表的一个.

public List getProductList(){

 return Collections.unmodifiableList(productList);
}
Run Code Online (Sandbox Code Playgroud)

在我的应用程序中,各种线程可以同时调用此函数.因此,synchronized在将List转换为不可修改的List时,我需要放置一个块,或者由于我使用的是sychronizedList,这是否已经处理好了?

TIA.

java collections multithreading arraylist

11
推荐指数
1
解决办法
475
查看次数

在多线程应用程序Android中访问数据库的最佳方法?

注意:请不要将此问题标记为重复.我已经经历了几个类似的问题,但找不到满意的答案.

我一直在研究使用Sqlite数据库的应用程序.我们遵循单例模式,确保我们只能在整个应用程序中创建一个辅助类实例.

public class CustomSqliteHelper extends SQLiteOpenHelper {

  public static CustomSqliteHelper getInstance(Context context) {
    if (instance == null) {
      synchronized (CustomSqliteHelper.class) {
        if (instance == null) {
          instance = new CustomSqliteHelper(context);
        }
      }
    }
    return instance;
  }
}
Run Code Online (Sandbox Code Playgroud)

但有时应用程序崩溃了SQLiteDatabaseLockedException.我理解当多个线程/进程尝试一次写入数据库时​​会出现此异常.即使一个线程/进程在写操作仍在进行时尝试读取数据库,也会抛出此异常.

所以我一直在阅读很多关于这个以及防止这种情况发生的可能方法.很多帖子建议使用ContentProvider而不是直接扩展SqliteOpenHelper类和对数据库对象执行操作.在阅读其中一篇帖子时,这篇文章提到在使用Content Provider时,您不需要手动处理多线程环境.

虽然ContentProvider缺乏线程安全性,但通常您会发现在防止潜在的竞争条件方面您无需采取进一步行动.规范示例是您的ContentProvider由SQLiteDatabase支持的时间; 当两个线程同时尝试写入数据库时​​,SQLiteDatabase将自行锁定,确保一个等待直到另一个完成.每个线程都将获得对数据源的互斥访问,从而确保满足线程安全性.

以上引用似乎令人困惑,因为首先它提到ContentProvider不支持线程安全.但他总结说,应用程序开发人员不需要做任何事情来实现并发.

另外,如果我选择使用SqliteOpenHelper,那么防止这些崩溃的最佳方法是什么?我一直在考虑为每个db操作使用锁.

 public class CustomSqliteHelper extends SQLiteOpenHelper {

 private String lock = "lock";

 public void insert(){
    synchronized(lock){
       // Do the insert operation here.
    }
 }


 public void update(){
    synchronized(lock){
       // Do …
Run Code Online (Sandbox Code Playgroud)

java sqlite multithreading android

10
推荐指数
1
解决办法
6049
查看次数

在运行单元测试用例时,Multidex限制命中

我有一个应用程序,我有本地单元测试(测试文件夹)和仪表单元测试用例(androidTest文件夹).现在,如果我点击androidTest文件夹,然后单击" 运行所有测试 ",它将抛出以下异常.

Error:Error converting bytecode to dex:
Cause: com.android.dex.DexIndexOverflowException: field ID not in [0, 0xffff]: 65536

Error:Execution failed for task ':news-app:transformClassesWithDexForDebugAndroidTest'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_79\bin\java.exe'' finished with non-zero exit value 2
Run Code Online (Sandbox Code Playgroud)

这个例外显然是因为达到了multidex限制.但我启用multi-dex了调试版本.我想当运行检测测试用例时,它们以调试模式运行.那为什么会发生这种异常呢?

我附加了build.gradle文件

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'


android {
  compileSdkVersion 22
  buildToolsVersion 22.0.1

  defaultConfig {
    minSdkVersion 14
    targetSdkVersion 22
    applicationId "com.xyz"
  }


  buildTypes {

    debug {
      minifyEnabled false
      shrinkResources false
      multiDexEnabled true
    }

    release { …
Run Code Online (Sandbox Code Playgroud)

android unit-testing

8
推荐指数
1
解决办法
722
查看次数

有没有办法从Espresso Recorder启动特定活动?

我正在使用Espresso进行UI测试.在Espresso中,我可以测试我想要的任何特定活动,而无需使用以下规则从第一个活动开始.

@Rule
public ActivityTestRule activityTestRule = new ActivityTestRule(HomeActivity.class);
Run Code Online (Sandbox Code Playgroud)

注意:这HomeActivity是后来的LoginActivity.

但是当我使用Espresso录音机时,似乎总是开始测试LoginActivity.我需要HomeActivity直接启动.因此Android Studio中有任何选项可以使用Espresso Recorder启动特定活动.

android android-espresso android-espresso-recorder

8
推荐指数
1
解决办法
1016
查看次数

如何在android中发布Event Bus发布的单元测试方法?

我在我的应用程序中使用Otto的事件总线.在我的一个课程中,我发布了这个活动.

MyEvent myevent = new MyEvent();
uiBus.post(myEvent);
Run Code Online (Sandbox Code Playgroud)

我能够测试post方法.

现在还有另一个正在接收事件的班级.

//ReceiverClass.java
@Subscribe
public void onEventReceived(MyEvent myevent) {
    callAMethod();
}
Run Code Online (Sandbox Code Playgroud)

如何进行单元测试以调用此方法.我尝试使用以下测试代码

@Mock
Bus uiBus;

@Test
public void testBusReceviedEvent() {
    ReceiverClass instance = new ReceiverClass();
    mockBus.register(instance);
    MyEvent myevent = new MyEvent();
    mockBus.post(myEvent);
    //Test
    verify(instance, times(1)).callAMethod();
}
Run Code Online (Sandbox Code Playgroud)

但是这段代码不起作用.

android unit-testing mockito

7
推荐指数
1
解决办法
4046
查看次数

Google App Indexing不适用于描述字段,但正在处理标题字段

我已经浏览了这个http://stackoverflow.com/questions/35053454/android-app-indexing-api-description-field.它讨论了在description字段中的使用App Indexing api.在调用此api时,我设置了两个titledescription字段.下面是示例代码

  Thing object = new Thing.Builder()
    .setName(title)
    .setUrl(uri)
    .setDescription(description)
    .build();

return new Action.Builder(Action.TYPE_VIEW)
    .setObject(object)
    .setActionStatus(Action.STATUS_TYPE_COMPLETED)
    .build();
Run Code Online (Sandbox Code Playgroud)

但是,当我使用谷歌应用搜索并输入其中的关键字时title,我能够看到自动完成结果.但是,如果我键入description字段中存在的关键字,我将无法获得自动完成结果.那么我该怎么做才能将描述字段的内容转化为应用索引?

因为这是一个新闻阅读应用程序.所以我将描述设置为新闻内容的第一段.第一段可以长达500个字符.我们传递给描述字段的内容长度是否有任何建议?

android android-app-indexing

7
推荐指数
1
解决办法
300
查看次数

当Web内容响应中包含iframe元素且高度设置为100%时,NestedScrollView内的WebView不会滚动

我有一个Fragment显示的地方WebView.所述Activity含有所述片段具有CoordinatorLayout作为其父.我想Toolbar在用户滚动时折叠WebView.

活动布局

    <android.support.design.widget.CoordinatorLayout android:id="@+id/cordinator_layout"
                                                 xmlns:android="http://schemas.android.com/apk/res/android"
                                                 xmlns:app="http://schemas.android.com/apk/res-auto"
                                                 android:layout_width="match_parent"
                                                 android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/topic_tile_height"
        app:layout_behavior="android.support.design.widget.NewshuntAppBarLayoutBehavior">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/topic_tile_collapsing_toolbar"
            app:layout_scrollFlags="scroll|enterAlways">

            <include layout="@layout/actionbar_news"/>

        </android.support.design.widget.CollapsingToolbarLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/category_tabs"
            android:layout_width="match_parent"
            android:layout_height="@dimen/topic_slidingtablayout_height"
            android:layout_gravity="bottom"
            android:contentDescription="@string/newspaper_category_tabs"
            android:paddingTop="@dimen/topics_sliding_tab_margin_top"/>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/news_list_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:contentDescription="@string/categories_pager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

建议的解决方案是在NestedScrollView中使用WebView. 片段的XML布局如下.

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/category_webitem_container"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
        <WebView
            android:id="@+id/web_item_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </WebView>
    </android.support.v4.widget.NestedScrollView>

</android.support.v4.widget.SwipeRefreshLayout>
Run Code Online (Sandbox Code Playgroud)

当要在WebView中加载的响应是简单的HTML时,这很好.但是,如果响应包含iframe元素,则WebView仅填充视图端口.我无法滚动WebView.

以下是WebView不滚动的示例响应.这里的高度为100%.如果我获得1000dp等固定高度,我可以滚动WebView.但我不会从服务器获得固定的高度.

"content":"<html>\r\n<head>\r\n\t<title></title>\r\n</head>\r\n<body>\r\n<div><iframe height=\"100%\" src=\"https://sportscafe.in/s3/sc-s3-static-mum.sportscafe.in/kwc/en_fixtures.html\" width=\"100%\"></iframe></div>\r\n</body>\r\n</html>\r\n"
Run Code Online (Sandbox Code Playgroud)

虽然WebView中有更多内容,但我无法将其滚动到3月29日的文本之外

在此输入图像描述

iframe android webview

7
推荐指数
0
解决办法
2179
查看次数

无法在仪器测试中模拟静态方法

我很难模拟用于仪器 ( Espresso) 测试的静态方法。对于模拟对象,我使用Mockito. 但是,由于Mockito不能模拟静态方法,我在Powermock它之上使用。这适用于在 JVM 机器上运行的测试,但对于 UI 测试,这种组合不起作用。我已经为仪器测试声明了以下依赖项。

androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'org.powermock:powermock-api-mockito:1.6.5'
androidTestCompile 'org.powermock:powermock-module-junit4:1.6.5' 
androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
Run Code Online (Sandbox Code Playgroud)

我正在编写一个示例测试,如下所示。

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(AndroidJUnit4.class)

public class SampleTest {

  @Test
  public void test(){
    //Test Code.
  }
}
Run Code Online (Sandbox Code Playgroud)

代码编译成功,但在运行时,它给了我以下异常

java.lang.IllegalStateException: Extension API internal error: org.powermock.api.extension.proxyframework.ProxyFrameworkImpl could not be located in classpath.
at org.powermock.reflect.proxyframework.ProxyFrameworkHelper.register(ProxyFrameworkHelper.java:35)
at org.powermock.reflect.proxyframework.ClassLoaderRegisterProxyFramework.registerProxyframework(ClassLoaderRegisterProxyFramework.java:28)
at org.powermock.tests.utils.impl.AbstractCommonTestSuiteChunkerImpl.registerProxyframework(AbstractCommonTestSuiteChunkerImpl.java:101)
at org.powermock.tests.utils.impl.AbstractCommonTestSuiteChunkerImpl.chunkClass(AbstractCommonTestSuiteChunkerImpl.java:114)
at org.powermock.tests.utils.impl.AbstractCommonTestSuiteChunkerImpl.<init>(AbstractCommonTestSuiteChunkerImpl.java:60)
at org.powermock.tests.utils.impl.AbstractCommonTestSuiteChunkerImpl.<init>(AbstractCommonTestSuiteChunkerImpl.java:54)
at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.<init>(AbstractTestSuiteChunkerImpl.java:58)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:59)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:32)
at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:34)
at java.lang.reflect.Constructor.newInstance(Native …
Run Code Online (Sandbox Code Playgroud)

android unit-testing mockito powermock android-espresso

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