📜  如何在android中添加自定义微调器?

📅  最后修改于: 2022-05-13 01:54:32.500000             🧑  作者: Mango

如何在android中添加自定义微调器?

Spinner是一个小部件,用于从项目列表中选择一个项目。当用户点击微调器时,用户可以看到一个下拉菜单。在本文中,我们将学习如何在应用程序中添加自定义微调器。如果您想详细了解微调器,请单击此链接。

方法:

  1. 创建一个新文件algorithm_spinner.xml并添加以下代码。微调器中的每个项目都将具有此布局、一个图像视图和一个文本视图。
    algorithm_spinner.xml
    
    
      
        
      
        
      
    


    AlgorithmItem.java
    package org.geeksforgeeks.gfgcustomspinner;
      
    public class AlgorithmItem {
        private String algorithmName;
      
        public AlgorithmItem(String countryName)
        {
            algorithmName = countryName;
        }
      
        public String getAlgorithmName()
        {
            return algorithmName;
        }
    }


    AlgorithmAdapter.java
    package org.geeksforgeeks.gfgcustomspinner;
      
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import java.util.ArrayList;
      
    public class AlgorithmAdapter extends ArrayAdapter {
      
        public AlgorithmAdapter(Context context,
                                ArrayList algorithmList)
        {
            super(context, 0, algorithmList);
        }
      
        @NonNull
        @Override
        public View getView(int position, @Nullable
                                          View convertView, @NonNull ViewGroup parent)
        {
            return initView(position, convertView, parent);
        }
      
        @Override
        public View getDropDownView(int position, @Nullable
                                                  View convertView, @NonNull ViewGroup parent)
        {
            return initView(position, convertView, parent);
        }
      
        private View initView(int position, View convertView,
                              ViewGroup parent)
        {
            // It is used to set our custom view.
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.algorithm_spinner, parent, false);
            }
      
            TextView textViewName = convertView.findViewById(R.id.text_view);
            AlgorithmItem currentItem = getItem(position);
      
            // It is used the name to the TextView when the
            // current item is not null.
            if (currentItem != null) {
                textViewName.setText(currentItem.getAlgorithmName());
            }
            return convertView;
        }
    }


    activity_main.xml
    
    
      
        
      
        
      
    


    MainActivity.java
    package org.geeksforgeeks.gfgcustomspinner;
      
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.Spinner;
    import android.widget.Toast;
    import java.util.ArrayList;
      
    public class MainActivity extends AppCompatActivity {
      
        ArrayList algorithmItems;
        AlgorithmAdapter adapter;
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            initList();
            Spinner spinner = findViewById(R.id.spinner_algorithm);
      
            // we pass our item list and context to our Adapter.
            adapter = new AlgorithmAdapter(this, algorithmItems);
            spinner.setAdapter(adapter);
      
            spinner.setOnItemSelectedListener(
                new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView parent,
                                               View view, int position, long id)
                    {
      
                        // It returns the clicked item.
                        AlgorithmItem clickedItem = (AlgorithmItem)
                                                        parent.getItemAtPosition(position);
                        String name = clickedItem.getAlgorithmName();
                        Toast.makeText(MainActivity.this, name + " selected", Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onNothingSelected(AdapterView parent)
                    {
                    }
                });
        }
      
        // It is used to set the algorithm names to our array list.
        private void initList()
        {
            algorithmItems = new ArrayList<>();
            algorithmItems.add(new AlgorithmItem("Quick Sort"));
            algorithmItems.add(new AlgorithmItem("Merge Sort"));
            algorithmItems.add(new AlgorithmItem("Heap Sort"));
            algorithmItems.add(new AlgorithmItem("Prims Algorithm"));
            algorithmItems.add(new AlgorithmItem("Kruskal Algorithm"));
            algorithmItems.add(new AlgorithmItem("Rabin Karp"));
            algorithmItems.add(new AlgorithmItem("Binary Search"));
        }
    }


  2. 创建一个新文件AlgorithmItem。 Java并添加以下代码。这是模型类,用于在用户单击任何项目时获取算法名称。在这里,我们定义了一个构造函数和一个getAlgorithmName方法,该方法返回对象的算法名称。

    算法项。Java

    package org.geeksforgeeks.gfgcustomspinner;
      
    public class AlgorithmItem {
        private String algorithmName;
      
        public AlgorithmItem(String countryName)
        {
            algorithmName = countryName;
        }
      
        public String getAlgorithmName()
        {
            return algorithmName;
        }
    }
    

  3. 创建一个新文件算法适配器。 Java并添加以下代码。这里我们定义了我们自己的Adapter 类。它将项目与其视图映射,提供对微调器列表中项目数据的访问。

    算法适配器。Java

    package org.geeksforgeeks.gfgcustomspinner;
      
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    import java.util.ArrayList;
      
    public class AlgorithmAdapter extends ArrayAdapter {
      
        public AlgorithmAdapter(Context context,
                                ArrayList algorithmList)
        {
            super(context, 0, algorithmList);
        }
      
        @NonNull
        @Override
        public View getView(int position, @Nullable
                                          View convertView, @NonNull ViewGroup parent)
        {
            return initView(position, convertView, parent);
        }
      
        @Override
        public View getDropDownView(int position, @Nullable
                                                  View convertView, @NonNull ViewGroup parent)
        {
            return initView(position, convertView, parent);
        }
      
        private View initView(int position, View convertView,
                              ViewGroup parent)
        {
            // It is used to set our custom view.
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.algorithm_spinner, parent, false);
            }
      
            TextView textViewName = convertView.findViewById(R.id.text_view);
            AlgorithmItem currentItem = getItem(position);
      
            // It is used the name to the TextView when the
            // current item is not null.
            if (currentItem != null) {
                textViewName.setText(currentItem.getAlgorithmName());
            }
            return convertView;
        }
    }
    

  4. 在 activity_main.xml 文件中添加以下代码。在这里,我们在布局上添加微调器。这将添加一个textview和一个spinner

    activity_main.xml

    
    
      
        
      
        
      
    
    

  5. 现在在MainActivity 中添加以下代码。 Java文件。这里创建了 AlgorithmAdapter 类对象,它充当微调器的适配器并将onItemSelectedListner()添加到我们的微调器。当用户点击微调器的任何项目时,它就会被调用。它显示了一个带有用户从列表中选择的项目名称的toast

    主要活动。Java

    package org.geeksforgeeks.gfgcustomspinner;
      
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.Spinner;
    import android.widget.Toast;
    import java.util.ArrayList;
      
    public class MainActivity extends AppCompatActivity {
      
        ArrayList algorithmItems;
        AlgorithmAdapter adapter;
      
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            initList();
            Spinner spinner = findViewById(R.id.spinner_algorithm);
      
            // we pass our item list and context to our Adapter.
            adapter = new AlgorithmAdapter(this, algorithmItems);
            spinner.setAdapter(adapter);
      
            spinner.setOnItemSelectedListener(
                new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView parent,
                                               View view, int position, long id)
                    {
      
                        // It returns the clicked item.
                        AlgorithmItem clickedItem = (AlgorithmItem)
                                                        parent.getItemAtPosition(position);
                        String name = clickedItem.getAlgorithmName();
                        Toast.makeText(MainActivity.this, name + " selected", Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onNothingSelected(AdapterView parent)
                    {
                    }
                });
        }
      
        // It is used to set the algorithm names to our array list.
        private void initList()
        {
            algorithmItems = new ArrayList<>();
            algorithmItems.add(new AlgorithmItem("Quick Sort"));
            algorithmItems.add(new AlgorithmItem("Merge Sort"));
            algorithmItems.add(new AlgorithmItem("Heap Sort"));
            algorithmItems.add(new AlgorithmItem("Prims Algorithm"));
            algorithmItems.add(new AlgorithmItem("Kruskal Algorithm"));
            algorithmItems.add(new AlgorithmItem("Rabin Karp"));
            algorithmItems.add(new AlgorithmItem("Binary Search"));
        }
    }
    

输出: