单击活动选项卡重新加载当前活动

发布于 2024-11-18 02:22:21 字数 4672 浏览 5 评论 0原文

我有一个选项卡布局,活动显示在框架布局中。 如何通过再次单击“主页”-选项卡从选项卡“主页”重新加载当前活动?

TabTestActivity 类

public class TabTestActivity extends TabActivity implements OnClickListener{

    TabHost tabHost;

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


        //MENÜ////////////////////////////////////////////

            /** TabHost will have Tabs */
            tabHost = (TabHost)findViewById(android.R.id.tabhost);

            /** tid1 is firstTabSpec Id. Its used to access outside. */
            TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
            TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
            TabSpec thirdTabSpec = tabHost.newTabSpec("tid3");
            TabSpec fourthTabSpec = tabHost.newTabSpec("tid4");

            /** TabSpec setIndicator() is used to set name for the tab. */
            /** TabSpec setContent() is used to set content for a particular tab. */
            firstTabSpec.setIndicator(new MyView(this, R.drawable.tabhome, "Home")).setContent(new Intent(this,FirstGroup.class));
            secondTabSpec.setIndicator(new MyView(this, R.drawable.tabmsg, "MSGs")).setContent(new Intent(this,FirstGroup.class));
            thirdTabSpec.setIndicator(new MyView(this, R.drawable.tabprofil, "Profil")).setContent(new Intent(this,FirstGroup.class));
            fourthTabSpec.setIndicator(new MyView(this, R.drawable.tabmehr, "Mehr...")).setContent(new Intent(this,FirstGroup.class));

            /** Add tabSpec to the TabHost to display. */
            tabHost.addTab(firstTabSpec);
            tabHost.addTab(secondTabSpec);
            tabHost.addTab(thirdTabSpec);
            tabHost.addTab(fourthTabSpec);

            tabHost.setCurrentTab(0);
    }

    //LAYOUT OF TABS
    private class MyView extends LinearLayout {
        public MyView(Context c, int drawable, String label) {
            super(c);

            LinearLayout la = new LinearLayout(c);
            //la.setBackgroundColor(Color.parseColor("#3b5091"));
            la.setOrientation(LinearLayout.VERTICAL);
            la.setMinimumHeight(63);
            la.setPadding(0,8,0,0);

            ImageView iv = new ImageView(c);
            TextView tv = new TextView(c);

            iv.setImageResource(drawable);

            setPadding(0,0,2,0);
            setOrientation(LinearLayout.VERTICAL);

            tv.setText(label);
            tv.setGravity(0x01); /* Center */

            tv.setTextColor(Color.WHITE);

            la.addView(iv);
            la.addView(tv);
            addView(la);
            setBackgroundDrawable( getResources().getDrawable(R.drawable.tab_indicator));

        }

    }

FirstGroup 类

public class FirstGroup extends ActivityGroup {  

        // Keep this in a static variable to make it accessible for all the nesten activities, lets them manipulate the view  
    public static FirstGroup group;  

        // Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.  
    private ArrayList<View> history;  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
          super.onCreate(savedInstanceState);  
          this.history = new ArrayList<View>();  
          group = this;  

              // Start the root activity withing the group and get its view  
          View view = getLocalActivityManager().startActivity("CitiesActivity", new  
                                            Intent(this,CitiesActivity.class)  
                                            .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP))  
                                            .getDecorView();  

              // Replace the view of this ActivityGroup  
          replaceView(view);  

       }  

    public void replaceView(View v) {  
                // Adds the old one to history  
        history.add(v);  
                // Changes this Groups View to the new View.  
        setContentView(v);  
    }  

    public void back() {  
        if(history.size() > 0) {  
            history.remove(history.size()-1);  
            setContentView(history.get(history.size()-1));  
        }else {  
            finish();  
        }  
    }  

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
    if(keyCode == KeyEvent.KEYCODE_BACK)
    {
    FirstGroup.group.back();
    return true;
    }
    return super.onKeyDown(keyCode, event);
    }

}  

i have a Tab Layout and the Activities are displayed in a frameLayout.
How can i reload the current Activity from Tab "Home" by clicking again on "Home"-Tab ?

TabTestActivity-class

public class TabTestActivity extends TabActivity implements OnClickListener{

    TabHost tabHost;

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


        //MENÜ////////////////////////////////////////////

            /** TabHost will have Tabs */
            tabHost = (TabHost)findViewById(android.R.id.tabhost);

            /** tid1 is firstTabSpec Id. Its used to access outside. */
            TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
            TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
            TabSpec thirdTabSpec = tabHost.newTabSpec("tid3");
            TabSpec fourthTabSpec = tabHost.newTabSpec("tid4");

            /** TabSpec setIndicator() is used to set name for the tab. */
            /** TabSpec setContent() is used to set content for a particular tab. */
            firstTabSpec.setIndicator(new MyView(this, R.drawable.tabhome, "Home")).setContent(new Intent(this,FirstGroup.class));
            secondTabSpec.setIndicator(new MyView(this, R.drawable.tabmsg, "MSGs")).setContent(new Intent(this,FirstGroup.class));
            thirdTabSpec.setIndicator(new MyView(this, R.drawable.tabprofil, "Profil")).setContent(new Intent(this,FirstGroup.class));
            fourthTabSpec.setIndicator(new MyView(this, R.drawable.tabmehr, "Mehr...")).setContent(new Intent(this,FirstGroup.class));

            /** Add tabSpec to the TabHost to display. */
            tabHost.addTab(firstTabSpec);
            tabHost.addTab(secondTabSpec);
            tabHost.addTab(thirdTabSpec);
            tabHost.addTab(fourthTabSpec);

            tabHost.setCurrentTab(0);
    }

    //LAYOUT OF TABS
    private class MyView extends LinearLayout {
        public MyView(Context c, int drawable, String label) {
            super(c);

            LinearLayout la = new LinearLayout(c);
            //la.setBackgroundColor(Color.parseColor("#3b5091"));
            la.setOrientation(LinearLayout.VERTICAL);
            la.setMinimumHeight(63);
            la.setPadding(0,8,0,0);

            ImageView iv = new ImageView(c);
            TextView tv = new TextView(c);

            iv.setImageResource(drawable);

            setPadding(0,0,2,0);
            setOrientation(LinearLayout.VERTICAL);

            tv.setText(label);
            tv.setGravity(0x01); /* Center */

            tv.setTextColor(Color.WHITE);

            la.addView(iv);
            la.addView(tv);
            addView(la);
            setBackgroundDrawable( getResources().getDrawable(R.drawable.tab_indicator));

        }

    }

FirstGroup-class

public class FirstGroup extends ActivityGroup {  

        // Keep this in a static variable to make it accessible for all the nesten activities, lets them manipulate the view  
    public static FirstGroup group;  

        // Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.  
    private ArrayList<View> history;  

    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
          super.onCreate(savedInstanceState);  
          this.history = new ArrayList<View>();  
          group = this;  

              // Start the root activity withing the group and get its view  
          View view = getLocalActivityManager().startActivity("CitiesActivity", new  
                                            Intent(this,CitiesActivity.class)  
                                            .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP))  
                                            .getDecorView();  

              // Replace the view of this ActivityGroup  
          replaceView(view);  

       }  

    public void replaceView(View v) {  
                // Adds the old one to history  
        history.add(v);  
                // Changes this Groups View to the new View.  
        setContentView(v);  
    }  

    public void back() {  
        if(history.size() > 0) {  
            history.remove(history.size()-1);  
            setContentView(history.get(history.size()-1));  
        }else {  
            finish();  
        }  
    }  

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
    if(keyCode == KeyEvent.KEYCODE_BACK)
    {
    FirstGroup.group.back();
    return true;
    }
    return super.onKeyDown(keyCode, event);
    }

}  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ペ泪落弦音 2024-11-25 02:22:21

我不确定您在寻找什么,但如果您想在用户返回到特定选项卡时更改某些值,您可以在活动的 onResume 方法中执行此操作。

I'm not sure what you're looking for but if you want to change some values when the user returns to a specific tab you can do that in the onResume method of your activity.

等风来 2024-11-25 02:22:21

我也面临同样的问题,但我这样解决这个问题......
这是我的 TabActivity 类...

public class MainActivity extends TabActivity {
TabHost tabhost;
String cTab = "0";
String nTab;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    tabhost = getTabHost();

    TabSpec one = tabhost.newTabSpec("0");

    // setting Title and Icon for the Tab
    one.setIndicator("", getResources().getDrawable(R.drawable.ic_launcher));
    Intent songsIntent = new Intent(this, FirstActivity.class);
    one.setContent(songsIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    one.setContent(songsIntent);

    TabSpec two = tabhost.newTabSpec("1");
    // setting Title and Icon for the Tab
    two.setIndicator("", getResources().getDrawable(R.drawable.ic_launcher));
    Intent songsIntent1 = new Intent(this, SecondActivity.class);
    two.setContent(songsIntent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    two.setContent(songsIntent1);

    TabSpec three = tabhost.newTabSpec("2");
    // setting Title and Icon for the Tab

    three.setIndicator("",
            getResources().getDrawable(R.drawable.ic_launcher));
    Intent songsIntent4 = new Intent(this, ThirdActivity.class);
    three.setContent(songsIntent4.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    three.setContent(songsIntent4);

    TabSpec four = tabhost.newTabSpec("3");
    // setting Title and Icon for the Tab
    four.setIndicator("", getResources()
            .getDrawable(R.drawable.ic_launcher));
    Intent songsIntent5 = new Intent(this, FourthActivity.class);
    four.setContent(songsIntent5.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    four.setContent(songsIntent5);

    tabhost.addTab(one);
    tabhost.addTab(two);
    tabhost.addTab(three);
    tabhost.addTab(four);
    tabhost.setOnTabChangedListener(new OnTabChangeListener() {
        @Override
        public void onTabChanged(String arg0) {
            cTab = "" + tabhost.getCurrentTab();

        }
    });

    int numberOfTabs = tabhost.getTabWidget().getChildCount();
    for (int t = 0; t < numberOfTabs; t++) {
        tabhost.getTabWidget().getChildAt(t)
                .setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                            String currentSelectedTag = MainActivity.this
                                    .getTabHost().getCurrentTabTag();
                            nTab = currentSelectedTag;
                            System.out.println(" nTab  " + nTab);
                            System.out.println("  cTab  " + cTab);
                            if (cTab.equals(nTab)) {
                                if (nTab.equals("0")) {
                                    Intent intent = new Intent();
                                    intent.setAction("com.ensis.first");
                                    MainActivity.this.sendBroadcast(intent);
                                }
                                if (nTab.equals("1")) {
                                    Intent intent = new Intent();
                                    intent.setAction("com.ensis.second");
                                    MainActivity.this.sendBroadcast(intent);
                                }
                                if (nTab.equals("2")) {
                                    Intent intent = new Intent();
                                    intent.setAction("com.ensis.third");
                                    MainActivity.this.sendBroadcast(intent);
                                }
                                if (nTab.equals("3")) {
                                    Intent intent = new Intent();
                                    intent.setAction("com.ensis.fourth");
                                    MainActivity.this.sendBroadcast(intent);
                                }
                            }
                        }
                        return false;
                    }
                });
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}}

这是我的 FirstActivity 类。

  public class FirstActivity extends ActivityGroup{
  @Override
  protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

ok();
IntentFilter filter = new IntentFilter("com.ensis.first");
registerReceiver(myReceiver, filter);

/**/
    }

   private void ok() {
// TODO Auto-generated method stub
setContentView(R.layout.firstscreen);
Button bt=(Button)findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent it=new Intent(FirstActivity.this,SubActivity.class);
        replaceContentView("activity3", it);
    }
});
    }

    public void replaceContentView(String id, Intent newIntent) {
        View view =     getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
        this.setContentView(view);
    }

     private BroadcastReceiver myReceiver = new BroadcastReceiver()
     {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v("22222222222222222", "22222222222");
        ok();
    }
     };}
I don't know whether it is exactly suitable for your answer or not.change the code as your needs...

i also face the same problem but i resolve this issue like this way...
this is my TabActivity class...

public class MainActivity extends TabActivity {
TabHost tabhost;
String cTab = "0";
String nTab;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    tabhost = getTabHost();

    TabSpec one = tabhost.newTabSpec("0");

    // setting Title and Icon for the Tab
    one.setIndicator("", getResources().getDrawable(R.drawable.ic_launcher));
    Intent songsIntent = new Intent(this, FirstActivity.class);
    one.setContent(songsIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    one.setContent(songsIntent);

    TabSpec two = tabhost.newTabSpec("1");
    // setting Title and Icon for the Tab
    two.setIndicator("", getResources().getDrawable(R.drawable.ic_launcher));
    Intent songsIntent1 = new Intent(this, SecondActivity.class);
    two.setContent(songsIntent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    two.setContent(songsIntent1);

    TabSpec three = tabhost.newTabSpec("2");
    // setting Title and Icon for the Tab

    three.setIndicator("",
            getResources().getDrawable(R.drawable.ic_launcher));
    Intent songsIntent4 = new Intent(this, ThirdActivity.class);
    three.setContent(songsIntent4.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    three.setContent(songsIntent4);

    TabSpec four = tabhost.newTabSpec("3");
    // setting Title and Icon for the Tab
    four.setIndicator("", getResources()
            .getDrawable(R.drawable.ic_launcher));
    Intent songsIntent5 = new Intent(this, FourthActivity.class);
    four.setContent(songsIntent5.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    four.setContent(songsIntent5);

    tabhost.addTab(one);
    tabhost.addTab(two);
    tabhost.addTab(three);
    tabhost.addTab(four);
    tabhost.setOnTabChangedListener(new OnTabChangeListener() {
        @Override
        public void onTabChanged(String arg0) {
            cTab = "" + tabhost.getCurrentTab();

        }
    });

    int numberOfTabs = tabhost.getTabWidget().getChildCount();
    for (int t = 0; t < numberOfTabs; t++) {
        tabhost.getTabWidget().getChildAt(t)
                .setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                            String currentSelectedTag = MainActivity.this
                                    .getTabHost().getCurrentTabTag();
                            nTab = currentSelectedTag;
                            System.out.println(" nTab  " + nTab);
                            System.out.println("  cTab  " + cTab);
                            if (cTab.equals(nTab)) {
                                if (nTab.equals("0")) {
                                    Intent intent = new Intent();
                                    intent.setAction("com.ensis.first");
                                    MainActivity.this.sendBroadcast(intent);
                                }
                                if (nTab.equals("1")) {
                                    Intent intent = new Intent();
                                    intent.setAction("com.ensis.second");
                                    MainActivity.this.sendBroadcast(intent);
                                }
                                if (nTab.equals("2")) {
                                    Intent intent = new Intent();
                                    intent.setAction("com.ensis.third");
                                    MainActivity.this.sendBroadcast(intent);
                                }
                                if (nTab.equals("3")) {
                                    Intent intent = new Intent();
                                    intent.setAction("com.ensis.fourth");
                                    MainActivity.this.sendBroadcast(intent);
                                }
                            }
                        }
                        return false;
                    }
                });
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}}

This is my FirstActivity Class.

  public class FirstActivity extends ActivityGroup{
  @Override
  protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

ok();
IntentFilter filter = new IntentFilter("com.ensis.first");
registerReceiver(myReceiver, filter);

/**/
    }

   private void ok() {
// TODO Auto-generated method stub
setContentView(R.layout.firstscreen);
Button bt=(Button)findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent it=new Intent(FirstActivity.this,SubActivity.class);
        replaceContentView("activity3", it);
    }
});
    }

    public void replaceContentView(String id, Intent newIntent) {
        View view =     getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
        this.setContentView(view);
    }

     private BroadcastReceiver myReceiver = new BroadcastReceiver()
     {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.v("22222222222222222", "22222222222");
        ok();
    }
     };}
I don't know whether it is exactly suitable for your answer or not.change the code as your needs...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文