2016-07-14 7 views
3

Horizo​​ntalScrollviewのコンテンツが動的に更新されないという問題に直面しています。初めて以下の関数を呼び出すと、scrollviewの内容が正しく設定されました(onCreateから呼び出されました)。しかし、次回以降、レイアウトは更新されません(バックグラウンドサービスでいくつかの新しいダウンロードが行われた後、基本的に関数を呼び出す)。 playlist_scrollは、(partial_main_playlistから膨らませた)アイテムが追加されるLinearLayout playlist_contentを持つHorizo​​ntalScrollViewです。Android:Horizo​​ntalScrollviewのコンテンツが動的に更新されない

<?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方法は、我々はGoogleドキュメントに読んだ本

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

コード内に水平スクロールビューについて言及したことはありません。 – chandil03

+0

@ chandil03申し訳ありませんが、playlist_scrollはHorizo​​ntalScrollViewです。ここにはアイテムを追加するLinearlayoutのplaylist_contentがあります。私の編集した質問を見てください。 –

+0

ダウンロードコードはどこにありますか?またはscrollView内のlinearLayout内のコンテンツを更新するコード。 – chandil03

答えて

0

ようReceiveResultにDownloadServiceのから呼び出され、次のように

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です他のチュートリアルでは、horiへのアップデートzontalscrollviewのコンテンツは、UIスレッドから発生する必要があります。だから私たちは私の理解「の新しいハンドラ()」を1として

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

のような受信機を設定した結果、受信メソッドの呼び出しは、UIスレッドで実行されることを確認します。しかし残念なことに、Android 5.1.1 APIレベル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; 
     } 
    } 
}; 
関連する問題