【问题标题】:Android: HorizontalScrollview content not getting updated DynamicallyAndroid:Horizo​​ntalScrollview 内容未动态更新
【发布时间】:2016-10-31 19:13:37
【问题描述】:

我们遇到了 Horizo​​ntalScrollview 内容没有动态更新的问题。在第一次调用以下函数时正确设置滚动视图内容(从 onCreate 调用)。但是从下一次开始布局不会更新(基本上是在后台服务上完成一些新下载后调用该函数)。 playlist_scroll 是 Horizo​​ntalScrollView,我们在其中添加了项目(从 partial_main_playlist 膨胀)的 LinearLayout playlist_content。

HorizontalScrollView playlist_scroll = (HorizontalScrollView) findViewById(R.id.playlist_scroll);
LinearLayout playlist_content = (LinearLayout) findViewById(R.id.playlist_content);

 private void setPlaylistNameScroll(List<Playlist> result) {
    if (result != null && result.size() > 0) {
        playlist_row_position = 0;
        playlists = result;
        playlist_content.removeAllViews();
        for (Playlist playlist : playlists) {
            playlist_ll = (LinearLayout) getLayoutInflater().inflate(R.layout.partial_main_playlist, playlist_content, false);
            playlist_ll.setTag(playlist.getId());
            Log.d(TAG, "setPlaylistNameScroll - playlist " + playlist.getName());
            if (border != null && !border.equals("")) {
                playlist_ll.setBackgroundColor(Color.parseColor("#" + border));
            }
            playlist_row = (LinearLayout) playlist_ll.findViewById(R.id.playlist_row);
            LinearLayout.LayoutParams playlist_row_params = (LinearLayout.LayoutParams) playlist_row.getLayoutParams();
            playlist_row_params.width = playlist_row_width;
            playlist_row_params.height = playlist_row_height;
            playlist_row.setLayoutParams(playlist_row_params);
            playlist_row_item = (LinearLayout) playlist_ll.findViewById(R.id.playlist_row_item);
            if (playlist_bg_bitmap_draw != null) {
                playlist_row_item.setBackground(playlist_bg_bitmap_draw);
            }
            playlist_tv = (TextView) playlist_ll.findViewById(R.id.playlist_tv);
            String _playlist_name = playlist.getName();
                playlist_tv.setText(_playlist_name);
            playlist_tv.setTextColor(Color.parseColor("#" + playlist_title_color));
            playlist_ll.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    switch (event.getAction()) {
                        case MotionEvent.ACTION_DOWN:
                            v.startAnimation(btn_click_animation);
                            break;
                    }
                    return false;
                }
            });
                    playlist_content.addView(playlist_row, playlist_row_position++);

        }
        playlist_scroll.post(new Runnable() {
            public void run() {
                if(playlist_content.getParent() != null)
                {
                    ((ViewGroup)playlist_content.getParent()).removeView(playlist_content);
                }
                playlist_scroll.addView(playlist_content);
            }
        });
    }
}

partial_main_playlist.xml如下

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:id="@+id/playlist_row"
    android:clickable="true"
    android:onClick="playlistClicked"
    android:background="@color/colorDefaultBg">
    <LinearLayout
        android:id="@+id/playlist_row_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="@dimen/resource_row_margin"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/playlist_tv"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.9"
            android:textAlignment="center"
            android:ellipsize="marquee"
            android:maxLines="1"
            android:textStyle="bold"
            android:gravity="center"
            android:layout_gravity="center_vertical"
            android:text="Playlist"
            android:textColor="@color/colorDefaultText"
            android:textSize="@dimen/playlist_scroll_title_font" />
        <ImageView
            android:id="@+id/playlist_status"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="0.1"
            android:layout_gravity="end"
            android:gravity="center"
            android:scaleType="center"
            android:visibility="invisible"
            android:src="@drawable/shuffle"/>
    </LinearLayout>
</LinearLayout>

We have referred the answer here and implemented the above code

setPlaylistNameScroll 方法在 ReceiveResult 上从 DownloadService 调用,如下所示

    public void onReceiveResult(int resultCode, Bundle resultData) {
    String error;
    switch (resultCode) {
        case DownloadService.STATUS_RUNNING:
            media_syncing = true;
            Log.d(TAG, "DownloadService.STATUS_RUNNING");
            break;
        case DownloadService.STATUS_FINISHED:
            Log.d(TAG, "DownloadService.STATUS_FINISHED");
            setPlaylistNameScroll(databaseHandler.getDevicePlaylists(device_id, playlist_ordering));
            break;
        case DownloadService.STATUS_ERROR:
            media_syncing = false;
            error = resultData.getString(Intent.EXTRA_TEXT);
            Log.d(TAG, "DownloadService.STATUS_ERROR " + error);
            break;
        case DownloadService.STATUS_STOPPED:
            media_syncing = false;
            error = resultData.getString(Intent.EXTRA_TEXT);
            Log.d(TAG, "DownloadService.STATUS_STOPPED " + error);
            break;
    }
}

【问题讨论】:

  • 代码中没有提到水平滚动视图。
  • @chandil03 抱歉,playlist_scroll 是 Horizo​​ntalScrollView,其中我们有一个 Linearlayout playlist_content,我们正在向其中添加项目。请查看我编辑的问题。
  • 您的下载代码在哪里?或您在 scrollView 内的 linearLayout 中更新内容的代码。
  • @chandil03 我们省略了下载代码,因为问题会变得太长。从作为 IntentService 的 DownloadService 调用 setPlaylistNameScroll 方法即将到来(我们可以看到确认它的日志)。滚动内容和线性布局内容更新如 playlist_content.addView(playlist_row, playlist_row_position++);最后是 playlist_scroll.post(new Runnable() { public void run() { if(playlist_content.getParent() != null) { ((ViewGroup)playlist_content.getParent()).removeView(playlist_content); } playlist_scroll.addView(播放列表内容);} });
  • 我告诉我把所有的代码,但只是 resultReceiver 放在方法被调用的地方。我没有得到你在 playlist_scroll.post 方法中想要做什么。我的意思是有这么多代码,我无法为您提供解决方案。

标签: android android-layout


【解决方案1】:

我们在 google doc 和其他教程中阅读到,任何对 Horizo​​ntalscrollview 内容的更新都应该从 UI 线程发生。所以我们将接收器设置为

mReceiver = new DownloadResultReceiver(new Handler());     
mReceiver.setReceiver(this);

根据我的理解,“new Handler()”将确保结果接收器方法调用将在 UI 线程中执行。但不幸的是,至少在 Android 5.1.1 API Level 22 中我们的情况并非如此,其他版本我们还没有观察到这个问题。最后我们得到了一个类似runnable的东西,它会不断检查是否有新版本,如果有,它会更新水平滚动视图

Runnable refreshChecker = new Runnable() {
    @Override
    public void run() {
        try {
            int save_media_called = databaseHandler.getSave_media_called();                               
            if (save_media_called_log != save_media_called && save_media_called != 0) {                    setPlaylistNameScroll(databaseHandler.getDevicePlaylists(device_id, playlist_ordering));
                save_media_called_log = save_media_called;
            }
        } finally {
            refreshHandler.postDelayed(refreshChecker, refresh_check_interval);
            refresh = true;
        }
    }
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 2017-04-15
    • 2021-06-13
    相关资源
    最近更新 更多