【问题标题】:Android Studio gridViewAndroid Studio 网格视图
【发布时间】:2015-05-17 20:29:05
【问题描述】:

是否可以在 GridView 的每个项目中包含方法和实际编码?

我正在尝试创建的应用程序是一个货币转换器,我目前在 gridView 中显示 3 个图像:欧元、比索和卢比。

一旦用户点击一个,我希望打开一个新的 XML,它显示一个 textView。用户在 textView 中输入美元的价值并点击计算按钮。然后应用程序会在屏幕底部显示转换后的金额。

问题是我无法弄清楚每次在 gridView 中单击图片时如何打开新的 XML。假设我能够做到这一点,我也不确定将转换后的代码放在哪里。我会创建一个新的.java 还是只是地方都在MainActivity.java 中?

谢谢。

【问题讨论】:

    标签: java android gridview


    【解决方案1】:

    您最好做的是,当用户点击一种货币时,它会将他们带到另一个活动,然后您将加载另一个 xml 以显示您想要显示的任何内容。

    为了检测哪个项目被点击,你可以实现一个 onItemClickListener 例如

        gridView.setOnItemClickListener(new OnItemClickListener() {
    
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
    
             //this assumes you give the gridview a list of currency which it then displays. Here we get the currency selected to then pass to our new activity 
             String selectedCurrency = myArrayOfCurrencies.get(position);
    
              //then start new activity and give it the currency. This means we won't have to create an activity for each currency. You just need to create 1 and then based on what currency you give it will change the functionality 
             Intent intent = new Intent(this, Converter.class);
             Intent.putExtra("currency", selectedCurrency);
             startActivity(intent); 
    
    }
    

    【讨论】:

      【解决方案2】:

      首先,您应该能够通过调用setOnItemClickListener() 方法来检测对GridView 的每个项目的点击。 如果您设置了clicklistener 并且仍然无法检测到点击,那么很可能您需要将这些属性添加到 xml 中的 imageView

      android:focusable="false"
      
      android:focusableInTouchMode="false"
      

      其次,一旦您能够检测到点击,您就可以开始新的活动或添加包含将促使用户输入值的编辑文本的片段。

      第三,我建议将负责货币转换的代码单独放在一个类中,并创建接受一个值并将其转换为其他货币的静态方法,例如:

      public class CurrencyConverter {
      
      public static double convertToRupees (String currencyType, double currencyValue){
               ....
                        return currencyInRupees;
                 } 
      }
      

      顺便说一句,我建议您将 RecyclerView 与网格布局管理器一起使用,而不是 GridView。

      【讨论】:

        【解决方案3】:

        我会创建更多的课程。 您询问了如何为每个 gridView 项打开不同的 XML 文件。

        1. 创建一个扩展 BaseAdapter 的自定义适配器。
        2. 覆盖 getView 并根据位置为每个视图附加正确的 Xml 文件。

        例如:

        YourActivity.java:

        GridView gridView;
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        
            gridView = (GridView) findViewById(R.id.gridView);
            gridView.setAdapter(new MyAdapter(getApplicationContext());
        }
        

        MyAdapter.java:

        ...
        @Override
        public int getCount() {
            return XmlArr.length;
        }
        
        @Override
        public Object getItem(int position) {
            return XmlArr[position];
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Xml myXml = (Xml) getItem(position);
            Holder holder;
        
            if (convertView == null) {  
                // Set your view's layout. Consider using LayoutInflater.
                // Use a static holder to prevent re-initialization:
        
                holder = new Holder();
                // holder.textView = ...
                // holder.Xml = ...
                // Or whatever you decided to have in each gridView item.
        
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
        
            holder.Xml = myXml;
            ...
        
            return convertView;
        }
        
        static class Holder() {
            TextView tv;
            Xml xml;
            ...
        }
        

        我假设您会使用 Xml 数组 (xmlArr)。 现在您可以根据需要选择使用每个 gridView 项目。您可以将每个视图/按钮/textView 设置为 onItemClickListener,也可以将整个 gridView 设置为 onItemClickListener(来自 YourActivity.java)。

        希望这会有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-07-26
          • 2020-09-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-03
          相关资源
          最近更新 更多