【问题标题】:How to start new activity in same tab?如何在同一选项卡中开始新活动?
【发布时间】:2012-03-28 05:47:14
【问题描述】:

我有 2 个选项卡,名为:Tab1、Tab2
MainActivity 类扩展 TabActivity。
MainActivity.java

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mTabHost = getTabHost();
    Intent intentTab;
    intentTab = new Intent().setClass(this, Tab1Activity.class);
    mTabHost.addTab(mTabHost.newTabSpec("tab_1").setIndicator("Tab1")
            .setContent(intentTab));

    intentTab = new Intent().setClass(this, Tab2Activity.class);
    mTabHost.addTab(mTabHost.newTabSpec("tab_2").setIndicator("Tab2")
            .setContent(intentTab));



    mTabHost.setCurrentTab(0);

ma​​in.xml

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

   <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5dp" >
    </FrameLayout>

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="-4dp"
        android:layout_weight="0" />
</LinearLayout>

Tab1Activity 扩展 ListActvity,它解析 json 并在列表视图中列出结果。 单击列表视图中的项目时,我想在同一选项卡中启动新活动,即在 Tab-1 中。
我该怎么做??
Tab1Activity.java

    public class Tab1Activity extends ListActivity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab1);

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> myList = 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 Root
        root = json.getJSONArray(TAG_ROOT);

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

            // Storing each json item in variable

            String name = c.getString(TAG_NAME);
            String town = c.getString(TAG_TOWN);
            String totalRating = c.getString(TAG_TOTALRATING);

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

            // adding each child node to HashMap key => value

            map.put(TAG_NAME, "Name: " + name);
            map.put(TAG_TOWN, "Town: " + town);
            map.put(TAG_RATING, "Rating: " + totalRating);

            // adding HashList to ArrayList
            myList.add(map);

        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    /**
     * Updating parsed JSON data into ListView
     * */
    ListAdapter adapter = new SimpleAdapter(this, myList,
            R.layout.list_item, new String[] { TAG_NAME, TAG_TOWN,
                    TAG_RATING }, new int[] { R.id.name, R.id.address,
                    R.id.rating });

    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) {
            //what code should i write to start new activity in this tab i.e Tab1
        }

    });

}

}

在上面的代码中:

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

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            //what code should i write to start new activity in this tab i.e Tab1
        }

    });

我应该在这个 ItemClickListener 中编写什么代码来在这个选项卡中启动新活动,即 Tab-1 ???

tab1.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/background"
  android:orientation="vertical" >


  <ListView
    android:id="@id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:choiceMode="singleChoice"
    android:drawSelectorOnTop="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:scrollbars="vertical" />

 </LinearLayout>

list_item.xml

  <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >



    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dip"
        android:paddingTop="6dip"
        android:textColor="#43bd00"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/town"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dip"
        android:textColor="#acacac" >
    </TextView>

    <TextView
        android:id="@+id/rating"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="2dip"
        android:textColor="#acacac" >
    </TextView>
</LinearLayout>

谢谢!!!

【问题讨论】:

  • 我真的不确定。但我认为在列表视图的OnItemClick 侦听器中使用IntentstartActivity() 会在当前选项卡本身为Tab-1 的同一选项卡中启动活动。完成后不要忘记finish() 活动。很抱歉这个“不确定”,但在你得到答案之前,你为什么不试试呢?顺便说一句,好问题。 +1
  • 我写了这段代码:Intent testIntent=new Intent(view.getContext(), NextActivity.class); startActivity(testIntent); finish(); 新活动开始,但问题是标签消失了??
  • 因为这行不通。只需将新活动扩展到 TabActivity 并暂时将这些选项卡再次包含在此新活动中。我会试试这个,因为它有点有趣,如果我成功了,我会告诉你的。 \m/
  • 感谢@Exorcist 的回复,我通过以下链接解决了我的问题:Experience - Multiple Android Activities in a TabActivity

标签: android listview tabactivity


【解决方案1】:

将您的活动用作选项卡、片段的内容。片段可能有自己的后台堆栈。

查看链接:

http://android.codeandmagic.org/2011/07/android-tabs-with-fragments/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    相关资源
    最近更新 更多