3

Android には、セクションの ArrayList があります (セクション クラスがあるため、単なる文字列の ArrayList ではありません)。各セクションをボタンとして表現したいと思います。現在、各セクションを反復処理し、section.xml を膨らませてから、特定のセクションごとに異なるプロパティを動的に追加することで、これを実現しています。

SectionsActivity.java:

public class SectionsActivity extends Activity {

    private int numSections;
    LayoutInflater inflater;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sections);

        numSections = App.Sections.getSectionList().size();
        inflater = getLayoutInflater();

        LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
        for (int i = 0; i < numSections; i++) {
            ll.addView(getSectionButton(App.Sections.getSectionList().get(i)));
        }
    }

    public Button getSectionButton(Section s) {
        Button b = (Button) inflater.inflate(R.layout.section, null);
        b.setHint("section" + s.getSectionId());
        b.setText(s.getName());
        b.setTextColor(Color.parseColor("#"+s.getColor()));
        return b;
    }

}

Sections.java:

public class Sections {

    private ArrayList<Section> SectionList;

    public ArrayList<Section> getSectionList() {
        return SectionList;
    }

    public void setSectionList(ArrayList<Section> sectionList) {
        SectionList = sectionList;
    }

}

Section.java:

public class Section {

    private String Color;
    private String Name;
    private int SectionId;

    //constructor, standard getters and setters

}

section.xml:

<?xml version="1.0" encoding="utf-8"?>

<Button
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textStyle="bold" />

これはうまくいきますが、おそらくもっと良い解決策があると思います。Windows Phone 7 の .NET の例を次に示します。XAML に何をバインドするかを伝え (SectionList、これはObservableCollectionです)、コレクション内の各項目をどのように表現するかのテンプレートを渡します。

<StackPanel Name="StackPanelSection">
    <ListBox Name="ListBoxSection" ItemsSource="{Binding SectionList}" ScrollViewer.VerticalScrollBarVisibility="Disabled">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Name, Converter={StaticResource StringToLowercaseConverter}}" FontFamily="Segoe WP SemiLight" FontSize="48" Foreground="{Binding HTMLColor}" Tap="TextBlockSection_Tap" />
                </StackPanel>
            </DataTemplate>
         </ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

これは、単純さと、SectionList の内容を変更すると UI が自動的に更新されるという点の両方で優れています。Android でのデータ バインディングについて十分に読んだので、おそらく真に同等のものはないことがわかりましたが、同じタスクを達成するための最良の方法は何でしょうか? ありますか?ここでデータバインディングが良い解決策ではない場合でも、Android コードを構築する別の方法はありますか?

4

1 に答える 1

1

フレームワークに組み込まれているため、XAMLでそのバインディングを取得します。アプリはバインディングルックアップをサポートするランタイム環境で実行されるため、ツールセットの一部としてMicrosoftからバインディングフレームワーク全体を取得します。Androidにはこのようなものはありません。

XAMLの場合のように宣言的な方法でバインディングを行う方法はわかりませんが、同様のボート(WPF / .Net / XAMLのバックグラウンドからAndroidに移行)に参加しているので、それをより便利にします。あなたはその道を順調に進んでいるようです。私は、使用するリストやグリッドにカスタムアダプターを使用します。これにより、同様の便利さが得られます... xamlバインディングほど便利ではありませんが、それでもかなりクールです。

私はあなたのUIを見たことがないので、あなたのコードから推測することしかできませんが、あなたがしていること(LinearLayout内のボタン)はListViewとカスタムアダプターでしか達成できないと思います。おそらく最も古典的なAndroid開発者のビデオは、過去のGoogle I/OのWorldofListViewです。それは数年前のものですが、それでも素晴らしい時計であり、関連性があります。

https://www.youtube.com/watch?v=wDBM6wVEO70

于 2012-10-12T22:31:29.703 に答える