2016-10-30 6 views
0

基本的には、複数の行を含むTableLayoutが必要です。また、すべての行には、別のカウンタ(行の2つのボタンの1つを押して1を加算または減算できます)が必要です。1つのアクティビティで複数のカウンタと異なるカウンタ

ここでは、明確な例を見ることができます:

content_main.xml

<TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginBottom="10dp" 
      android:layout_marginTop="55dp"> 

      <TextView 
       android:text="Pizza Margherita" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/textView27" 
       android:layout_weight="10" 
       android:layout_column="1" /> 

      <Button 
       android:text="-" 
       android:layout_height="wrap_content" 
       android:id="@+id/minusButton" 
       android:layout_weight="1" 
       android:layout_column="1" 
       android:layout_width="50dp" /> 

      <TextView 
       android:text="0" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/result" 
       android:layout_weight="5" 
       android:layout_column="1" 
       android:textAlignment="center" /> 

      <Button 
       android:text="+" 
       android:layout_width="50dp" 
       android:layout_height="wrap_content" 
       android:id="@+id/plusButton" 
       android:elevation="0dp" 
       android:layout_weight="0.5" 
       android:layout_column="1" /> 

     </TableRow> 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginBottom="10dp"> 

      <TextView 
       android:text="Pizza Salame" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/textView27" 
       android:layout_weight="10" 
       android:layout_column="1" /> 

      <Button 
       android:text="-" 
       android:layout_height="wrap_content" 
       android:id="@+id/minusButton2" 
       android:layout_weight="1" 
       android:layout_column="1" 
       android:layout_width="50dp" /> 

      <TextView 
       android:text="0" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/result2" 
       android:layout_weight="5" 
       android:layout_column="1" 
       android:textAlignment="center" /> 

      <Button 
       android:text="+" 
       android:layout_width="50dp" 
       android:layout_height="wrap_content" 
       android:id="@+id/plusButton2" 
       android:elevation="0dp" 
       android:layout_weight="0.5" 
       android:layout_column="1" /> 

     </TableRow> 
</TableLayout> 

私はMainActivity.java上で動作する方法は考えています。 私はずっと配列を実装しようとしました(成功しなかった)、すべての単一のカウンタに対して別のクラスを書きました(しかし余りにも冗長になりました)。

ヒント?

+1

ヒント:より多くのコードを表示することなく、ここではあまり発生しません。私たちは "私のビジョンではありませんが、間違いなく、ギャップサービスを記入してください"。さらに、より明確なボタン名を付けることもできます。物事をbuttonpまたはbuttonmと呼ぶのはなぜですか?minusButtonまたはplusButtonと呼ぶのはなぜですか? – GhostCat

+0

ええ、ごめんなさい。私のせい。 – Kevin

答えて

0

質問が正しく理解されていれば、カウンターとアダプターのカスタム表示ロジックを実装して、よりスケーラブルにすることをお勧めします。

基本的にあなたが持っているでしょう:

  • counter_layout.xml - コンポーネント
  • CounterView.javaを宣言 - CounterAdapter.javaに
  • カウンタのロジックをレイアウトを膨らませると制御 - 複数のintancesをインスタンス化しますカウンタービューの

次に、アクティビティーで、アダプターのコンテンツを表示するListViewを使用することができます。

もちろん、各CounterViewの状態を管理する必要があります。しかし、それは簡単ですし、あなたのプロジェクトの次のステップになるかもしれません。

関連する問題