内内对话框选项卡布局Android的列表视图不能让滚动条显示出来 [英] Android listview inside tab layout inside dialog can't get scroll bars to show up

查看:109
本文介绍了内内对话框选项卡布局Android的列表视图不能让滚动条显示出来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展对话框的自定义对话框类。这里面我有一个选项卡布局与2个选项卡。在每个选项卡我有一个列表视图。一切正常,但我不能让滚动条显示出来。

下面是我的XML:

 < XML版本=1.0编码=UTF-8&GT?;

< TabHost
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:ID =@ + ID / TabHost01
机器人:layout_width =300dp
机器人:layout_height =300dp>

    <的LinearLayout
    机器人:方向=垂直
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT>

    < TabWidget
        机器人:ID =@机器人:ID /标签
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT/>

    <的FrameLayout
        机器人:ID =@机器人:ID / tabcontent
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT>

    <的ListView
        机器人:ID =@ + ID / ListView01
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:滚动条=垂直
        机器人:scrollbarAlwaysDrawVerticalTrack =真/>

    <的ListView
        机器人:ID =@ + ID / ListView02
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:滚动条=垂直
        机器人:scrollbarAlwaysDrawVerticalTrack =真/>

    < /的FrameLayout>
< / LinearLayout中>
< / TabHost>
 

和这里是我的code部分,设置这个了:

  //得到这个窗口的布局参数,所以我们可以改变位置
WindowManager.LayoutParams PARAMS = getWindow()的getAttributes()。

    //改变位置。 0,0为中心
    params.x = 0;
    params.y = 250;
    。this.getWindow()setAttributes(PARAMS);

    //这个对话框没有标题
requestWindowFeature(Window.FEATURE_NO_TITLE);

的setContentView(R.layout.myLayout);

//实例化列表视图中为每个标签
ListView控件listView01 =(ListView控件)findViewById(R.id.ListView01);
ListView控件listView02 =(ListView控件)findViewById(R.id.ListView02);


//实例化和设置我们的自定义列表视图的适配器
listViewAdapter01 =新ListViewAdapter01(上下文);
listView01.setAdapter(listViewAdapter01);

listViewAdapter02 =新ListViewAdapter02(上下文);
listView02.setAdapter(listViewAdapter02);

//从XML得到我们tabHost
TabHost标签=(TabHost)findViewById(R.id.TabHost01);
tabs.setup();

//创建标签1
TabHost.TabSpec TAB1 = tabs.newTabSpec(TAB1);
tab1.setContent(R.id.listView01);
tab1.setIndicator(列表1);
tabs.addTab(TAB1);

//创建标签2
TabHost.TabSpec TAB2 = tabs.newTabSpec(TAB2);
tab2.setContent(R.id.listView02);
tab2.setIndicator(列出01);
tabs.addTab(TAB2);
 

解决方案

确定这里是完整的工作code为包含一个标签式的布局,其中包含一个ListView自定义对话框类。第一个选项卡具有行是一个TextView和ImageView的一个ImageView的被右对齐的列表视图。第二个选项卡具有行是一个TextView的列表视图。滚动条被设置为一个高的衰落持续时间,以使它们总是显示。对话窗口本身被设置为静态大小至prevent从切换标签时调整大小的对话框。对话窗口也位于下在屏幕上,而不是在中心。该列表视图使用自定义适配器和第二个标签的ListView控件注册了一个右键菜单。

我已经更名一切是更通用和ONT包含我们产品的名称,所以重命名时,我可能已经取得了一些错别字,但我认为一切是正确的。试图作出评论code,尽我所能。希望这可以帮助一些人。

该customDialog的XML(custom_dialog_layout.xml):

 < XML版本=1.0编码=UTF-8&GT?;

< TabHost
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / TabHost01
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =300dip>

    <的LinearLayout
        机器人:方向=垂直
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT>

        < TabWidget
            机器人:ID =@机器人:ID /标签
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT/>

        <的FrameLayout
            机器人:ID =@机器人:ID / tabcontent
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT>

        <的ListView
            机器人:ID =@ + ID / listView01
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:滚动条=垂直
            机器人:scrollbarFadeDuration =百万/>

        <的ListView
            机器人:ID =@ + ID / listView02
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:滚动条=垂直
            机器人:scrollbarFadeDuration =百万/>

        < /的FrameLayout>
    < / LinearLayout中>
< / TabHost>
 

标签1的ListView行XML(list_view_01_row.xml)。这是一个TextView,左对齐和ImageView的,右对齐。 TextView的已被设置为一个较大的高度,以强制列表视图行要高。列表视图也被设置为特定的宽度,这推动的ImageView向右以右对齐。

 < XML版本=1.0编码=UTF-8&GT?;

<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:paddingBottom会=5dip
    机器人:paddingTop =5dip
    机器人:以下属性来=10dip
    机器人:paddingRight =10dip>

    < TableLayout
        机器人:ID =@ + ID / list_view_01_row_table_layout
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:stretchColumns =0>

        <的TableRow
            机器人:ID =@ + ID / list_view_01_row_table_row
            机器人:重力=center_vertical
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT>

                <的TextView
                    机器人:ID =@ + ID / list_view_01_row_text_view
                    机器人:TEXTSIZE =18sp
                    机器人:文字颜色=#FFFFFF
                    机器人:重力=center_vertical
                    机器人:layout_width =200dip
                    机器人:layout_height =75dip/>

                < ImageView的
                    机器人:ID =@ + ID / list_view_01_row_image_view
                    机器人:layout_width =WRAP_CONTENT
                    机器人:layout_height =WRAP_CONTENT/>
        < /的TableRow>
    < / TableLayout>
< / LinearLayout中>
 

标签2的ListView行XML(list_view_02_row.xml)。同片1,但用一个TextView的,没有ImageView的。

 < XML版本=1.0编码=UTF-8&GT?;

<的LinearLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:paddingBottom会=5dip
    机器人:paddingTop =5dip
    机器人:以下属性来=10dip
    机器人:paddingRight =10dip>

    < TableLayout
        机器人:ID =@ + ID / list_view_02_row_table_layout
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:stretchColumns =0>

        <的TableRow
            机器人:ID =@ + ID / list_view_02_row_table_row
            机器人:重力=center_vertical
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT>

            <的TextView
                机器人:ID =@ + ID / list_view_02_row_text_view
                机器人:TEXTSIZE =18sp
                机器人:文字颜色=#FFFFFF
                机器人:重力=center_vertical
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =75dip/>
        < /的TableRow>
    < / TableLayout>
< / LinearLayout中>
 

和最后的自定义对话框类。

 进口android.app.Dialog;
进口android.content.Context;
进口android.view.ContextMenu;
进口android.view.LayoutInflater;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.ViewGroup;
进口android.view.Window;
进口android.view.WindowManager;
进口android.view.ContextMenu.ContextMenuInfo;
进口android.widget.AdapterView;
进口android.widget.BaseAdapter;
进口android.widget.ImageView;
进口android.widget.ListView;
进口android.widget.TabHost;
进口android.widget.TextView;

/ **
 *这是一个将与2选项卡的标签视图的自定义对话框类。
 *选项卡1将是一个列表视图。标签2将是一个列表视图。
 *
 * /
公共类CustomDialog扩展对话框
{
    / **
     *我们的自定义列表视图适配器卡1的ListView(listView01)。
     * /
    ListView01Adapter listView01Adapter = NULL;

    / **
     *我们的自定义列表视图适配器TAB2的ListView(listView02)。
     * /
    ListView02Adapter listView02Adapter = NULL;

    / **
     *默认构造函数。
     *
     * @参数方面
     * /
    公共CustomDialog(上下文的背景下)
    {
        超(上下文);

        //得到这个窗口的布局参数,所以我们可以改变位置
        WindowManager.LayoutParams PARAMS = getWindow()的getAttributes()。

        //改变位置。 0,0为中心
        params.x = 0;
        params.y = 250;
        。this.getWindow()setAttributes(PARAMS);

        //这个对话框没有标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        的setContentView(R.layout.custom_dialog_layout);

        //实例化列表视图中为每个标签
        ListView控件listView01 =(ListView控件)findViewById(R.id.listView01);
        ListView控件listView02 =(ListView控件)findViewById(R.id.listView02);

        //注册为我们所有的listView02项目的上下文菜单
        registerForContextMenu(listView02);

        //实例化和设置我们的自定义列表视图的适配器
        listView01Adapter =新ListView01Adapter(上下文);
        listView01.setAdapter(listView01Adapter);

        listView02Adapter =新ListView02Adapter(上下文);
        listView02.setAdapter(listView02Adapter);

        //点​​击监听器绑定到listView01列表
        listView01.setOnItemClickListener(新AdapterView.OnItemClickListener()
        {
            公共无效onItemClick(适配器视图<> parentView,查看childView,INT位置,长ID)
            {
                //将关闭该对话框
                解雇();
            }
        });

        //点​​击监听器绑定到listView02列表
        listView02.setOnItemClickListener(新AdapterView.OnItemClickListener()
        {
            公共无效onItemClick(适配器视图<> parentView,查看childView,INT位置,长ID)
            {
                //将关闭该对话框
                解雇();
            }
        });

        //从XML得到我们tabHost
        TabHost标签=(TabHost)findViewById(R.id.TabHost01);
        tabs.setup();

        //创建标签1
        TabHost.TabSpec TAB1 = tabs.newTabSpec(TAB1);
        tab1.setContent(R.id.listView01);
        tab1.setIndicator(列表1);
        tabs.addTab(TAB1);

        //创建标签2
        TabHost.TabSpec TAB2 = tabs.newTabSpec(TAB2);
        tab2.setContent(R.id.listView02);
        tab2.setIndicator(列表2);
        tabs.addTab(TAB2);
    }

    / **
     *自定义列表适配器的listView01
     * /
    私有类ListView01Adapter扩展了BaseAdapter
    {
        公共ListView01Adapter(上下文的背景下)
        {

        }

        / **
         *这是用来返回多少行列表视图
         * /
        公众诠释getCount将()
        {
            //增加code在这里,以确定有多少成果有,硬codeD现在

            返回10;
        }

        / **
         *如果返回的任何对象重新presents在一排
         *名单。
         * /
        公共对象的getItem(INT位置)
        {
            返回的位置;
        }

        / **
         *用于返回任何自定义数据对象的ID。
         * /
        众长getItemId(INT位置)
        {
            返回的位置;
        }

        / **
         *这是用来在列表视图中定义的每一行。
         * /
        公共查看getView(INT位置,查看convertView,父母的ViewGroup)
        {
            查看排= convertView;

            //我们自定义的持有人将重新present各行的看法。请参阅下面的类。
            ListView01Holder支架=无效;

            如果(行== NULL)
            {
                LayoutInflater充气= getLayoutInflater();

                //从XML夸大我们的行
                行= inflater.inflate(R.layout.list_view_01_row,父母,假);

                //实例我们的持有人
                持有人=新ListView01Holder(行);

                //我们的保持器设置在排
                row.setTag(保持器);
            }
            其他
            {
                支架=(ListView01Holder)row.getTag();
            }

            返回行;
        }

        //我们自定义的持有人
        类ListView01Holder
        {
            //文本视图
            私人TextView的文本= NULL;

            //图像视图
            私人ImageView的形象= NULL;

            ListView01Holder(查看行)
            {
                //走出从XML文本视图
                文=(TextView的)row.findViewById(R.id.image);

                //增加code在这里设置文本
                text.setText();

                //让我们从XML图像视图
                图像=(ImageView的)row.findViewById(R.id.list_view_01_row_image_view);

                //增加code此处以确定加载,硬codeD现在哪个图像
                rating.setImageResource(R.drawable.image);
            }
        }
    }

    / **
     *自定义列表适配器listView02
     * /
    私有类ListView02Adapter扩展了BaseAdapter
    {
        公共ListView02Adapter(上下文的背景下)
        {

        }

        / **
         *这是用来返回多少行列表视图
         * /
        公众诠释getCount将()
        {
            //增加code在这里,以确定有多少成果有,硬codeD现在

            返回5;
        }

        / **
         *如果返回的任何对象重新presents在一排
         *名单。
         * /
        公共对象的getItem(INT位置)
        {
            返回的位置;
        }

        / **
         *用于返回任何自定义数据对象的ID。
         * /
        众长getItemId(INT位置)
        {
            返回的位置;
        }

        / **
         *这是用来在列表视图中定义的每一行。
         * /
        公共查看getView(INT位置,查看convertView,父母的ViewGroup)
        {
            查看排= convertView;
            ListView02Holder支架=无效;

            如果(行== NULL)
            {
                LayoutInflater充气= getLayoutInflater();

                行= inflater.inflate(R.layout.list_view_02_row,父母,假);
                持有人=新ListView02Holder(行);
                row.setTag(保持器);
            }
            其他
            {
                支架=(ListView02Holder)row.getTag();
            }

            返回行;
        }

        类ListView02Holder
        {
            私人TextView的文本= NULL;

            ListView02Holder(查看行)
            {
                文=(TextView的)row.findViewById(R.id.list_view_02_row_text_view);
                text.setText();
            }
        }
    }

    / **
     *当长preSS发生在我们listView02项目这就是所谓的。
     * /
    公共无效onCreateContextMenu(文本菜单菜单,视图V,ContextMenuInfo menuInfo)
    {
        super.onCreateContextMenu(菜单,V,menuInfo);

        menu.setHeaderTitle(上下文菜单);
        menu.add(0,v.getId(),0,删除);
    }

    / **
     *在我们的上下文菜单项被点击时,这就是所谓的。
     * /
    公共布尔onContextItemSelected(菜单项项)
    {
        如果(item.getTitle()==删除)
        {

        }
        其他
        {
            返回false;
        }

        返回true;
    }
}
 

I have a custom dialog class that extends Dialog. Inside this I have a Tab Layout with 2 tabs. In each tab I have a list view. Everything works but I can't get scroll bars to show up.

Here is my XML:

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

<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TabHost01"
android:layout_width="300dp"
android:layout_height="300dp">

    <LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

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

    <ListView
        android:id="@+id/ListView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:scrollbarAlwaysDrawVerticalTrack="true"/>

    <ListView
        android:id="@+id/ListView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:scrollbarAlwaysDrawVerticalTrack="true"/>

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

and here is part of my code that sets this up:

    // get this window's layout parameters so we can change the position
WindowManager.LayoutParams params = getWindow().getAttributes(); 

    // change the position. 0,0 is center
    params.x = 0;
    params.y = 250;
    this.getWindow().setAttributes(params); 

    // no title on this dialog
requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.myLayout);

// instantiate our list views for each tab
ListView listView01 = (ListView)findViewById(R.id.ListView01);
ListView listView02 = (ListView)findViewById(R.id.ListView02);


// instantiate and set our custom list view adapters
listViewAdapter01 = new ListViewAdapter01(context);
listView01.setAdapter(listViewAdapter01);

listViewAdapter02 = new ListViewAdapter02(context);
listView02.setAdapter(listViewAdapter02);

// get our tabHost from the xml
TabHost tabs = (TabHost)findViewById(R.id.TabHost01);
tabs.setup();

// create tab 1
TabHost.TabSpec tab1 = tabs.newTabSpec("tab1");
tab1.setContent(R.id.listView01);
tab1.setIndicator("List 1");
tabs.addTab(tab1);

// create tab 2
TabHost.TabSpec tab2 = tabs.newTabSpec("tab2");
tab2.setContent(R.id.listView02);
tab2.setIndicator("List 01");
tabs.addTab(tab2);

解决方案

OK here is the complete working code for a custom dialog class that contains a tabbed layout which contains a listView. The first tab has a listView with rows being a textView and an imageView with the imageView being right aligned. The second tab has a listView with rows being a single textView. The scroll bars are set to a high fade duration to make them always show. The dialog window itself is set to a static size to prevent the dialog from resizing when switching tabs. The dialog window is also positioned lower on the screen, not in the center. The listViews use custom adapters and the second tab's listView is registered for a context menu.

I have renamed everything to be more generic and ont contain names of our product, so I may have made some typos when renaming but I think everything is right. Tried to comment the code as best I could. Hope this helps some people.

The customDialog's XML (custom_dialog_layout.xml):

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

<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TabHost01"
    android:layout_width="fill_parent"
    android:layout_height="300dip">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

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

        <ListView
            android:id="@+id/listView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:scrollbarFadeDuration="1000000"/>

        <ListView
            android:id="@+id/listView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:scrollbarFadeDuration="1000000"/>

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

Tab 1 listView row XML (list_view_01_row.xml). This is a textView, left aligned and an imageView, right aligned. The textView has been set to a larger height in order to force the listView rows to be higher. The listView has also been set to a specific width, this pushes the imageView to the right in order to right align it.

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

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="5dip"
    android:paddingTop="5dip"
    android:paddingLeft="10dip"
    android:paddingRight="10dip">

    <TableLayout
        android:id="@+id/list_view_01_row_table_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0">

        <TableRow
            android:id="@+id/list_view_01_row_table_row"
            android:gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/list_view_01_row_text_view"
                    android:textSize="18sp"
                    android:textColor="#ffffff"  
                    android:gravity="center_vertical"
                    android:layout_width="200dip" 
                    android:layout_height="75dip" />

                <ImageView
                    android:id="@+id/list_view_01_row_image_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
        </TableRow>
    </TableLayout>
</LinearLayout>

Tab 2 listView row XML (list_view_02_row.xml). Same as tab 1 but with a single textView, no imageView.

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

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="5dip"
    android:paddingTop="5dip"
    android:paddingLeft="10dip"
    android:paddingRight="10dip">

    <TableLayout
        android:id="@+id/list_view_02_row_table_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="0">

        <TableRow
            android:id="@+id/list_view_02_row_table_row"
            android:gravity="center_vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/list_view_02_row_text_view"
                android:textSize="18sp"
                android:textColor="#ffffff"  
                android:gravity="center_vertical"
                android:layout_width="fill_parent" 
                android:layout_height="75dip" />
        </TableRow>
    </TableLayout>
</LinearLayout>

And finally the custom Dialog class.

import android.app.Dialog;
import android.content.Context;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TextView;

/**
 * This is a custom dialog class that will hold a tab view with 2 tabs.
 * Tab 1 will be a list view. Tab 2 will be a list view.
 * 
 */
public class CustomDialog extends Dialog
{
    /**
     * Our custom list view adapter for tab 1 listView (listView01).
     */
    ListView01Adapter listView01Adapter = null;

    /**
     * Our custom list view adapter for tab2 listView (listView02).
     */
    ListView02Adapter listView02Adapter = null;

    /**
     * Default constructor.
     * 
     * @param context
     */
    public CustomDialog(Context context)
    {
        super(context);

        // get this window's layout parameters so we can change the position
        WindowManager.LayoutParams params = getWindow().getAttributes(); 

        // change the position. 0,0 is center
        params.x = 0;
        params.y = 250;
        this.getWindow().setAttributes(params); 

        // no title on this dialog
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.custom_dialog_layout);

        // instantiate our list views for each tab
        ListView listView01 = (ListView)findViewById(R.id.listView01);
        ListView listView02 = (ListView)findViewById(R.id.listView02);

        // register a context menu for all our listView02 items
        registerForContextMenu(listView02);

        // instantiate and set our custom list view adapters
        listView01Adapter = new ListView01Adapter(context);
        listView01.setAdapter(listView01Adapter);

        listView02Adapter = new ListView02Adapter(context);
        listView02.setAdapter(listView02Adapter);

        // bind a click listener to the listView01 list
        listView01.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parentView, View childView, int position, long id)
            {                   
                // will dismiss the dialog
                dismiss();
            }           
        });

        // bind a click listener to the listView02 list
        listView02.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> parentView, View childView, int position, long id)
            {                   
                // will dismiss the dialog
                dismiss();          
            }           
        });

        // get our tabHost from the xml
        TabHost tabs = (TabHost)findViewById(R.id.TabHost01);
        tabs.setup();

        // create tab 1
        TabHost.TabSpec tab1 = tabs.newTabSpec("tab1");
        tab1.setContent(R.id.listView01);
        tab1.setIndicator("List 1");
        tabs.addTab(tab1);

        // create tab 2
        TabHost.TabSpec tab2 = tabs.newTabSpec("tab2");
        tab2.setContent(R.id.listView02);
        tab2.setIndicator("List 2");
        tabs.addTab(tab2);
    }

    /**
     * A custom list adapter for the listView01
     */
    private class ListView01Adapter extends BaseAdapter
    {        
        public ListView01Adapter(Context context)
        {

        }

        /**
         * This is used to return how many rows are in the list view
         */
        public int getCount()
        {
            // add code here to determine how many results we have, hard coded for now

            return 10;
        }

        /**
         * Should return whatever object represents one row in the
         * list.
         */
        public Object getItem(int position)
        {
            return position;
        }

        /**
         * Used to return the id of any custom data object.
         */
        public long getItemId(int position)
        {
            return position;
        }

        /**
         * This is used to define each row in the list view.
         */
        public View getView(int position, View convertView, ViewGroup parent)
        {            
            View row = convertView;

            // our custom holder will represent the view on each row. See class below.
            ListView01Holder holder = null;

            if(row == null)
            {                                                   
                LayoutInflater inflater = getLayoutInflater();

                // inflate our row from xml
                row = inflater.inflate(R.layout.list_view_01_row, parent, false);

                // instantiate our holder
                holder = new ListView01Holder(row);

                // set our holder to the row
                row.setTag(holder);
            }
            else
            {
                holder = (ListView01Holder)row.getTag();
            }

            return row;
        }

        // our custom holder
        class ListView01Holder
        {
            // text view
            private TextView text = null;

            // image view
            private ImageView image = null;

            ListView01Holder(View row)
            {    
                // get out text view from xml
                text = (TextView)row.findViewById(R.id.image);

                // add code here to set the text
                text.setText("");

                // get our image view from xml
                image = (ImageView)row.findViewById(R.id.list_view_01_row_image_view);

                // add code here to determine which image to load, hard coded for now
                rating.setImageResource(R.drawable.image);
            }
        }
    }

    /**
     * A custom list adapter for listView02
     */
    private class ListView02Adapter extends BaseAdapter
    {        
        public ListView02Adapter(Context context)
        {

        }

        /**
         * This is used to return how many rows are in the list view
         */
        public int getCount()
        {
            // add code here to determine how many results we have, hard coded for now

            return 5;
        }

        /**
         * Should return whatever object represents one row in the
         * list.
         */
        public Object getItem(int position)
        {
            return position;
        }

        /**
         * Used to return the id of any custom data object.
         */
        public long getItemId(int position)
        {
            return position;
        }

        /**
         * This is used to define each row in the list view.
         */
        public View getView(int position, View convertView, ViewGroup parent)
        {            
            View row = convertView;
            ListView02Holder holder = null;

            if(row == null)
            {                                                   
                LayoutInflater inflater = getLayoutInflater();

                row=inflater.inflate(R.layout.list_view_02_row, parent, false);
                holder = new ListView02Holder(row);
                row.setTag(holder);
            }
            else
            {
                holder = (ListView02Holder)row.getTag();
            }

            return row;
        }

        class ListView02Holder
        {
            private TextView text = null;

            ListView02Holder(View row)
            {
                text = (TextView)row.findViewById(R.id.list_view_02_row_text_view);
                text.setText("");
            }
        }
    }

    /**
     * This is called when a long press occurs on our listView02 items.
     */
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
    {
        super.onCreateContextMenu(menu, v, menuInfo);

        menu.setHeaderTitle("Context Menu");  
        menu.add(0, v.getId(), 0, "Delete");  
    }

    /**
     * This is called when an item in our context menu is clicked.
     */
    public boolean onContextItemSelected(MenuItem item)
    {  
        if(item.getTitle() == "Delete")
        {

        }  
        else
        {
            return false;
        }

        return true;  
    }  
}

这篇关于内内对话框选项卡布局Android的列表视图不能让滚动条显示出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆