活动变化不起作用。与谢尔洛克活动

时间:2013-10-30 09:26:41

标签: android android-activity actionbarsherlock

我只做了两个活动,这些是sherlockActivity。

我可以按标签。但是,不会改变任何东西。

以下是我的来源。

对不起。并欣赏。

MainActivity.java

package com.example.sherlocktest;

imports;

public class MainActivity extends SherlockActivity implements ActionBar.TabListener {

private ActionBar aBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cal);

    /** TabBar */
    aBar = getSupportActionBar();
    aBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    aBar.setTitle("Darphin's bar");

    ActionBar.Tab tab1 = aBar.newTab().setText("버튼1").setTabListener(this);
    ActionBar.Tab tab2 = aBar.newTab().setText("버튼2").setTabListener(this);
    ActionBar.Tab tab3 = aBar.newTab().setText("버튼3").setTabListener(this);

    aBar.addTab(tab1);
    aBar.addTab(tab2);
    aBar.addTab(tab3);
}

@Override
public void onTabSelected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
    if(tab.equals(aBar.getTabAt(0))) {
        Toast.makeText(this,"1",Toast.LENGTH_SHORT).show();
//          setContentView(R.layout.activity_cal);
    }
    else if(tab.equals(aBar.getTabAt(1))) {
        Toast.makeText(this,"2",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(MainActivity.this , ListActivity.class);
        startActivity(intent);
//          setContentView(R.layout.activity_list);
    }
    else if(tab.equals(aBar.getTabAt(2))) {
        Toast.makeText(this,"3",Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onTabUnselected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
}

@Override
public void onTabReselected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
}

ListActivity.java

package com.example.sherlocktest;

imports

public class ListActivity extends SherlockActivity implements ActionBar.TabListener {

private ActionBar aBar;
private ListView listView;
private List arrayList;

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

    /** TabBar */
    aBar = getSupportActionBar();
    aBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    aBar.setTitle("Darphin's bar");

    ActionBar.Tab tab1 = aBar.newTab().setText("버튼1").setTabListener(this);
    ActionBar.Tab tab2 = aBar.newTab().setText("버튼2").setTabListener(this);
    ActionBar.Tab tab3 = aBar.newTab().setText("버튼3").setTabListener(this);

    aBar.addTab(tab1);
    aBar.addTab(tab2);
    aBar.addTab(tab3);

    /** ListView */
    arrayList = new ArrayList<String>();

    arrayList.add("first");

    listView = new ListView(this);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_list, arrayList);
    listView.setAdapter(adapter);
}

@Override
public void onTabSelected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
    if(tab.equals(aBar.getTabAt(0))) {
        Toast.makeText(this,"1",Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(ListActivity.this , MainActivity.class);
        startActivity(intent);
    }
    else if(tab.equals(aBar.getTabAt(1))) {
        Toast.makeText(this,"2",Toast.LENGTH_SHORT).show();
//          setContentView(R.layout.activity_list);
    }
    else if(tab.equals(aBar.getTabAt(2))) {
        Toast.makeText(this,"3",Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onTabUnselected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
}

@Override
public void onTabReselected(Tab tab, android.support.v4.app.FragmentTransaction ft) {
}
}

activity_cal.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<CalendarView
    android:id="@+id/calendar"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

activity_list.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ListActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" >

</ListView>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/listView1"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="28dp"
    android:text="TextView" />

</RelativeLayout>

sherlockTestManifest.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ListActivity" >

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" >

</ListView>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/listView1"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="28dp"
    android:text="TextView" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

考虑使用appcompat-v7库而不是Sherlock,它包含大部分功能。

if(tab.equals(aBar.getTabAt(0))) {
      Toast.makeText(this,"1",Toast.LENGTH_SHORT).show();
//          setContentView(R.layout.activity_cal);
}
else if(tab.equals(aBar.getTabAt(1))
...

看起来这种比较不起作用。尝试为标签分配标签

此外,

arrayList = new ArrayList<String>();

arrayList.add("first");

listView = new ListView(this);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_list, arrayList);
listView.setAdapter(adapter);

你正在创建新的ListView,为什么?你甚至不把它放在任何地方。您应该找到包含findViewById的现有ListView。