1

(此处的所有屏幕截图:http: //imgur.com/a/uSQEe)您好,我正在为《使命召唤:黑色行动 2》开发完整指南。我已经基于并使用 ActionBarSherlockLibrary 和 SlidingMenu 库构建ScrollViewsGridViews布局: 它在 Android 4.0+ 中运行良好。但我对旧版本 Android 中的 Scroll(ScrollViews 和 GridViews)有疑问。 首先,我在我的 Corby 智能手机(Android 2.2)上对其进行了测试,而 ScrollView 根本不起作用。其次,我在 Android 2.3 模拟器上对其进行了测试:同样的故障。

这是此活动基于 GridView 的 Java 代码

        public class MainActivity extends SherlockActivity {
        final String pacchetto_app_prenium = Strings.PreniumPackage;
    final String prenium = Strings.prenium;
    final String free = Strings.free;
    private String LicenzaCaricata;

    private MissionsGridviewAdapter mAdapter1;
    private ArrayList<String> listMaps1;
    private ArrayList<Integer> listImages1;
    private GridView gridView1;

    private static SlidingMenu menu;

    final static String pyrrhycvictory  = "Pyrrhic Victory";
    final static String celerium  = "Celerium";
    final static String oldwounds  = "Old Wounds";
    final static String timeandfate  = "Time And Fate";
    final static String fallenangel  = "Fallen Angel";
    final static String karma  = "Karma";
    final static String sufferwithme  = "Suffer With Me";
    final static String achillesveil  = "Achille's Veil";
    final static String odysseus  = "Odysseus";
    final static String cordisdie  = "Cordis Die";
    final static String judgmentday  = "Judgment Day";
    final static String strikeforcetraining  = "(SF) Strike Force Training";
    final static String fobspectre  = "(SF) FOB Spectre";
    final static String shipwreck  = "(SF)Shipwreck";
    final static String ied  = "(SF)I.E.D.";
    final static String secondchange  = "(SF)Second Change";
    final static String dispatch  = "(SF)Dispatch";

    String valoreOttenuto;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.c_missions);
        prepareList();

        AdView adView = (AdView) this.findViewById(R.id.adView);
        adView.loadAd(new AdRequest());

        menu = new SlidingMenu(context);
        menu.setMode(SlidingMenu.LEFT);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setShadowWidthRes(R.dimen.shadow_width);
        menu.setShadowDrawable(R.drawable.shadow);
        menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(activity, SlidingMenu.SLIDING_CONTENT);
        menu.setMenu(R.layout.main_slidingmenu);

        gridView1 = (GridView) findViewById(R.id.gridView1);
        mAdapter1 = new MissionsGridviewAdapter(this, listMaps1, listImages1);
        gridView1.setAdapter(mAdapter1);

        gridView1.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                valoreOttenuto = mAdapter1.getItem(position);
                if (valoreOttenuto.equals(pyrrhycvictory)) {
                    startClasse(CMpyrrhicVictory.class);
                } else if (valoreOttenuto.equals(celerium)) {
                    startClasse(CMcelerium.class);
                } else if (valoreOttenuto.equals(oldwounds)) {
                    startClasse(CMoldwounds.class);
                } else if (valoreOttenuto.equals(timeandfate)) {
                    Controllo(CMtime_and_fate.class);
                } else if (valoreOttenuto.equals(fallenangel)) {
                    Controllo(CMfallenangel.class);
                } else if (valoreOttenuto.equals(karma)) {
                    Controllo(CMkarma.class);
                } else if (valoreOttenuto.equals(sufferwithme)) {
                    Controllo(CMsufferwithme.class);
                } else if (valoreOttenuto.equals(achillesveil)) {
                    Controllo(CMachillesveil.class);
                } else if (valoreOttenuto.equals(achillesveil)) {
                    Controllo(CMachillesveil.class);
                } else if (valoreOttenuto.equals(cordisdie)) {
                    Controllo(CMcordisdie.class);
                } else if (valoreOttenuto.equals(judgmentday)) {
                    Controllo(CMjudgmentday.class);
                } else if (valoreOttenuto.equals(strikeforcetraining)) {
                    Controllo(CMSstrikeforcetraining.class);
                } else if (valoreOttenuto.equals(fobspectre)) {
                    Controllo(CMSfobspectre.class);
                } else if (valoreOttenuto.equals(shipwreck)) {
                    Controllo(CMSshipwreck.class);
                } else if (valoreOttenuto.equals(ied)) {
                    Controllo(CMSied.class);
                } else if (valoreOttenuto.equals(secondchange)) {
                    Controllo(CMSsecondchange.class);
                } else if (valoreOttenuto.equals(dispatch)) {
                    Controllo(CMSdispatch.class);}
            }
        });


        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setSubtitle("Campaign/");
        getSupportActionBar().setTitle("Missions");

        // START SLIDING MENU CONTROL
        Button c_missions = (Button) findViewById(R.id.c_missions);
        c_missions.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                             menu.toggle();
            }
        });

        Button c_intels = (Button) findViewById(R.id.c_allintels);
        c_intels.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startClasse(Cintels.class);
            }
        });

        Button m_classes = (Button) findViewById(R.id.m_classes);
        m_classes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startClasse(Mclasses.class);
            }
        });

        Button m_scorestreaks = (Button) findViewById(R.id.m_scorestreaks);
        m_scorestreaks.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startClasse(Mscorestreaks.class);
            }
        });

        Button m_maps = (Button) findViewById(R.id.m_maps);
        m_maps.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startClasse(Mmaps.class);
            }
        });

        Button m_ranks = (Button) findViewById(R.id.m_ranks);
        m_ranks.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startClasse(Mranks.class);
            }
        });

        Button m_emblems = (Button) findViewById(R.id.m_emblems);
        m_emblems.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startClasse(Memblems.class);
            }
        });

        Button m_callingcards = (Button) findViewById(R.id.m_callingcards);
        m_callingcards.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startClasse(Mcallingcards.class);
            }
        });

        Button z_weapons = (Button) findViewById(R.id.z_weapons);
        z_weapons.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startClasse(Zweapons.class);
            }
        });

        Button z_maps = (Button) findViewById(R.id.z_maps);
        z_maps.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startClasse(Zmaps.class);
            }
        }); };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.mainactivity_actionbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
                        menu.toggle();
            break;
        case R.id.settings_button:
            startClasse(SettingsActivity.class);
            break;
        case R.id.buy_button:
            BuyPRO();
            break;
        default:
            return super.onOptionsItemSelected(item);
        }
        return true;
    }

    private void startClasse(Class<?> ClasseDaAvviare) {
        Intent intent = new Intent(this, ClasseDaAvviare);
        startActivity(intent);
    }

    private void BuyPRO() {
        startActivity(new Intent(Intent.ACTION_VIEW,
                Uri.parse(Strings.LinkProMarket)));
    }

    private void Dialogo() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(Strings.DialogTitle);
        builder.setIcon(R.drawable.icona)
                .setMessage(Strings.DialogMessage)
                .setCancelable(true)
                .setPositiveButton(Strings.DialogPositiveButtonText,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                BuyPRO();
                            }
                        })
                .setNegativeButton(Strings.DialogNegativeButtonText,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                                finishActivity(0);
                            }
                        });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();
        ;
    }

    public void prepareList() {
        listMaps1 = new ArrayList<String>();

        listMaps1.add(pyrrhycvictory);
        listMaps1.add(celerium);
        listMaps1.add(oldwounds);
        listMaps1.add(timeandfate);
        listMaps1.add(fallenangel);
        listMaps1.add(karma);
        listMaps1.add(sufferwithme);
        listMaps1.add(achillesveil);
        listMaps1.add(odysseus);
        listMaps1.add(cordisdie);
        listMaps1.add(judgmentday);
        listMaps1.add(strikeforcetraining);
        listMaps1.add(fobspectre);
        listMaps1.add(shipwreck);
        listMaps1.add(ied);
        listMaps1.add(secondchange);
        listMaps1.add(dispatch);

        listImages1 = new ArrayList<Integer>();
        listImages1.add(R.drawable.m1);
        listImages1.add(R.drawable.m2);
        listImages1.add(R.drawable.m3);
        listImages1.add(R.drawable.m4);
        listImages1.add(R.drawable.m5);
        listImages1.add(R.drawable.m6);
        listImages1.add(R.drawable.m7);
        listImages1.add(R.drawable.m8);
        listImages1.add(R.drawable.m9);
        listImages1.add(R.drawable.m10);
        listImages1.add(R.drawable.m11);
        listImages1.add(R.drawable.m12);
        listImages1.add(R.drawable.m13);
        listImages1.add(R.drawable.m14);
        listImages1.add(R.drawable.m15);
        listImages1.add(R.drawable.m16);
        listImages1.add(R.drawable.m17);
    }
}

布局的XML代码:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <com.google.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="@string/pub_id" >
    </com.google.ads.AdView>

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        tools:listitem="@android:layout/simple_list_item_2" >
    </GridView>

</LinearLayout>

GridView 适配器 (MissionsGridviewAdapter) 的代码`

import java.util.ArrayList;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MissionsGridviewAdapter extends BaseAdapter {
    private ArrayList<String> listCountry;
    private ArrayList<Integer> listFlag;
    private Activity activity;

    public MissionsGridviewAdapter(Activity activity, ArrayList<String> listCountry,
            ArrayList<Integer> listFlag) {
        super();
        this.listCountry = listCountry;
        this.listFlag = listFlag;
        this.activity = activity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return listCountry.size();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return listCountry.get(position);
    }

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

    public static class ViewHolder {
        public ImageView imgViewFlag;
        public TextView txtViewTitle;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if (convertView == null) {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.c_missions_gridviewrow, null);

            view.txtViewTitle = (TextView) convertView
                    .findViewById(R.id.textView1);
            view.imgViewFlag = (ImageView) convertView
                    .findViewById(R.id.imageView1);

            convertView.setTag(view);
        } else {
            view = (ViewHolder) convertView.getTag();
        }

        view.txtViewTitle.setText(listCountry.get(position));
        view.imgViewFlag.setImageResource(listFlag.get(position));
        return convertView;
    }

}

`

4

1 回答 1

0

一个建议,使用 strings.xml 来存储您的静态字符串。

于 2013-11-27T11:53:53.660 回答