小编Ben*_*fez的帖子

如何以编程方式滚动Horizo​​ntalScrollView

我有一个HorizontalScrollView包含一个RelativeLayout.此布局在XML中为空,并在onCreate中从java填充.

我希望这个滚动视图最初位于中间的某个位置RelativeLayout,这比屏幕大.

我试过mHorizScrollView.scrollTo(offsetX, 0);哪个不起作用.我不知道这有什么问题.

我可以发布代码,但它并不真正相关.重要的是一切都是以编程方式完成的(必须:s),并且HorizontalScrollView必须以编程方式设置初始位置.

谢谢阅读.请告诉我您是否需要更多详细信息,或者这是否不够清楚.

android horizontal-scrolling horizontalscrollview

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

列表视图中有大量垃圾回收

我有一个使用自定义适配器的ListView.自定义适配器的getView使用了所有建议的做法:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    SuscriptionsViewsHolder holder;
    ItemInRootList item = mItemsInList.get(position);

    if (convertView == null) {
         convertView = mInflater.inflate(R.layout.label, null);

         holder = new SuscriptionsViewsHolder();
         holder.label = (TextView) convertView.findViewById(R.id.label_label);
         holder.icon = (ImageView) convertView.findViewById(R.id.label_icon);

        convertView.setTag(holder);
    } else {
        holder = (SuscriptionsViewsHolder) convertView.getTag();
    }

    String text = String.format("%1$s (%2$s)", item.title, item.unreadCount);
    holder.label.setText(text);
    holder.icon.setImageResource(item.isLabel ? R.drawable.folder : R.drawable.file );

    return convertView;
}
Run Code Online (Sandbox Code Playgroud)

但是当我滚动时,由于垃圾收集量很大,它很迟钝:

GC_EXTERNAL_ALLOC freed 87K, 48% free 2873K/5447K, external 516K/519K, paused 30ms
GC_EXTERNAL_ALLOC freed 7K, 48% free …
Run Code Online (Sandbox Code Playgroud)

android listview garbage-collection

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

如何使用配置活动创建应用小部件,并首次更新它?

这真让我抓狂.即使使用推荐的做法,我也不知道如何从配置活动更新app小部件.为什么不在应用程序小部件创建上调用更新方法超出了我的理解.

我想要的是:包含项目集合(带有列表视图)的应用程序小部件.但是用户需要选择一些东西,所以我需要一个配置活动.

配置活动是ListActivity:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class ChecksWidgetConfigureActivity extends SherlockListActivity {
    private List<Long> mRowIDs;
    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
    private BaseAdapter mAdapter;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setResult(RESULT_CANCELED);
        setContentView(R.layout.checks_widget_configure);

        final Intent intent = getIntent();
        final Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        // If they gave us an intent without the widget id, just bail.
        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            finish();
        }

        mRowIDs = new ArrayList<Long>(); // it's actually loaded …
Run Code Online (Sandbox Code Playgroud)

collections android android-appwidget

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

如何使用Android Studio和Gradle创建最佳的Android应用程序项目结构?

我正在尝试切换到Android Studio和Gradle,但是我在与Studio集成和使用Gradle构建时遇到了一些问题.

  • 我有一个依赖于几个库的应用程序.
  • 我想使用Android Studio和Gradle构建系统.
  • 我正在使用git
  • 我的大多数图书馆直接git clone来自他们的github位置

目前,我所拥有的是:

Main Project
 ??? GH Lib 1
 ?     ??? <some stuff from the lib>
 ?     ??? library
 ?           ??? AndroidManifest.xml
 ?           ??? res
 ?           ??? src
 ??? GH Lib 2
 ?     ??? <same structure as the lib 1>
 ??? GH Lib 3
 ?     ??? <same structure as the lib 1>
 ??? GH Lib 4
 ?     ??? <same structure as the lib 1>
 ??? My App folder
       ??? AndroidManifest.xml
       ??? res …
Run Code Online (Sandbox Code Playgroud)

android gradle android-studio

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

如何在Room中过滤嵌套关系?

让我们举个例子:我有一个表单,其中有几个部分,每个部分都有问题.侧面,我有答案映射到问题,他们有另一列,我想在查询时过滤:

数据库架构

所以我有以下实体:

@Entity(tableName = "sections")
public class Section {
    @PrimaryKey
    public long id;
    public String title;
}
@Entity(tableName = "questions")
public class Question {
    @PrimaryKey
    public long id;
    public String title;
    public long sectionId;
}
@Entity(tableName = "answers")
public class Answer {
    @PrimaryKey
    public long id;
    public long questionId;
    public int otherColumn;
}
Run Code Online (Sandbox Code Playgroud)

在DAO部分,我想要检索所有这些.

这是我想通过此查询填充的POJO:

class SectionWithQuestions {
    @Embedded
    public Section section;

    @Relation(parentColumn = "id", entityColumn = "sectionId", entity = Question.class)
    public List<QuestionWithAnswer> questions;

    public static class QuestionWithAnswer {
        @Embedded …
Run Code Online (Sandbox Code Playgroud)

sqlite android android-room

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

如何在Backstack事务后保留片段数据?

我有一个活动,包含片段'list',点击其中一个项目后会将其自身替换为'content'片段.当用户使用后退按钮时,他再次被带到"列表"片段.
问题是片段处于默认状态,无论我尝试持久化数据.

事实:

  1. 两个片段通过创建public static TheFragment newInstance(Bundle args),setArguments(args)Bundle args = getArguments()
  2. 两个片段都在同一级别,它直接位于FrameLayout父活动的内部(即不是嵌套片段)
  3. 我不想打电话setRetainInstance,因为我的活动是主/细节流程,在大屏幕上有2个窗格布局.7"平板电脑有1个纵向画面和2个横向画面.如果我保留'list'片段实例,它会(我认为)搞乱屏幕旋转
  4. 当用户点击"列表"片段中的某个项目时,"内容"片段会FragmentTransaction#replace(int, Fragment, String)以相同的ID但不同的标签显示
  5. 我确实覆盖了onSaveInstanceState(Bundle),但是这并不总是被框架调用,正如文档所述:"在很多情况下,片段可能会被大部分拆除(例如放置在没有UI显示的后台堆栈上),但是它的状态在其拥有的活动实际上需要保存其状态之前不会被保存."
  6. 我正在使用支持库

从上面的子弹5中,我猜测片段事务后需要恢复内存的低端设备可能会调用Fragment#onSaveInstanceState(Bundle).但是,在我的测试设备(Galaxy Nexus和Nexus 7)上,框架不会调用该方法.所以这不是一个有效的选择.

那么,我如何保留一些片段数据呢?传递给捆绑Fragment#onCreate,Fragment#onActivityCreated等始终null.

因此,我无法从全新的片段启动到后台堆栈恢复有所作为.

注意:可能的相关/重复问题

android android-fragments back-stack

9
推荐指数
1
解决办法
8520
查看次数

当CollapsingToolbarLayout的子项时,不会调用Android布局行为

我试图layout_behavior在一个元素上设置一个子元素CollapsingToolbarLayout但是从不在iv_header视图上调用行为.当它固定在外面时,例如tv_follow视图,它可以很好地工作.

文档没有明确说明layout_behavior不能在一个AppBarLayout或那个范围内应用CollapsingToolbarLayout我因为它不工作而感到茫然.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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/header"
        android:layout_width="match_parent"
        android:layout_height="@dimen/full_header_height"
        android:focusable="true"
        android:focusableInTouchMode="true">

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

            <ImageView
                android:id="@+id/iv_header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:contentDescription="@null"
                app:layout_behavior="com.package.view.HidingBehavior"
                app:layout_collapseMode="parallax"
                android:src="@drawable/profile_background"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/header_toolbar"
                android:layout_height="@dimen/action_bar_height"
                android:layout_width="match_parent"
                android:background="@drawable/toolbar_dark_gradient_half"
                android:gravity="top"
                app:layout_collapseMode="pin"/>

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

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

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <TextView
        android:id="@+id/tv_follow"
        android:textSize="20sp"
        android:textColor="@android:color/white"
        android:text="@string/follow"
        android:drawableLeft="@drawable/comm_follow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/header"
        app:layout_anchorGravity="center"
        app:layout_behavior="com.package.view.HidingBehavior"
        android:drawablePadding="8dp"
        android:gravity="center"
        android:visibility="gone"
        android:fitsSystemWindows="true"/>

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

行为被从设计支持库中的FloatingActionButton代码中拉出.

public class HidingBehavior extends CoordinatorLayout.Behavior<View>{ …
Run Code Online (Sandbox Code Playgroud)

android androiddesignsupport android-coordinatorlayout android-collapsingtoolbarlayout

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

如何确定文件是否为空?

如何确定文件是否为空?该文件由在Windows平台上运行的C程序打开.我想以追加模式打开文件,如果为空,首先打印一个标题.

// Open CSV & write header
report_csv = fopen("SNR.csv", "a+");
if (!report_csv) {
    fprintf(stderr, "Unable to open CSV output file...");
    return -1;
}
if (!ftell(report_csv)) {
    fprintf(report_csv, "Column A;Column B;Column C\n");
}
// ... print data to file
fclose(report_csv);
Run Code Online (Sandbox Code Playgroud)

ftell如果文件不为空,我希望返回当前文件大小,这是因为上面的代码是循环的.

但是,ftell始终返回0并且标题被多次打印.

我知道我可以fopen将其与r和使用fseek/ ftell/ fclose,然后fopen再次用a+,但我认为这是可能做到这一点而无需打开和多次关闭文件.

c file-io

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

无论操作系统如何,在Java中执行计划任务的最佳解决方案是什么?

我想在我的Java桌面应用程序上生成警报:

  • 警报设置的具体日期/时间可以是5分钟或5个月
  • 我需要能够在触发警报时创建SWT应用程序
  • 我需要这个能够在任何操作系统上工作.软件用户可能拥有Windows(其中90%),其余Mac OS(包括我)
  • 软件许可证必须允许我在商业程序中使用它,而不需要开源它(因此,没有GPL)
  • 我不能要求用户安装Cygwin,因此实现需要是Windows和Unix的原生

我正在使用Java,Eclipse,SWT进行开发,我的应用程序是使用Java Web Start从我的服务器部署的.我正在使用Mac OS X.6进行开发.


我想我有几个选择:

  1. 在启动时运行我的应用程序,并自己处理一切;
  2. 使用系统服务.
  3. 在Unix上使用cron表,在Windows上使用Scheduled Tasks

在启动时运行

我真的不喜欢这个解决方案,我希望有更优雅的东西.
参考:我想在Mac OS/Windows上的System Startup上运行我的Java程序.我怎样才能做到这一点?

系统服务

如果我将它作为系统服务运行,我可以从中受益,因为操作系统将确保我的软件:

  • 一直在运行
  • 没有/需要GUI
  • 失败后重新启动

我研究了一些我可以使用的资源:

  • run4j - CPL - 仅在Windows上运行,似乎是一个有效的候选者
  • jsvc - Apache 2.0 - 仅限Unix,似乎是一个有效的候选者
  • Java Service Wrapper - 各种 - 我买不起付费许可证,免费的是GPL.因此,我不想/不能使用它

我在系统服务选项中的问题是:

  1. 还有其他选择吗?
  2. 我的计划实施是否正确:

    • 在应用程序启动时,检查服务是否存在
    • 如果没有安装:
      • 升级用户以安装服务(Unix上的root,Windows上的UAC)
      • 如果主机操作系统是Windows,请使用run4j注册该服务
      • 如果主机操作系统是Unix,请使用jsvc注册该服务
    • 如果它没有运行,请启动它

因此,在第一次运行时,应用程序将安装该服务并启动它.当应用程序关闭时,服务仍在运行,并且不再需要应用程序,除非它未注册.
但是,我想我仍然会错过"启动时运行"功能.

我对吗?我错过了什么吗?

cron /任务计划程序

在Unix上,我可以轻松使用cron表而无需应用程序将用户升级为root.我不需要处理重启,系统日期更改等.看起来不错.

在Windows上,我可以使用任务计划程序,即使在使用AtSchTasks的命令行中也是如此.这看起来不错,但是我需要这个兼容从XP到7,我不能轻易测试这个.


那你会怎么做?我错过了什么?您有什么建议可以帮助我选择最好,最优雅的解决方案吗?

java cron service scheduled-tasks

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

如何告诉IDEA/Studio已完成空检查?

我正在开发Android Studio/IntelliJ IDEA.

我已启用名为"常量条件和例外"的检查检查,如果我冒着NPE的风险,则显示警告,例如:

String foo = foo.bar(); // Foo#bar() is @nullable
if (foo.contains("bar")) { // I'm living dangerously
    ...
}
Run Code Online (Sandbox Code Playgroud)

我的代码中有以下内容:

String encoding = contentEncoding == null ? null : contentEncoding.getValue();
if (!TextUtils.isEmpty(encoding) && encoding.equalsIgnoreCase("gzip")) {
    inputStream = new GZIPInputStream(entity.getContent());
} else {
    inputStream = entity.getContent();
}
Run Code Online (Sandbox Code Playgroud)

这是源代码TextUtils#isEmpty(String):

/**
 * Returns true if the string is null or 0-length.
 * @param str the string to be examined
 * @return true if str is null or zero length …
Run Code Online (Sandbox Code Playgroud)

intellij-idea android-studio

8
推荐指数
3
解决办法
6362
查看次数