Android listview dans la mise en page de l'onglet à l'intérieur de dialogue ne peut pas obtenir des barres de défilement apparaissent

StackOverflow https://stackoverflow.com/questions/4492162

Question

J'ai une classe de dialogue personnalisée qui étend la boîte de dialogue. Dans ce que j'ai un onglet Mise en page avec 2 onglets. Dans chaque onglet, j'ai une vue liste. Tout fonctionne, mais je ne peux pas obtenir des barres de défilement apparaissent.

Voici mon 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>

et est ici une partie de mon code qui définit cette place:

    // 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);
Était-ce utile?

La solution

OK est ici le code de travail complet pour une classe de dialogue personnalisée qui contient une mise en page à onglets qui contient une listView. La première patte a une listView avec des rangées étant un textView et un imageView avec le imageView étant aligné à droite. La deuxième patte présente une liste image avec des rangées étant un seul textView. Les barres de défilement sont réglées sur une durée de fondu élevée pour les rendre toujours montrer. La fenêtre de dialogue elle-même est réglée à une taille statique pour empêcher la boîte de dialogue de redimensionnement lors du passage des pattes. La fenêtre de dialogue est également positionné plus bas sur l'écran, pas au centre. Les listviews utilisent des cartes personnalisées et est enregistré listView du deuxième onglet pour un menu contextuel.

J'ai renommé tout pour être plus générique et contiennent les noms de ONt notre produit, donc je l'ai fait quelques fautes de frappe lors du renommage mais je pense que tout est juste. Essayé de commenter le code mieux que je pouvais. Espérons que cela aide certaines personnes.

XML de la customDialog (de 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 XML de rangée (list_view_01_row.xml). Ceci est un textView, aligné à gauche et un imageView, aligné à droite. Le textView a été réglé à une hauteur plus grande pour forcer les lignes de Listview à être plus élevés. Le listView a également été mis à une largeur spécifique, ce qui pousse le imageView à droite afin d'aligner à droite il.

<?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 XML de rangée (list_view_02_row.xml). Identique à onglet 1, mais avec un seul textView, pas 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>

Et enfin la classe personnalisée de dialogue.

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;  
    }  
}

Autres conseils

Essayez de mettre tous les attributs de android:layout_width au sein de votre mise en page XML "fill_parent".

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top