多个列表视图被剪切
当我尝试使用多个列表视图制作线性布局时,我遇到了问题。最初,当我添加多个包含文本视图和列表视图的自定义线性布局时,我无法向下滚动页面,所以我发现添加滚动视图可以解决这个问题,它确实做到了,但由于某种原因,它随后裁剪了线性布局视图,所以我只能看到每个列表的顶部。
以下是不同部分的代码:
主布局
<?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:weightSum="1">
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/whatsNewTitle"
android:text="Whats New?"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="30dip">
</TextView>
<Button android:layout_height="wrap_content"
android:id="@+id/home"
android:text="Home"
android:layout_weight="0.07"
android:layout_width="126dp">
</Button>
<ScrollView
android:id="@+id/sv1"
android:layout_width="fill_parent"
android:fillViewport="true"
android:layout_height="630dp">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:id="@+id/whats_new_content"
android:layout_height="fill_parent">
</LinearLayout>
</ScrollView>
</LinearLayout>
布局我正在膨胀并多次添加到主布局中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/new_date"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:textSize="25dip">
</TextView>
<ListView
android:id="@+id/new_files"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:background="#000000">
</ListView>
</LinearLayout>
创建多个列表视图并显示它们的主要活动:
public class WhatsNew extends Activity{
private Button homeBtn;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.whats_new_main);
CreateLastModDateList();
homeBtn = (Button) findViewById(R.id.home);
homeBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent myIntent = new Intent(getApplication().getApplicationContext(),
Main.class);
startActivityForResult(myIntent, 0);
}
});
}
private ArrayList<File> GenerateNewFileDates(){
Files.clearFilesList();
Files.buildFilesList(Files.getRoot());
return Files.getAllFiles();
}
private void CreateLastModDateList(){
ArrayList<File> files = GenerateNewFileDates();
ArrayList<String> allDates = new ArrayList<String>();
ArrayList<String> dates = new ArrayList<String>();
Context c = this.getApplicationContext();
LinearLayout l = (LinearLayout) findViewById(R.id.whats_new_content);
/** Builds a list of all file dates **/
for(File file : files){
Date lastModifiedDate = new Date(file.lastModified());
allDates.add(lastModifiedDate.toString());
}
/** For each date, check if we have already put that date into the dates array
if we have then we want to ignore it, otherwise add it to the dates array. **/
for(String date : allDates){
if (exists(dates,date) == false){
dates.add(date);
}
}
for(String date : dates){
ArrayList<String> test = new ArrayList<String>();
test = generateFileList(date, files);
WhatsNewLayout whatsNew = new WhatsNewLayout(c,test, date);
l.addView(whatsNew);
}
}
public ArrayList<String> generateFileList(String date, ArrayList<File> mFiles){
ArrayList<String> filesFromDate = new ArrayList<String>();
for(File file : mFiles){
Date lastModifiedDate = new Date(file.lastModified());
boolean x = lastModifiedDate.toString().equals(date);
if (x == true){
filesFromDate.add(file.getName());
}
}
return filesFromDate;
}
public boolean exists(ArrayList<String> list, String compare){
boolean result = false;
if(list.contains(compare)){
result = true;
}
return result;
}
}
自定义线性布局我膨胀:
public class WhatsNewLayout extends LinearLayout {
private LayoutInflater mInflater;
public WhatsNewLayout(Context context, ArrayList<String> files, String date) {
super(context);
mInflater = LayoutInflater.from(context);
View convertView = new View(context);
/** initialize variables **/
DataHolder holder;
Context c = this.getContext();
ArrayAdapter<String> list = new ArrayAdapter<String>(c,
android.R.layout.simple_list_item_1, files);
convertView = mInflater.inflate(R.layout.whats_new_item, null);
holder = new DataHolder();
holder.modDate = (TextView) convertView.findViewById(R.id.new_date);
holder.FileList = (ListView) convertView.findViewById(R.id.new_files);
convertView.setTag(holder);
holder.modDate.setText(date);
holder.FileList.setAdapter(list);
this.addView(convertView);
}
}
对大量代码感到抱歉,但我没有想要省略任何细节,以防它很重要,希望这就是一切,我确信它对于我对布局的调用来说确实是非常重要的,但我只是看不出是什么导致自定义布局被剪切。
如果需要更多详细信息,请告诉我。 =)
im having an issue when i try to make a linearlayout with multiple listviews. originally when i added multiple custom linearlayouts containing a textview and a listview i could not scroll down the page so i found out adding a scrollview would fix this, which it did but for some reason it then cropped the linearlayout view so i could only see the top part of each list.
heres the code for the different parts:
Main layout
<?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:weightSum="1">
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/whatsNewTitle"
android:text="Whats New?"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="30dip">
</TextView>
<Button android:layout_height="wrap_content"
android:id="@+id/home"
android:text="Home"
android:layout_weight="0.07"
android:layout_width="126dp">
</Button>
<ScrollView
android:id="@+id/sv1"
android:layout_width="fill_parent"
android:fillViewport="true"
android:layout_height="630dp">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:id="@+id/whats_new_content"
android:layout_height="fill_parent">
</LinearLayout>
</ScrollView>
</LinearLayout>
layout im inflating and adding multiple times into main layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/new_date"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:textSize="25dip">
</TextView>
<ListView
android:id="@+id/new_files"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:background="#000000">
</ListView>
</LinearLayout>
the main activity that creates the multiple listviews and displays them:
public class WhatsNew extends Activity{
private Button homeBtn;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.whats_new_main);
CreateLastModDateList();
homeBtn = (Button) findViewById(R.id.home);
homeBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent myIntent = new Intent(getApplication().getApplicationContext(),
Main.class);
startActivityForResult(myIntent, 0);
}
});
}
private ArrayList<File> GenerateNewFileDates(){
Files.clearFilesList();
Files.buildFilesList(Files.getRoot());
return Files.getAllFiles();
}
private void CreateLastModDateList(){
ArrayList<File> files = GenerateNewFileDates();
ArrayList<String> allDates = new ArrayList<String>();
ArrayList<String> dates = new ArrayList<String>();
Context c = this.getApplicationContext();
LinearLayout l = (LinearLayout) findViewById(R.id.whats_new_content);
/** Builds a list of all file dates **/
for(File file : files){
Date lastModifiedDate = new Date(file.lastModified());
allDates.add(lastModifiedDate.toString());
}
/** For each date, check if we have already put that date into the dates array
if we have then we want to ignore it, otherwise add it to the dates array. **/
for(String date : allDates){
if (exists(dates,date) == false){
dates.add(date);
}
}
for(String date : dates){
ArrayList<String> test = new ArrayList<String>();
test = generateFileList(date, files);
WhatsNewLayout whatsNew = new WhatsNewLayout(c,test, date);
l.addView(whatsNew);
}
}
public ArrayList<String> generateFileList(String date, ArrayList<File> mFiles){
ArrayList<String> filesFromDate = new ArrayList<String>();
for(File file : mFiles){
Date lastModifiedDate = new Date(file.lastModified());
boolean x = lastModifiedDate.toString().equals(date);
if (x == true){
filesFromDate.add(file.getName());
}
}
return filesFromDate;
}
public boolean exists(ArrayList<String> list, String compare){
boolean result = false;
if(list.contains(compare)){
result = true;
}
return result;
}
}
The custom linearlayout i inflate:
public class WhatsNewLayout extends LinearLayout {
private LayoutInflater mInflater;
public WhatsNewLayout(Context context, ArrayList<String> files, String date) {
super(context);
mInflater = LayoutInflater.from(context);
View convertView = new View(context);
/** initialize variables **/
DataHolder holder;
Context c = this.getContext();
ArrayAdapter<String> list = new ArrayAdapter<String>(c,
android.R.layout.simple_list_item_1, files);
convertView = mInflater.inflate(R.layout.whats_new_item, null);
holder = new DataHolder();
holder.modDate = (TextView) convertView.findViewById(R.id.new_date);
holder.FileList = (ListView) convertView.findViewById(R.id.new_files);
convertView.setTag(holder);
holder.modDate.setText(date);
holder.FileList.setAdapter(list);
this.addView(convertView);
}
}
Sorry about the large mass of code, but i didnt want to leave any details out in case it is important, hopefully this is everything, im sure its something really fundermental with my call of the layout but i just cannot see what is casuing the custom layout to be cut.
if any more details are required please let me know. =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将您的 listViews 放在垂直滚动中。您可以通过以下技巧在垂直滚动内拥有可滚动的 listView 。使用以下代码并享受吧!
listViewTouchAction 是一个全局整数值。
Put your listViews in a vertical scroll. You can have scrollable listView inside of a vertical scroll by the following trick. use the following code and enjoy!
listViewTouchAction is a global integer value.
ScrollView 中不可能有 ListView(因为 ListView 也是可滚动组件),这就是 ListView 被裁剪到屏幕上的原因。
如果您需要在屏幕上显示多个列表以让用户选择某些内容,您可以使用 Spinner 而不是列表
It is not possible to have a ListView inside ScrollView (since the ListView is also a scrollable component) this is why your ListView is cropped to the screen.
If you need to display several List on screen to let user select something, you may use Spinner instead of List