2017-07-19 80 views
0

我正在創建一個應用程序,其中有一個片段,用戶可以使用按鈕打開微調器並選擇一個食物項目。食物項目將在列表中的按鈕下顯示。我曾嘗試過創建文字瀏覽和製作無形文字瀏覽並稍後設置文字,但都沒有成功。我想知道是否有人知道更好的創建方式,我沒有看到。顯示數據的高效方式

screenshot

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.lastineindustries.ingredismartv2.Kitchen"> 

<!-- TODO: Update blank fragment layout --> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <Button 
     android:text="Add An Ingredient" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/add" 
     android:textSize="30sp" 
     android:layout_gravity="center" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 


     <LinearLayout 
      android:layout_width="320dp" 
      android:layout_height="match_parent" 
      android:id="@+id/main" 
      android:orientation="vertical"> 


     </LinearLayout> 

     <LinearLayout 
      android:layout_width="60dp" 
      android:layout_height="match_parent" 
      android:id="@+id/valueButton" 
      android:orientation="vertical"> 

     </LinearLayout> 

    </LinearLayout> 

</LinearLayout> 

+0

嵌套的LinearLayouts絕對不好......你能畫一幅你真正想要展示的圖片嗎? –

+0

*食物項目然後將顯示在按鈕列表中* ... ListView和ArrayAdapter是爲此目的而構建的,而不是動態LinearLayout –

+0

我仍然使用在代碼中編輯的TextViews嗎? – Keith

回答

0

您應該使用ListViewRecyclerView此類任務。查看Android官方文檔中的示例:link

1

這是你想要的嗎?

image

然後,閱讀我的代碼。的ChooseFoodFragment.java

  1. 代碼和它的佈局文件:

    public class ChooseFoodFragment extends Fragment{ 
    
        @Nullable 
        @Override 
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
         View v = inflater.inflate(R.layout.fragment_choose_food, container, false); 
         initViews(v); 
         return v; 
        } 
    
        private Button btn_add; 
        private ListView listView; 
        private List<String> mData = new ArrayList<>(); 
        private ArrayAdapter<String> mAdapter; 
    
    
        private void initViews(View v) { 
         listView = v.findViewById(R.id.list); 
         mAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, mData); 
         listView.setAdapter(mAdapter); 
    
         btn_add = v.findViewById(R.id.add); 
         btn_add.setOnClickListener(new View.OnClickListener() { 
          @Override 
          public void onClick(View view) { 
           ChooseFoodDialog dialog = new ChooseFoodDialog(); 
           dialog.setOnSelectedListener(new ChooseFoodDialog.OnSelectedListener() { 
            @Override 
            public void onSelected(String name) { 
             mData.add(name); 
             mAdapter.notifyDataSetChanged();// update the list of selected food 
            } 
           }); 
           dialog.show(getFragmentManager(), "");//show the spinner items to select 
          } 
         }); 
        } 
    } 
    

fragment_choose_food.xml,只是刪除其他視圖,除了根視圖和按鈕,再加入一個ListView。

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.lastineindustries.ingredismartv2.Kitchen"> 

    <!-- TODO: Update blank fragment layout --> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <Button 
      android:id="@+id/add" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="Add An Ingredient" 
      android:textSize="30sp" /> 

     <ListView 
      android:id="@+id/list" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="16dp" /> 

    </LinearLayout> 
</FrameLayout> 
  • ChooseFoodDialog.java代碼及其佈局文件。

    public class ChooseFoodDialog extends DialogFragment { 
    
        @Nullable 
        @Override 
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
         View v = LayoutInflater.from(getContext()).inflate(R.layout.dialog_choose_food, container, false); 
         initViews(v); 
         return v; 
        } 
    
        private ListView lv; 
        private List<String> mData = new ArrayList<>(); 
    
        private void initViews(View v) { 
         // prepare some temp data 
         for (int i = 0; i < 10; i++) { 
          mData.add("Ingredients_" + i); 
         } 
    
         lv = v.findViewById(R.id.lv_ingredients); 
         ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, mData); 
         lv.setAdapter(adapter); 
         lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
          @Override 
          public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
           String ingredient = mData.get(i); 
           if (mListener != null) { 
            mListener.onSelected(ingredient);// when the item of food is selected 
           } 
           dismiss(); 
          } 
         }); 
    
        } 
    
        private OnSelectedListener mListener; 
    
        public void setOnSelectedListener(OnSelectedListener listener) { 
         mListener = listener; 
        } 
    
    
        interface OnSelectedListener { 
         void onSelected(String name); 
        } 
    } 
    
  • dialog_choose_food.xml,這很簡單。

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
    
        <ListView 
         android:id="@+id/lv_ingredients" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"/> 
    
    </LinearLayout> 
    
  • 運行程序,檢查它。