我有一个应用程序,其中我的设计有 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>