2017-07-11 19 views
0

this onethis oneなど、RemoteViewsオブジェクトに新しいビューを追加する正しい方法を説明しています。最初のリンクでは、内部レイアウトがaddViewメソッドで参照されるように、LinearLayoutの内部にネストされたLinearLayoutが存在するように指定されています。 addView(innerLayoutID, view)RemoteViewsのaddView()の使い方

外側のレイアウトではなく内側のレイアウトを参照する理由は何ですか? addViewは外部レイアウトで動作しないのでしょうか、これは個人的な意見ですか?

答えて

0

最初のリンクの回答は一般的には正しいものの、掲載されたとおりに正確に実行することはできませんでしたので、私のプロジェクトでこれを実行するには詳細が必要でした。私は将来の参照のために私のコードを以下に掲載しました。

他のスレッドで指定されていなかった非常に重要なことは、rv.addView()rvオブジェクトで指定されたのと同じレイアウトを必要とすることです。すなわち、LinearLayoutを別のLinearLayoutの中に入れ子にしてから、LinearLayoutを参照することができませんでした。それは親のrvインスタンスで参照されているものなので、外側のものを参照する必要がありました。マイonUpdate()

はサムスンルックのエッジパネルのAPIに固有のものですが、一般的なフォーマットは同じです。

@Override 
public void onUpdate(Context context, SlookCocktailManager manager, int[] cocktailIds) { 
    //Parent RemoteViews object 
    RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.main_view); 
    for (int i = 0; i < someObjectArray.length; i++) { 
     //Create new remote view using the xml file that represents a list entry 
     RemoteViews listEntryLayout = new RemoteViews(context.getPackageName(), R.layout.list_entry); 
     //Set the text of the TextView that is inside the above specified listEntryLayout RemoteViews 
     listEntryLayout.setTextViewText(R.id.stock_text, someObjectArray[i].getName()); 
     //Add the new remote view to the parent/containing Layout object 
     rv.addView(R.id.main_layout, listEntryLayout); 
    } 
    //standard update 
    if (cocktailIds != null) { 
     for (int id : cocktailIds) { 
      manager.updateCocktail(id, rv); 
     } 
    } 
} 

main_view.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/main_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:importantForAccessibility="2" 
    android:orientation="vertical" > 
</LinearLayout> 

list_entry.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/stock_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/stock_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:background="@android:color/holo_green_light" 
     android:text="TextView" 
     android:textSize="20dp"/> 
</LinearLayout> 
関連する問題