2017-07-19 78 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> 
    
  • 运行程序,检查它。