gpt4 book ai didi

Android,如何将 ListView 项目分成三部分?

转载 作者:太空狗 更新时间:2023-10-29 14:27:37 26 4
gpt4 key购买 nike

在我的 Activity 中,我有三个列表,每个列表有三个项目。以这种方式测试后(请参阅:Android, How to add ScrollView into screen which has some list items?),我发现当您在 Activity 中有一些列表时,无法滚动屏幕。此外,我发现这个答案 ( add multiple listview in one activity ) 将屏幕分为三部分,因此,我没有足够的空间来显示所有项目,屏幕也不可滚动。

一个愚蠢的解决方案是为每个项目创建 xml 并将其放入 Activity 的 xml 布局中。虽然它有效,但我必须复制/粘贴这么多行代码。

更好的方法是使用适配器,因此,在我的下一次尝试中,我可以将另外两个列表的结果添加到我的第一个列表中,然后将结果发送到适配器类。因此,适配器将显示列表中的所有项目。虽然,它解决了我关于屏幕滚动的问题(此时屏幕滚动 = 列表滚动),但我需要在每三个项目的顶部都有标题。

是否可以告诉适配器在每三个列表项之后添加例如 header.xml?

希望解释清楚!谢谢

最佳答案

主.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="true"
/>

标题.xml

<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" android:scrollbars="none"
style="?android:attr/listSeparatorTextViewStyle" />

下面给出java文件代码。

分段适配器.java

package com.android.listview;

import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import java.util.ArrayList;
import java.util.List;

abstract public class SectionedAdapter extends BaseAdapter {
abstract protected View getHeaderView(String caption, int index,
View convertView, ViewGroup parent);

private List
sections = new ArrayList
();
private static int TYPE_SECTION_HEADER = 1;

public SectionedAdapter() {
super();
}

public void addSection(String caption, Adapter adapter) {
sections.add(new Section(caption, adapter));
}

public Object getItem(int position) {
for (Section section : this.sections) {
if (position == 0) {
return (section);
}

int size = section.adapter.getCount() + 1;

if (position <>
return (section.adapter.getItem(position - 1));
}

position -= size;
}

return (null);
}

public int getCount() {
int total = 0;

for (Section section : this.sections) {
total += section.adapter.getCount() + 1; // add one for header
}

return (total);
}

public int getViewTypeCount() {
int total = 1; // one for the header, plus those from sections

for (Section section : this.sections) {
total += section.adapter.getViewTypeCount();
}

return (total);
}

public int getItemViewType(int position) {
int typeOffset = TYPE_SECTION_HEADER + 1; // start counting from here

for (Section section : this.sections) {
if (position == 0) {
return (TYPE_SECTION_HEADER);
}

int size = section.adapter.getCount() + 1;

if (position <>
return (typeOffset + section.adapter
.getItemViewType(position - 1));
}

position -= size;
typeOffset += section.adapter.getViewTypeCount();
}

return (-1);
}

public boolean areAllItemsSelectable() {
return (false);
}

public boolean isEnabled(int position) {
return (getItemViewType(position) != TYPE_SECTION_HEADER);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
int sectionIndex = 0;

for (Section section : this.sections) {
if (position == 0) {
return (getHeaderView(section.caption, sectionIndex,
convertView, parent));
}

int size = section.adapter.getCount() + 1;

if (position <>
return (section.adapter.getView(position - 1, convertView,
parent));
}

position -= size;
sectionIndex++;
}

return (null);
}

@Override
public long getItemId(int position) {
return (position);
}

class Section {
String caption;
Adapter adapter;

Section(String caption, Adapter adapter) {
this.caption = caption;
this.adapter = adapter;
}
}
}

选择.java

package com.commonsware.android.listview;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class SectionedDemo extends ListActivity {
private static String[] items = { "US", "UK", "CANADA", "JAPAN", "SINGAPORE",
"INDIA", "CHINA" };

private static String[] Sect = { "GOOGLE", "FACEBOOK","DELL" };

private static String[] Doc = { "FRONT", "TOP","BACK" };

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);

adapter.addSection("step 1", new ArrayAdapter(this,
android.R.layout.simple_list_item_1, items));

adapter.addSection("Step 2", new ArrayAdapter(this,
android.R.layout.simple_list_item_1, Sect));

adapter.addSection("Step 3", new ArrayAdapter(this,
android.R.layout.simple_list_item_1, Doc));

setListAdapter(adapter);
}

SectionedAdapter adapter = new SectionedAdapter() {
protected View getHeaderView(String caption, int index,
View convertView, ViewGroup parent) {
TextView result = (TextView) convertView;

if (convertView == null) {
result = (TextView) getLayoutInflater().inflate(
R.layout.header, null);
}

result.setText(caption);

return (result);
}
};
}

LINK LINK

关于Android,如何将 ListView 项目分成三部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10509306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com