Android:Horizo​​ntalScrollview内容未动态更新

时间:2016-07-14 12:51:56

标签: android android-layout

我们面临的问题是Horizo​​ntalScrollview内容未动态更新。第一次调用以下函数时,正确设置了scrollview内容(从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

从ReceiveResult上的DownloadService调用setPlaylistNameScroll方法

    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;
    }
}

1 个答案:

答案 0 :(得分:0)

我们已经阅读了谷歌文档,其他教程,任何更新到horizo​​ntalscrollview内容应该从UI线程发生。所以我们将接收器设置为

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

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

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;
        }
    }
};