Android 我想在不打开新活动的情况下切换选项卡

Android 我想在不打开新活动的情况下切换选项卡,android,android-intent,android-tabhost,Android,Android Intent,Android Tabhost,在这里,我的代码创建了三个选项卡,我想在第一个选项卡中单击列表项,其结果显示在第三个选项卡中,但当前它打开了新的活动窗口,我想单击列表项,将其移动到第三个选项卡并查看结果 public class AndroidTabLayoutActivity extends TabActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceSta

在这里,我的代码创建了三个选项卡,我想在第一个选项卡中单击列表项,其结果显示在第三个选项卡中,但当前它打开了新的活动窗口,我想单击列表项,将其移动到第三个选项卡并查看结果

public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TabHost tabHost = getTabHost();

    // Tab for Photos
    TabSpec photospec = tabHost.newTabSpec("Photos");
    photospec.setIndicator("Photos",
            getResources().getDrawable(R.drawable.icon_photos_tab));
    Intent photosIntent = new Intent(this, PhotosActivity.class);
    photospec.setContent(photosIntent);

    // Tab for Songs
    TabSpec songspec = tabHost.newTabSpec("Songs");
    // setting Title and Icon for the Tab
    songspec.setIndicator("Songs",
            getResources().getDrawable(R.drawable.icon_songs_tab));
    Intent songsIntent = new Intent(this, SongsActivity.class);
    songspec.setContent(songsIntent);

    // Tab for Videos
    TabSpec videospec = tabHost.newTabSpec("Videos");
    videospec.setIndicator("Videos",
            getResources().getDrawable(R.drawable.icon_videos_tab));
    Intent videosIntent = new Intent(this, VideosActivity.class);
    videospec.setContent(videosIntent);

    // Adding all TabSpec to TabHost
    tabHost.addTab(photospec); // Adding photos tab
    tabHost.addTab(songspec); // Adding songs tab
    tabHost.addTab(videospec); // Adding videos tab
}
}

表1**********************************************************


}

您的代码需要做太多的工作

您可以尝试
PagerSlidingTabStrip
在其就绪项目中添加选项卡和动态页面。您可以从该链接下载并将其导入Eclipse

public class PhotosActivity extends ListActivity {

// url to make request
private static String url = "http://api.androidhive.info/contacts/";

// JSON Node names
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_GENDER = "gender";
private static final String TAG_PHONE = "phone";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
private static final String TAG_PHONE_OFFICE = "office";

// contacts JSONArray
JSONArray contacts = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photos_layout);

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();

    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting Array of Contacts
        contacts = json.getJSONArray(TAG_CONTACTS);

        // looping through All Contacts
        for(int i = 0; i < contacts.length(); i++){
            JSONObject c = contacts.getJSONObject(i);

            // Storing each json item in variable
            String id = c.getString(TAG_ID);
            String name = c.getString(TAG_NAME);
            String email = c.getString(TAG_EMAIL);
            String address = c.getString(TAG_ADDRESS);
            String gender = c.getString(TAG_GENDER);

            // Phone number is agin JSON Object
            JSONObject phone = c.getJSONObject(TAG_PHONE);
            String mobile = phone.getString(TAG_PHONE_MOBILE);
            String home = phone.getString(TAG_PHONE_HOME);
            String office = phone.getString(TAG_PHONE_OFFICE);

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
            map.put(TAG_ID, id);
            map.put(TAG_NAME, name);
            map.put(TAG_EMAIL, email);
            map.put(TAG_PHONE_MOBILE, mobile);

            // adding HashList to ArrayList
            contactList.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }


    /**
     * Updating parsed JSON data into ListView
     * */
    ListAdapter adapter = new SimpleAdapter(this, contactList,
            R.layout.list_item,
            new String[] { TAG_NAME, TAG_EMAIL, TAG_PHONE_MOBILE }, new int[] {
                    R.id.name, R.id.email, R.id.mobile });

    setListAdapter(adapter);

    // selecting single ListView item
    ListView lv = getListView();

    // Launching new screen on Selecting Single ListItem
    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // getting values from selected ListItem
            String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
            String cost = ((TextView) view.findViewById(R.id.email)).getText().toString();
            String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();

            // Starting new intent
            Intent in = new Intent(getApplicationContext(), VideosActivity.class);
            in.putExtra(TAG_NAME, name);
            in.putExtra(TAG_EMAIL, cost);
            in.putExtra(TAG_PHONE_MOBILE, description);
            startActivity(in);

        }
    });



}
public class VideosActivity extends Activity {
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_PHONE_MOBILE = "mobile";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.videos_layout);

    // getting intent data
    Intent in = getIntent();

    // Get JSON values from previous intent
    String name = in.getStringExtra(TAG_NAME);
    String cost = in.getStringExtra(TAG_EMAIL);
    String description = in.getStringExtra(TAG_PHONE_MOBILE);

    // Displaying all values on the screen
    TextView lblName = (TextView) findViewById(R.id.name_label);
    TextView lblCost = (TextView) findViewById(R.id.email_label);
    TextView lblDesc = (TextView) findViewById(R.id.mobile_label);

    lblName.setText(name);
    lblCost.setText(cost);
    lblDesc.setText(description);
}