0

我有一个应用程序,其中我的设计有 2 个选项卡。Tab1 具有列表视图,我希望单击每个列表项以提供详细信息。但是在单击项目时,选项卡设计和 Tab2 数据和设计必须保持相同。那么我可以用相同的设计开始新的意图吗?如果是的话应该复制设计代码还是我应该为标签视图设计和每个标签制作单独的类?

或者有什么方法可以更新选项卡 1 的视图?而且,如果我更新 tab1 的视图,将按下后退按钮将用户带到上一个视图或启动我的应用程序???

代码 :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);     
    setContentView(R.layout.activity_main);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);

    TabHost tabhost=(TabHost) findViewById(R.id.tabHost);
    tabhost.setup();

    tabhost.getTabWidget().setDividerDrawable(R.drawable.divider2);

    TabSpec spec1=tabhost.newTabSpec("Tab 1");
    spec1.setContent(R.id.tab1);
    spec1.setIndicator("Tab 1");

    TabSpec spec2=tabhost.newTabSpec("Tab 2");
    spec2.setIndicator("Tab 2");
    spec2.setContent(R.id.tab2);

    tabhost.addTab(spec1);
    tabhost.addTab(spec2);             

    tabhost.getTabWidget().getChildTabViewAt(0).setBackgroundDrawable(null);
    tabhost.getTabWidget().getChildTabViewAt(1).setBackgroundDrawable(null);


    String values[]={"Apple", "IOS", "Google", "Android"};

    ListView listview=(ListView) findViewById(R.id.listview);

    ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.listviewstyle,R.id.textlist, values);
    //ArrayAdapter<String> adapter2=new ArrayAdapter<String>(this, R.id.textlist2);           

    listview.setAdapter(adapter);

    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
        {
            //I want to update Tab1 now using this function and change into a completely new layout

                    TextView tv=(TextView) findViewById(R.id.frame2text);
            tv.setText(data);

            frm1=(FrameLayout) findViewById(R.id.frame1);
            frm2=(FrameLayout) findViewById(R.id.frame2);

            frm1.setVisibility(View.GONE);
            frm2.setVisibility(View.VISIBLE);

            islast=true;

        }
    });               

}

我的 android_main.xml

<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabHost"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">

<TabWidget 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"
android:tabStripEnabled="false"     
android:background="@drawable/tabwstyle"/>

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:paddingTop="60dp"
    android:id="@+id/tab1">     

    <FrameLayout 
        android:id="@+id/frame1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"    
        android:layout_height="wrap_content" 
        android:divider="@drawable/divider"
        android:dividerHeight="10dp" 
        android:paddingTop="15dp"
        android:smoothScrollbar="true"/>

    </FrameLayout>

    <FrameLayout 
        android:id="@+id/frame2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:visibility="gone">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:text="@string/testtxt"
            android:id="@+id/frame2text"/>
    </FrameLayout>
</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingTop="60dp"
    android:id="@+id/tab2">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:text="@string/testtxt"/>

</LinearLayout>
  </FrameLayout>
 </TabHost>

日志输出

11-06 18:50:06.990: E/AndroidRuntime(437): FATAL EXCEPTION: main
11-06 18:50:06.990: E/AndroidRuntime(437): java.lang.ClassCastException: android.widget.LinearLayout
11-06 18:50:06.990: E/AndroidRuntime(437):  at com.example.pain.killer.PainKiller$1.onItemClick(PainKiller.java:64)
11-06 18:50:06.990: E/AndroidRuntime(437):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
11-06 18:50:06.990: E/AndroidRuntime(437):  at android.widget.ListView.performItemClick(ListView.java:3382)
11-06 18:50:06.990: E/AndroidRuntime(437):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
11-06 18:50:06.990: E/AndroidRuntime(437):  at android.os.Handler.handleCallback(Handler.java:587)
11-06 18:50:06.990: E/AndroidRuntime(437):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-06 18:50:06.990: E/AndroidRuntime(437):  at android.os.Looper.loop(Looper.java:123)
11-06 18:50:06.990: E/AndroidRuntime(437):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-06 18:50:06.990: E/AndroidRuntime(437):  at java.lang.reflect.Method.invokeNative(Native Method)
11-06 18:50:06.990: E/AndroidRuntime(437):  at java.lang.reflect.Method.invoke(Method.java:521)
11-06 18:50:06.990: E/AndroidRuntime(437):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-06 18:50:06.990: E/AndroidRuntime(437):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-06 18:50:06.990: E/AndroidRuntime(437):  at dalvik.system.NativeStart.main(Native Method)

我的 listviewststyle.xml

<?xml version="1.0" encoding="utf-8"?>

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

<!--      <ImageView
    android:contentDescription="@string/img"
    android:id="@+id/icon"
    android:layout_width="40dp"
    android:layout_height="30dp"        
    android:layout_marginRight="10dp"
    android:layout_marginTop="4dp"
    android:src="@drawable/ic_launcher" >  
</ImageView> -->

<TextView  
android:id="@+id/textlist"  
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:textSize="16dp"
android:drawableLeft="@drawable/ic_launcher"
android:drawablePadding="6dp"
android:paddingTop="5dp"
android:typeface="serif"/>

<!--    <TextView 
    android:layout_marginTop="35dp"
    android:layout_marginLeft="-310dp"
    android:id="@+id/textlist2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:text="@string/testtxt"/> -->

</LinearLayout>
4

3 回答 3

0

ArrayList根据您的需要更新并使用tab1notifydatasetchanged()来更新列表项。

于 2012-11-06T04:32:17.513 回答
0

你好布鲁克林你的列表项点击工作正常。

要解决您的问题,您必须使用

TabGroupActivity 扩展了 ActivityGroup

在同一选项卡下处理多个活动。

你可以通过这里

或者您可以在此处查看带有标签的片段

我希望它可以帮助你。

于 2012-11-06T05:34:53.493 回答
0

使用您想要的新布局在相同的 xml 中创建另一个 FrameLayout。设置 framelayout 可见性 GONE。

现在在列表视图项目的项目单击上,第二个框架布局的可见性是可见的,而第一个框架布局是 GONE。

用这个更新你上面的代码

FrameLayout fm1,fm2;
boolean islast;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);        

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);     
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);

TabHost tabhost=(TabHost) findViewById(R.id.tabHost);
tabhost.setup();

tabhost.getTabWidget().setDividerDrawable(R.drawable.divider2);

TabSpec spec1=tabhost.newTabSpec("Tab 1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("Tab 1");

TabSpec spec2=tabhost.newTabSpec("Tab 2");
spec2.setIndicator("Tab 2");
spec2.setContent(R.id.tab2);

tabhost.addTab(spec1);
tabhost.addTab(spec2);             

tabhost.getTabWidget().getChildTabViewAt(0).setBackgroundDrawable(null);
tabhost.getTabWidget().getChildTabViewAt(1).setBackgroundDrawable(null);


String values[]={"Apple", "IOS", "Google", "Android"};
 fm1 = (FrameLayout) findViewById(R.id.fm1);
fm2 = (FrameLayout) findViewById(R.id.fm2);

ListView listview=(ListView) findViewById(R.id.listview);

ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.listviewstyle,R.id.textlist, values);
//ArrayAdapter<String> adapter2=new ArrayAdapter<String>(this, R.id.textlist2);           

listview.setAdapter(adapter);

listview.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {
        //I want to update Tab1 now using this function and change into a completely new layout
            Toast.makeText(MainActivity.this,"ITEM CLICK", Toast.LENGTH_SHORT).show();  
            fm1.setVisibility(View.GONE);
            fm2.setVisibility(View.VISIBLE);
            islast = true;

    }
});               
@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    if (islast) {
        fm1.setVisibility(View.VISIBLE);
        fm2.setVisibility(View.GONE);
        islast = false;
        startActivity(new Intent(MainActivity.this,MainActivity.class));
    }else {
        finish();
    }
}

}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
android:paddingTop="60dp"
android:id="@+id/tab1">     

//Update HERE below given code

</LinearLayout>

使用以下代码更新 tab1 的线性布局。

<FrameLayout
            android:id="@+id/fm1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <ListView
                android:id="@+id/listview"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:dividerHeight="10dp"
                android:paddingTop="15dp"
                android:smoothScrollbar="true" />
        </FrameLayout>

        <FrameLayout
            android:id="@+id/fm2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:visibility="gone" >

            <TextView
                android:id="@+id/txt1"
                android:layout_width="fill_parent"
                android:layout_height="50dp" />
        </FrameLayout>
于 2012-11-06T05:45:29.690 回答