我正在尝试向代码中的ListView
添加ScrollView
。但由于某种原因它不起作用。我是新来的。有人可以帮助我吗?
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);
//代码无效 * ** * ***
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>
答案 0 :(得分:2)
如果您不知道自己在做什么,请不要将ListView添加到ScrollView 。
如果您是世界上最好的Android程序员,也不要将ListView添加到ScrollView 。
ListViews不应该进入ScrollView。即使你破解,我的意思是大黑客,通过扩展它们并让它们滚动,也可以是itemclickable等等两种观点,最终会破坏。 另外,不要将ListView添加到ScrollView。
观看this
答案 1 :(得分:0)
您不能以这种方式引用ListView。你应该执行findViewById(/ listview元素的ID在这里 /)。
编辑:
假设你有:
<ListView android:layout_height="wrap_content" android:id="@+id/listView1" android:layout_weight="1" android:layout_width="fill_parent"></ListView>
您可以通过以下方式访问列表视图:
ListView l = findViewById(R.id.listView1);
希望这有帮助!
答案 2 :(得分:0)
你首先应该使用findViewById(android.R.id.list)在布局中获取ListView,然后你不能将这些项添加到ListView中,然后将其添加到scrollview。
答案 3 :(得分:0)
得到解决方案....这里是完整的代码......
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.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
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 );
ListView maListViewPerso = new ListView(this);
maListViewPerso.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
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);
midLayout.addView(maListViewPerso);
setContentView(global_panel);
}
}