Admob显示时,Android GridView项目顺序更改

时间:2012-03-13 12:59:03

标签: android gridview scroll admob

我搜索了这些问题并尝试了大部分给出的答案,但我的问题尚未解决。 我的网格视图有大约10-12个项目,每个项目都有一个ImageView和一个TextView。图像和文本是从资源动态加载的。

问题1:当网格滚动时,项目顺序发生变化。第一个项目下降,最后一个项目到顶部

问题2:当Admob广告加载到屏幕底部时,整个网格项目都在混合。即使没有任何滚动。

问题3:目前我已将onClickListeners放入ImageView Only。如何将相同的OnclickListener添加到相关的TextView

我使用了网络中每个位置都找到的常见gridview生成代码。

这是我的代码

public class ImageAdapter extends BaseAdapter{
      Context myContext;

      public ImageAdapter(Context _myContext){
         myContext = _myContext;
      }

      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
         View MyView = convertView;

         try{

         if ( convertView == null ){
            LayoutInflater li = ((Activity)myContext).getLayoutInflater();
            MyView = li.inflate(R.layout.weather_grid, null);

            TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text);        

            Resources res = getResources();
            String[] items = res.getStringArray(R.array.weather_items);

            final String[] titles = new String[items.length];
            int x = 0;
            for(String item:items){

                titles[x]=item;
                x++;
            }

           // getStringFromRes(titles[position]);
            tv.setText(titles[position]);

            // Add The Image!!!           
            final ImageView iv = (ImageView)MyView.findViewById(R.id.grid_item_image);
            Class<drawable> resources = R.drawable.class;
            Field[] fields = resources.getFields();
            String[] imageName = new String[fields.length];     
            int index = 0;
            for( Field field : fields )
            {

                if(field.getName().startsWith("weather")){
                    imageName[index] = field.getName();
                    index++;
                }
            }
            iv.setImageResource(getDrawable(myContext, imageName[position]));

            iv.setOnClickListener(new OnClickListener() {               
                @Override
                public void onClick(View v) {
                    System.out.println("Clicked Item = " +      titles[position]);
                    Bundle b = new Bundle();
                    if(titles[position].equals("Weather Overview")){
                        startActivity(new Intent(WeatherGridActivity.this, WeatherActivity.class));
                    }
                    if(titles[position].equals("Current Weather")){
                        b.putString("display", "current");
                        Intent intent = new Intent(WeatherGridActivity.this,WeatherActivity.class);
                        intent.putExtras(b);
                        startActivityForResult(intent, 0);
                        //startActivity(new Intent(WeatherGridActivity.this, WeatherActivity.class));
                    }
                    if(titles[position].equals("Ask a Question")){
                        startActivity(new Intent(WeatherGridActivity.this, AskQuestionActivity.class));
                    }
                    if(titles[position].equals("Average Temperature")){
                        startActivity(new Intent(WeatherGridActivity.this, AverageTemperatureActivity.class));
                    }
                }
            });
         }
         }catch(Exception e){
             System.out.println("Error Occured = " + e.getMessage());
             e.printStackTrace();
         }

         return MyView;
      }

      @Override
      public Object getItem(int arg0) {
         // TODO Auto-generated method stub
         return null;
      }

      @Override
      public long getItemId(int arg0) {
         // TODO Auto-generated method stub
         return 0;
      }

    @Override
    public int getCount() {
        return 10;
    }

      public int getDrawable(Context context, String name){
            Assert.assertNotNull(context);
            Assert.assertNotNull(name);
            return context.getResources().getIdentifier(name,"drawable", context.getPackageName());
       }

      public String getStringFromRes(String name){
            try{
                int resId = (Integer) R.string.class.getField(name).get(null);
               // Toast.makeText(MyContext, getResources().getString(resId), Toast.LENGTH_LONG).show();
                return getResources().getString(resId);
            }catch(Exception e){
                // no such string
                return "empty";
            }
        }
   }

这是xml

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg"
        android:orientation="vertical" >

        <GridView
            android:id="@+id/weather"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/bg"
            android:columnWidth="70dp"
            android:gravity="center_horizontal"
            android:horizontalSpacing="20dp"
            android:numColumns="auto_fit"
            android:padding="20dp"
            android:stretchMode="columnWidth"
            android:tileMode="repeat"
            android:verticalSpacing="20dp" >
        </GridView>

        <ImageView
            android:id="@+id/back_button"
            style="@style/book_button" />

        <com.google.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            ads:adSize="BANNER"
            ads:adUnitId="dummy id"
            ads:loadAdOnCreate="true"
            ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" />
    </LinearLayout>

</ScrollView>

我添加了RelativeLayout而不是LinerLayout和ScrollViews,但现在整个Grid都没有显示,但广告显示正常。 这是新的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/home_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/bg"
    >

    <GridView
        android:id="@+id/home_grid"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:columnWidth="100dp"
        android:rowHeight="30dp"
        android:gravity="center_horizontal"
        android:horizontalSpacing="5dp"
        android:numColumns="auto_fit"
        android:stretchMode="none"
        android:tileMode="repeat"
        android:verticalSpacing="30dp" 
        >
    </GridView>

    <com.google.ads.AdView
        android:layout_alignParentBottom="true"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="dummy id"
        ads:loadAdOnCreate="true"
        ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

非常感谢您的回答 谢谢

1 个答案:

答案 0 :(得分:0)

这是工作代码。这也解决了滚动问题,Admob问题和onClickListener问题。

 public class ImageAdapter extends BaseAdapter{
          Context myContext;

          public ImageAdapter(Context _myContext){
             myContext = _myContext;    
             layoutInflater = LayoutInflater.from(myContext);
                Resources res = getResources();
                String[] items = res.getStringArray(R.array.weather_items);           
                titles = new String[items.length];
                int x = 0;
                for(String item:items){
                    titles[x]=item;
                    x++;
                }

                Class<drawable> resources = R.drawable.class;
                Field[] fields = resources.getFields();
                imageName = new String[fields.length];     
                int index = 0;
                for( Field field : fields ){
                    if(field.getName().startsWith("weather")){
                        imageName[index] = field.getName();
                        index++;
                    }
                }

          }

          @Override
          public View getView(final int position, View convertView, ViewGroup parent) {
            // View MyView = convertView;
             View grid = null;
             try{

             if ( convertView == null ){
                 grid = new View(myContext);
               // LayoutInflater li = ((Activity)myContext).getLayoutInflater();
                grid = layoutInflater.inflate(R.layout.weather_grid, null);
             }else{
                 grid = (View)convertView;
             }
                TextView tv = (TextView)grid.findViewById(R.id.grid_item_text);        
                tv.setText(titles[position]);         
                ImageView iv = (ImageView)grid.findViewById(R.id.grid_item_image);

                iv.setImageResource(getDrawable(myContext, imageName[position]));

                iv.setOnClickListener(new OnClickListener() {               
                    @Override
                    public void onClick(View v) {
                        executeListners(position);
                    }
                });

                tv.setOnClickListener(new OnClickListener() {               
                    @Override
                    public void onClick(View v) {
                        executeListners(position);
                    }
                });
             }catch(Exception e){
                 System.out.println("Error Occured = " + e.getMessage());
                 e.printStackTrace();
             }

             return grid;
          }



          @Override
          public Object getItem(int arg0) {
             // TODO Auto-generated method stub
             return null;
          }

          @Override
          public long getItemId(int arg0) {
             // TODO Auto-generated method stub
             return 0;
          }

        @Override
        public int getCount() {
            return 10;
        }

          public int getDrawable(Context context, String name){
                Assert.assertNotNull(context);
                Assert.assertNotNull(name);
                return context.getResources().getIdentifier(name,"drawable", context.getPackageName());
           }

          public String getStringFromRes(String name){
                try{
                    int resId = (Integer) R.string.class.getField(name).get(null);
                   // Toast.makeText(MyContext, getResources().getString(resId), Toast.LENGTH_LONG).show();
                    return getResources().getString(resId);
                }catch(Exception e){
                    return "empty";
                }
            }
       }

xml文件没有变化。这适用于RelativeLayout或LinearLayout