ListView onClick处理程序

时间:2012-11-06 04:23:55

标签: android android-layout android-intent

我有一个应用程序,我的设计有2个标签。 Tab1有listview,我想点击每个listitem给出一个细节。但是在项目的选项上,标签设计和Tab2数据和设计必须保持一致。那么我可以用相同的设计开始新的意图吗?如果是,应该复制设计代码,或者我应该为Tab View设计和每个Tab ??

单独制作类

或者有没有办法可以更新标签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>

logcat输出

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>

3 个答案:

答案 0 :(得分:0)

根据您的需要更新ArrayList,并使用notifydatasetchanged() for tab1更新列表项。

答案 1 :(得分:0)

Hello Brooklyn你的列表项目点击工作正常。

解决您必须使用的问题

  

TabGroupActivity扩展了ActivityGroup

在同一个标​​签下处理多项活动。

  

您可以浏览here

     

或者您可以使用标签here

查看其他方式的片段      
    

  

我希望它可以帮到你。

答案 2 :(得分:0)

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

现在点击列表视图项目的第二个framelayout的setvisibility是可见的,第一个framelayout是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>