动态在code,以滚动型增加的ListView动态、code、ListView

2023-09-04 05:47:53 作者:丶烟瘾i

我想一个的ListView 添加到滚动型里面的code。但它不工作的某些原因。我是新来这个。有人可以帮我吗?

 包net.pocketmagic.android.HeaderContentFooterGUI;

进口的java.util.ArrayList;
进口的java.util.HashMap;

进口android.app.Activity;
进口android.graphics.Color;
进口android.graphics.Typeface;
进口android.os.Bundle;
进口android.view.Gravity;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup.LayoutParams;
进口android.view.Window;
进口android.widget.Button;
进口android.widget.LinearLayout;
进口android.widget.ListView;
进口android.widget.RelativeLayout;
进口android.widget.ScrollView;
进口android.widget.SimpleAdapter;
进口android.widget.TextView;

/ * HeaderContentFooterGUI
 *
 *创建示例GUI与标题栏,滚动就能内容和页脚
 *
 *(C)2011拉杜Motisan,radu.motisan@gmail.com
 * www.pocketmagic.net
 * 版权所有。
 * /

公共类HeaderContentFooterGUI延伸活动{

    最终静态INT idTopLayout = Menu.FIRST + 100,
                            idBack = Menu.FIRST + 101,
                            idBotLayout = Menu.FIRST + 102;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        //隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //创建我们的首要内容载体
        RelativeLayout的global_panel =新RelativeLayout的(这一点);
        global_panel.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        global_panel.setGravity(Gravity.FILL);

        // +++++++++++++顶部的组件:头
        RelativeLayout的ibMenu =新RelativeLayout的(这一点);
        ibMenu.setId(idTopLayout);
        ibMenu.setBackgroundDrawable(getResources()getDrawable(R.drawable.line));
        INT ibMenuPadding =(INT)6;
        ibMenu.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams topParams =新RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        topParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        global_panel.addView(ibMenu,topParams);
        // TextView的在ibMenu:名片夹
        TextView的CTV =新的TextView(本);
        cTV.setText(Abirami超级城);
        cTV.setTextColor(Color.rgb(255255255));
        INT nTextH = 18;
        cTV.setTextSize(nTextH);
        cTV.setTypeface(Typeface.create(宋体,Typeface.BOLD));
        RelativeLayout.LayoutParams lpcTV =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        lpcTV.addRule(RelativeLayout.CENTER_IN_PARENT);
        ibMenu.addView(CTV,lpcTV);

        // +++++++++++++底部的组件:页脚
        nTextH = 13;
         ibMenuPadding =(INT)15;
        RelativeLayout的ibMenuBot =新RelativeLayout的(这一点);
        ibMenuBot.setId(idBotLayout);
        ibMenuBot.setBackgroundDrawable(getResources()getDrawable(R.drawable.line));
        ibMenuBot.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams botParams =新RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        botParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        global_panel.addView(ibMenuBot,botParams);
        // TextView的在ibMenu:名片夹

        TextView的cTVBot =新的TextView(本);
        cTVBot.setId(5001);
        cTVBot.setText(家);
        cTVBot.setTextColor(Color.rgb(179116197));
        cTVBot.setTextSize(nTextH);
        cTVBot.setTypeface(Typeface.create(宋体,Typeface.NORMAL));
        cTVBot.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams lpcTVBot =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        lpcTVBot.addRule(RelativeLayout.CENTER_VERTICAL);
        ibMenuBot.addView(cTVBot,lpcTVBot);

        TextView的cTVBot2 =新的TextView(本);
        cTVBot2.setId(5002);
        cTVBot2.setText(预订史);
        cTVBot2.setTextColor(Color.rgb(179116197));
        cTVBot2.setTextSize(nTextH);
        cTVBot2.setTypeface(Typeface.create(宋体,Typeface.NORMAL));
        cTVBot2.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams lpcTVBot2 =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        lpcTVBot2.addRule(RelativeLayout.RIGHT_OF,cTVBot.getId());
        lpcTVBot2.addRule(RelativeLayout.CENTER_VERTICAL);
        ibMenuBot.addView(cTVBot2,lpcTVBot2);

        TextView的cTVBot3 =新的TextView(本);
        cTVBot3.setId(5003);
        cTVBot3.setText(帮助?);
        cTVBot3.setTextColor(Color.rgb(179116197));
        cTVBot3.setTextSize(nTextH);
        cTVBot3.setTypeface(Typeface.create(宋体,Typeface.NORMAL));
        cTVBot3.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams lpcTVBot3 =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        lpcTVBot3.addRule(RelativeLayout.RIGHT_OF,cTVBot2.getId());
        lpcTVBot3.addRule(RelativeLayout.CENTER_VERTICAL);
        ibMenuBot.addView(cTVBot3,lpcTVBot3);

        TextView的cTVBot4 =新的TextView(本);
        cTVBot4.setId(5004);
        cTVBot4.setText(关于我们);
        cTVBot4.setTextColor(Color.rgb(179116197));
        cTVBot4.setTextSize(nTextH);
        cTVBot4.setTypeface(Typeface.create(宋体,Typeface.NORMAL));
        cTVBot4.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams lpcTVBot4 =新RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        lpcTVBot4.addRule(RelativeLayout.RIGHT_OF,cTVBot3.getId());
        lpcTVBot4.addRule(RelativeLayout.CENTER_VERTICAL);
        ibMenuBot.addView(cTVBot4,lpcTVBot4);


        // +++++++++++++ MIDDLE组件:我们所有的GUI内容
        的LinearLayout midLayout =新的LinearLayout(本);
        midLayout.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        midLayout.setOrientation(LinearLayout.VERTICAL);
        RelativeLayout.LayoutParams midParams =新RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
        midParams.addRule(RelativeLayout.ABOVE,ibMenuBot.getId());
        midParams.addRule(RelativeLayout.BELOW,ibMenu.getId());
        global_panel.addView(midLayout,midParams);
        //涡旋 - 所以我们的内容将是可滚动的头部和底部之间
        滚动型VSCROLL =新的滚动型(本);
        vscroll.setId(6004);
        vscroll.setFillViewport(假);
        midLayout.addView(VSCROLL);
 

//低于code不工作的的 * 的**的 * 的***

 的ListView maListViewPerso =(ListView控件)findViewById(vscroll.getId());
            ArrayList的< HashMap的<字符串,字符串>>的listItem =新的ArrayList< HashMap的<字符串,字符串>>();
            HashMap的<字符串,字符串>地图;
            图=新的HashMap<字符串,字符串>();
            map.put(效价,电影票);
            map.put(说明,电影订票);
            map.put(IMG,将String.valueOf(R.drawable.reel_icon));
            listItem.add(图)



            图=新的HashMap<字符串,字符串>();
            map.put(效价,雪世界);
            map.put(说明,SnowWorld门票);
            map.put(IMG,将String.valueOf(R.drawable.reel_icon));
            listItem.add(图)

            图=新的HashMap<字符串,字符串>();
            map.put(效价,接吻汽车);
            map.put(说明,接吻车船票);
            map.put(IMG,将String.valueOf(R.drawable.reel_icon));
            listItem.add(图)

            图=新的HashMap<字符串,字符串>();
            map.put(效价,事件);
            map.put(说明,在广场活动);
            map.put(IMG,将String.valueOf(R.drawable.reel_icon));
            listItem.add(图)

            图=新的HashMap<字符串,字符串>();
            map.put(效价,画面);
            map.put(说明,画面@ Abirami);
            map.put(IMG,将String.valueOf(R.drawable.reel_icon));
            listItem.add(图)


            SimpleAdapter mSchedule =新SimpleAdapter(this.getBaseContext()的listItem,R.layout.main_menu_list,
                   新的String [] {IMG,效价,说明},新的INT [] {R.id.img,R.id.titre,R.id.description});


            maListViewPerso.setAdapter(mSchedule);




        的setContentView(global_panel);
    }


}

main_menu_list.xml
------------------

< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=横向
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    >

    < ImageView的
        机器人:ID =@ + ID / IMG

        机器人:layout_gravity =center_vertical

        机器人:填充=10px的
        机器人:layout_width =70dp
        机器人:layout_height =70dp/>
        />

    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:方向=垂直
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_gravity =center_vertical
        机器人:以下属性来=10px的
            机器人:paddingRight =10px的
                    机器人:layout_weight =1
        >

        < TextView的机器人:ID =@ + ID /滴度
             机器人:layout_width =FILL_PARENT
             机器人:layout_height =WRAP_CONTENT
             机器人:TEXTSIZE =26px
             机器人:TEXTSTYLE =黑体
             />

        < TextView的机器人:ID =@ + ID /说明
             机器人:layout_width =FILL_PARENT
             机器人:layout_height =WRAP_CONTENT
             />

    < / LinearLayout中>

< / LinearLayout中>
 

解决方案

不要一个ListView添加到滚动型如果您不知道exaclty你在做什么。

不要一个ListView添加到滚动型同时,如果您是在世界上最好的Andr​​oid程序员。

列表视图不应该获得的滚动型。即使你砍,我的意思是大黑客,这两种观点通过延长他们,让他们来滚动,还可以itemclickable等等等等,的东西会打破最终会。 还做了ListView控件不添加到滚动型。

观看这

vs code如何设置格式化类型 vs code格式化代码的操作方法

I am trying to add a ListView to a ScrollView inside the code. But it's not working for some reason. I am new to this. Can somebody help me out?

package net.pocketmagic.android.HeaderContentFooterGUI;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

/* HeaderContentFooterGUI
 * 
 * Create a sample GUI with a header bar, scroll-able content and a footer
 * 
 * (C) 2011 Radu Motisan, radu.motisan@gmail.com
 * www.pocketmagic.net
 * All rights reserved.
 */

public class HeaderContentFooterGUI extends Activity {

    final static int        idTopLayout = Menu.FIRST + 100,
                            idBack      = Menu.FIRST + 101,
                            idBotLayout = Menu.FIRST + 102;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Hide titlebar
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //Create our top content holder
        RelativeLayout global_panel = new RelativeLayout (this);
        global_panel.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        global_panel.setGravity(Gravity.FILL);

        // +++++++++++++ TOP COMPONENT: the header
        RelativeLayout ibMenu = new RelativeLayout(this);
        ibMenu.setId(idTopLayout);
        ibMenu.setBackgroundDrawable(getResources().getDrawable(R.drawable.line));
        int ibMenuPadding = (int) 6;
        ibMenu.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams topParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        topParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        global_panel.addView(ibMenu,topParams);
        // textview in ibMenu : card holder
        TextView cTV = new TextView(this);
        cTV.setText("Abirami Mega Mall");
        cTV.setTextColor(Color.rgb(255,255,255));
        int nTextH =  18;
        cTV.setTextSize(nTextH);
        cTV.setTypeface(Typeface.create("arial", Typeface.BOLD));
        RelativeLayout.LayoutParams lpcTV = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lpcTV.addRule(RelativeLayout.CENTER_IN_PARENT);
        ibMenu.addView(cTV, lpcTV);

        // +++++++++++++ BOTTOM COMPONENT: the footer
        nTextH =  13;
         ibMenuPadding = (int) 15;
        RelativeLayout ibMenuBot = new RelativeLayout(this);
        ibMenuBot.setId(idBotLayout);
        ibMenuBot.setBackgroundDrawable(getResources().getDrawable(R.drawable.line));
        ibMenuBot.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams botParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        botParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        global_panel.addView(ibMenuBot,botParams);
        // textview in ibMenu : card holder

        TextView cTVBot = new TextView(this);
        cTVBot.setId(5001);
        cTVBot.setText("Home"); 
        cTVBot.setTextColor(Color.rgb(179,116,197));
        cTVBot.setTextSize(nTextH);
        cTVBot.setTypeface(Typeface.create("arial", Typeface.NORMAL));
        cTVBot.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams lpcTVBot = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);   
        lpcTVBot.addRule(RelativeLayout.CENTER_VERTICAL);
        ibMenuBot.addView(cTVBot, lpcTVBot);

        TextView cTVBot2 = new TextView(this);
        cTVBot2.setId(5002);        
        cTVBot2.setText("Booking History");     
        cTVBot2.setTextColor(Color.rgb(179,116,197));
        cTVBot2.setTextSize(nTextH);
        cTVBot2.setTypeface(Typeface.create("arial", Typeface.NORMAL));
        cTVBot2.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams lpcTVBot2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);      
        lpcTVBot2.addRule(RelativeLayout.RIGHT_OF,cTVBot.getId() );
        lpcTVBot2.addRule(RelativeLayout.CENTER_VERTICAL);      
        ibMenuBot.addView(cTVBot2, lpcTVBot2);

        TextView cTVBot3 = new TextView(this);
        cTVBot3.setId(5003);        
        cTVBot3.setText("Help?");       
        cTVBot3.setTextColor(Color.rgb(179,116,197));
        cTVBot3.setTextSize(nTextH);
        cTVBot3.setTypeface(Typeface.create("arial", Typeface.NORMAL));
        cTVBot3.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams lpcTVBot3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);      
        lpcTVBot3.addRule(RelativeLayout.RIGHT_OF,cTVBot2.getId() );
        lpcTVBot3.addRule(RelativeLayout.CENTER_VERTICAL);      
        ibMenuBot.addView(cTVBot3, lpcTVBot3);      

        TextView cTVBot4 = new TextView(this);
        cTVBot4.setId(5004);        
        cTVBot4.setText("About Us");        
        cTVBot4.setTextColor(Color.rgb(179,116,197));
        cTVBot4.setTextSize(nTextH);
        cTVBot4.setTypeface(Typeface.create("arial", Typeface.NORMAL));
        cTVBot4.setPadding(ibMenuPadding,ibMenuPadding,ibMenuPadding,ibMenuPadding);
        RelativeLayout.LayoutParams lpcTVBot4= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);       
        lpcTVBot4.addRule(RelativeLayout.RIGHT_OF,cTVBot3.getId() );
        lpcTVBot4.addRule(RelativeLayout.CENTER_VERTICAL);      
        ibMenuBot.addView(cTVBot4, lpcTVBot4);      


        // +++++++++++++ MIDDLE COMPONENT: all our GUI content
        LinearLayout midLayout = new LinearLayout (this);
        midLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        midLayout.setOrientation(LinearLayout.VERTICAL);
        RelativeLayout.LayoutParams midParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        midParams.addRule(RelativeLayout.ABOVE,ibMenuBot.getId());
        midParams.addRule(RelativeLayout.BELOW,ibMenu.getId());
        global_panel.addView(midLayout,midParams );
        //scroll - so our content will be scrollable between the header and the footer
        ScrollView vscroll = new ScrollView(this);
        vscroll.setId(6004);    
        vscroll.setFillViewport(false);
        midLayout.addView(vscroll);

//below code not working *******

            ListView maListViewPerso = (ListView) findViewById(vscroll.getId());
            ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
            HashMap<String, String> map;     
            map = new HashMap<String, String>();
            map.put("titre", "Movie Ticket");
            map.put("description", "Movie Ticket Booking");
            map.put("img", String.valueOf(R.drawable.reel_icon));
            listItem.add(map);



            map = new HashMap<String, String>();
            map.put("titre", "Snow World");
            map.put("description", "SnowWorld Tickets");
            map.put("img", String.valueOf(R.drawable.reel_icon));
            listItem.add(map);

            map = new HashMap<String, String>();
            map.put("titre", "Kissing Car");
            map.put("description", "Kissing Car Tickets");
            map.put("img", String.valueOf(R.drawable.reel_icon));
            listItem.add(map);

            map = new HashMap<String, String>();
            map.put("titre", "Events");
            map.put("description", "Events in Mall");
            map.put("img", String.valueOf(R.drawable.reel_icon));
            listItem.add(map);

            map = new HashMap<String, String>();
            map.put("titre", "Screens");
            map.put("description", "Screens @ Abirami");
            map.put("img", String.valueOf(R.drawable.reel_icon));
            listItem.add(map);


            SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.main_menu_list,
                   new String[] {"img", "titre", "description"}, new int[] {R.id.img, R.id.titre, R.id.description});


            maListViewPerso.setAdapter(mSchedule);




        setContentView(global_panel);
    }


}

main_menu_list.xml
------------------

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >

    <ImageView
        android:id="@+id/img"

        android:layout_gravity="center_vertical"

        android:padding="10px"
        android:layout_width="70dp"
        android:layout_height="70dp"/>
        />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:paddingLeft="10px"
            android:paddingRight="10px"
                    android:layout_weight="1"
        >

        <TextView android:id="@+id/titre"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:textSize="26px"
             android:textStyle="bold"
             />

        <TextView android:id="@+id/description"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             />

    </LinearLayout>

</LinearLayout>

解决方案

Do not add a ListView to a ScrollView if you don't know exaclty what you are doing.

Do not add a ListView to a ScrollView also if you are the best android programmer in the World.

ListViews are not supposed to get in ScrollView. Even if you hack, and i mean big hack, both views by extending them and got them to scroll , also be itemclickable etc etc , something will break eventually. Also do not add a ListView to a ScrollView.

Watch this