3

由于某种原因 setCurrentTab 在第一次之后没有切换标签。这是我使用的代码。

private OnClickListener buttonListener = new OnClickListener(){

    @Override
    public void onClick(View v) {
        tabHost.setCurrentTab(Integer.parseInt((String) v.getTag()));

    }
};

它连接到按钮,每个按钮都有一个标签,该标签等于要显示的选项卡的数量。当第一次单击按钮并调用此方法时,将显示选项卡。我还可以看到它执行了创建选项卡内容的代码。

但是,一旦一个选项卡显示过一次并且我已经移动到另一个选项卡,单击返回它就不起作用了。该方法肯定被调用,并且标签也是正确的。我输入了打印到日志的命令以确认这一点。此外,它第一次工作,所以一定没问题。

任何想法?

完整代码:

public class TestTabs extends TabbedScreen implements TabHost.TabContentFactory{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTabs();
    }

    @Override
    protected void initTabs() {

        addTab("0",this);
        addTab("1",this);
        addTab("2",this);
        addTab("3",this);

    }

    /*
     * 
     * @param extras The Bundle received in onCreate when this class is first created, and which contains the initial set of objects to be displayed.
     * @return An array containing all of the objects in the list.
     */
    protected Serializable[] getDataArray(Bundle extras) {
        int size = extras.size();
        Serializable[] data = new Serializable[size];
        for (int i = 0; i < size; i++) {
            data[i] = extras.getSerializable(Integer.toString(i));
        }

        return data;
    }

    @Override
    public View createTabContent(String tag) {


        // Get the data returned from the servelet and display it in the ListView
        Log.d("TestTabs","createTabContent");
         ListView lv = (ListView) findViewById(R.id.list_view);
         List<String> list1Strings = new ArrayList<String>();
         switch (Integer.parseInt(tag)){
            case 0:
                Log.d("TestTabs","Case 0");
                lv.setAdapter(new MyAdapter(this,lv,null));
                break;

            case 1:

                Log.d("TestTabs","Case 1");
                list1Strings.add("Item 21");
                list1Strings.add("Item 22");
                list1Strings.add("Item 23");
                list1Strings.add("Item 24");
                lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
                break;

            case 2:

                Log.d("TestTabs","Case 2");
                list1Strings.add("Item 31");
                list1Strings.add("Item 32");
                list1Strings.add("Item 33");
                list1Strings.add("Item 34");
                lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
                break;      

            case 3:

                Log.d("TestTabs","Case 3");
                list1Strings.add("Item 41");
                list1Strings.add("Item 42");
                list1Strings.add("Item 43");
                list1Strings.add("Item 44");
                lv.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
                break;      
        }

        return lv;
    }


    protected void addTab(String tabName, TabHost.TabContentFactory tabFactory){
        TabSpec tabSpec = tabHost.newTabSpec(tabName);
        tabSpec.setIndicator(tabName);                // Don't set tab layout since we are going to make it invisible
        tabSpec.setContent(tabFactory);
        tabHost.addTab(tabSpec);

        addButton(tabName);


    }

    protected void addButton(String tabName){
        Button button = (Button) buttonHolder.getChildAt(nextChild);
        button.setText(tabName);
        button.setVisibility(View.VISIBLE);
        button.setOnClickListener(buttonListener);
        nextChild--;
    }


    private OnClickListener buttonListener = new OnClickListener(){

        @Override
        public void onClick(View v) {
            v.setBackgroundResource(R.drawable.tab_selected);
            tabHost.setCurrentTab(Integer.parseInt((String) v.getTag()));
            Log.d("TabbedScreen","Set tab to " + v.getTag());
            View view;
            for (int i=0; i< childCount; i++)
                if ((view = buttonHolder.getChildAt(i)) != v){
                    view.setBackgroundResource(R.drawable.button_tab);
                    view.invalidate();
                }

        }
    };
}

XML:

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


  <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:roundedListView="http://schemas.android.com/apk/res/com.applicat.meuchedet"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
       android:layout_below="@id/content_screen_user_details">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
            <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" 
                android:layout_height="wrap_content" android:visibility="gone"/>            
            <LinearLayout android:id="@+id/tabButtons" android:layout_width="wrap_content" android:layout_height="20dip">
                <Button android:layout_height="wrap_content" android:layout_width="0dip" 
                    android:layout_weight="1.0" android:id="@+id/button3" 
                    android:background="@drawable/button_tab" android:visibility="invisible"
                    android:tag="3"/>
                <Button android:layout_height="wrap_content" android:layout_width="0dip" 
                    android:layout_weight="1.0" android:id="@+id/button2" 
                    android:background="@drawable/button_tab" android:visibility="invisible"
                     android:tag="2"/>
                <Button android:layout_height="wrap_content" android:layout_width="0dip" 
                    android:layout_weight="1.0" android:id="@+id/button1" 
                    android:background="@drawable/button_tab" android:visibility="invisible"
                    android:tag="1"/>
                <Button android:layout_height="wrap_content" android:layout_width="0dip" 
                    android:layout_weight="1.0" android:id="@+id/button0" 
                    android:background="@drawable/tab_selected" android:visibility="invisible"
                    android:tag="0"/>
            </LinearLayout> 
        </FrameLayout>

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <com.applicat.meuchedet.views.RoundedListView
        android:id="@+id/list_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:paddingLeft="2dip" 
        android:paddingRight="2dip" 
        android:dividerHeight="1dip"
        android:footerDividersEnabled="true"
        android:listSelector="@drawable/listview_selected_item_background"
        android:fadingEdge="none"
        android:cacheColorHint = "#00000000"
        roundedListView:radius="20"
        roundedListView:border="2"
        />
        </FrameLayout>
    </LinearLayout>
</TabHost>

编辑:

我想知道问题是否在行中:

ListView lv = (ListView) findViewById(R.id.list_view);

我每次都取回同一个对象吗?如果是这样,我如何根据FrameLayout中定义的每次获得一个新的。

4

4 回答 4

1

您如何确定分配给 xml 中按钮的值与仅使用 nextChild-- 获得的值相同。

不是很好的解决方案,你可以测试它。

void addButton(String tabName){ 
Button button = (Button) buttonHolder.getChildAt(nextChild);
button.getTag() !=  String.valueOf(nextChild); ERROR;  

但最好将其设置为与 buttonHolder.getChildAt() 一样

    Button button = (Button) buttonHolder.getChildAt(nextChild);
    button.setTag(String.valueOf(nextChild));

修改的:

protected void addButton(String tabName){
    Button button = (Button) buttonHolder.getChildAt(nextChild);
    // ----------- modified -----------
    button.setTag(String.valueOf(nextChild));
    button.setText(tabName);
    button.setVisibility(View.VISIBLE);
    button.setOnClickListener(buttonListener);
    nextChild--;
}


private OnClickListener buttonListener = new OnClickListener(){

    @Override
    public void onClick(View v) {
        v.setBackgroundResource(R.drawable.tab_selected);
        // ----------- modified -----------
        Button btc = (Button)v;
        int idx;
        try {
          idx = Integer.parseInt((String) btc.getTag());
            } catch(NumberFormatException exx) {
            System.out.println("Could not parse " + exx);
            } 
        if ( idx < childCount) {
           Log.d("TabbedScreen","Set tab to " + String.valueOf(idx);
           Button butv;
           for (int i=0; i< childCount; i++) 
            if ( i != idx){
            butv = (Button)  buttonHolder.getChildAt(i);    
            butv.setBackgroundResource(R.drawable.button_tab);
            butv.invalidate();
            }
           tabHost.setCurrentTab(idx);
           tabHost.focusCurrentTab(idx);
        }
        // ----------- modified ----------- 

    }
};

}

于 2012-07-29T21:42:20.250 回答
1

为了使用getTag,您必须先设置它。所以我相信你忘记在你的方法中添加以下行addButton

button.setTag(tabName);
于 2012-07-19T14:20:15.400 回答
0

可能是 getTag() 创建问题只是将其返回值存储在变量中检查值是否正确(您实际想要的整数否)?

于 2012-07-27T13:04:46.917 回答
0

用这个

    ListView lv = getListView();

代替

ListView lv = (ListView) findViewById(R.id.list_view);

getListView()方法返回与“当前”布局关联的列表视图。

此外,要使其正常工作,您必须将 xml 中的列表视图 ID 更改为

<ListView
          android:id="@+android:id/list"
于 2012-07-27T05:04:43.573 回答