Android 带ArrayAdapter的圆形(或无限)listview
我希望我的listview在滚动到末尾后重复所有行,依此类推。这称为无限或循环,您可以找到实现它的方法,但这些方法似乎都不适用于我的适配器。我尝试过这种方法:但当我把Android 带ArrayAdapter的圆形(或无限)listview,android,listview,android-listview,Android,Listview,Android Listview,我希望我的listview在滚动到末尾后重复所有行,依此类推。这称为无限或循环,您可以找到实现它的方法,但这些方法似乎都不适用于我的适配器。我尝试过这种方法:但当我把 @Override public int getCount() { return Integer.MAX_VALUE; } 此外,getItem Doessent似乎与我的适配器“配合使用” @Override public T getItem(int positi
@Override
public int getCount()
{
return Integer.MAX_VALUE;
}
此外,getItem Doessent似乎与我的适配器“配合使用”
@Override
public T getItem(int position)
{
return objects[position % objects.length];
}
有没有一种方法可以为我的适配器实现类似的功能?代码如下:
public class EntryAdapter extends ArrayAdapter<Item> {
private ArrayList<Item> items;
private Context fontcontext;
private LayoutInflater vi;
public EntryAdapter(Context context, ArrayList<Item> items) {
super(context,0, items);
fontcontext = context;
this.items = items;
vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final Item i = items.get(position);
if (i != null) {
if(i.isSection()){
SectionItem si = (SectionItem)i;
v = vi.inflate(R.layout.list_item_section, null);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
v.setLongClickable(false);
final TextView sectionView = (TextView) v.findViewById(R.id.list_item_section_text);
sectionView.setText(si.getTitle());
Typeface faceT = Typeface.createFromAsset(fontcontext.getAssets(),"fonts/Walkwayrounded.ttf");
sectionView.setTypeface(faceT);
final TextView conditionView = (TextView) v.findViewById(R.id.textViewn);
conditionView.setText(si.getCondition());
Typeface faceC = Typeface.createFromAsset(fontcontext.getAssets(),"fonts/Walkwayrounded.ttf");
conditionView.setTypeface(faceC);
}else{
EntryItem ei = (EntryItem)i;
v = vi.inflate(R.layout.list_item_entry, null);
final TextView title = (TextView)v.findViewById(R.id.list_item_entry_title);
final TextView subtitle = (TextView)v.findViewById(R.id.list_item_entry_summary);
if (title != null)
title.setText(ei.title);
if(subtitle != null)
subtitle.setText(ei.subtitle);
}
}
return v;
}
}
公共类入口适配器扩展了ArrayAdapter{
私有ArrayList项;
私人语境;
私人停车场6号;
公共入口适配器(上下文、ArrayList项){
超级(上下文,0,项);
fontcontext=上下文;
这个项目=项目;
vi=(LayoutInflater)context.getSystemService(context.LAYOUT\u充气机\u服务);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
最终项目i=项目。获取(位置);
如果(i!=null){
if(i.isSection()){
SectionItem si=(SectionItem)i;
v=vi.充气(R.布局.列表项目部分,空);
v、 setOnClickListener(空);
v、 setOnLongClickListener(空);
v、 设置长点击(假);
最终文本视图部分视图=(文本视图)v.findViewById(R.id.list\u item\u section\u text);
sectionView.setText(si.getTitle());
Typeface faceT=Typeface.createFromAsset(fontcontext.getAssets(),“fonts/Foots.ttf”);
截面视图。设置字体(刻面);
最终文本视图条件视图=(文本视图)v.findViewById(R.id.textViewn);
conditionView.setText(si.getCondition());
Typeface faceC=Typeface.createFromAsset(fontcontext.getAssets(),“fonts/Foots.ttf”);
conditionView.setTypeface(faceC);
}否则{
EntryItem ei=(EntryItem)i;
v=vi.充气(R.布局.列表项目输入,空);
最终文本视图标题=(文本视图)v.findViewById(R.id.list\u item\u entry\u title);
最终文本视图字幕=(文本视图)v.findViewById(R.id.list\u item\u entry\u summary);
如果(标题!=null)
title.setText(ei.title);
如果(副标题!=null)
subtitle.setText(ei.subtitle);
}
}
返回v;
}
}
任何解决问题的方法都将受到赞赏 您的主要问题似乎是如何引用项目。无法使用
项。获取(位置)
,因为位置将溢出。尝试将以下方法添加到适配器:
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public Item getItem(int position) {
return items.get(position % items.size());
}
替换
final Item i = items.get(position);
与
看起来您的主要问题是如何引用项目。无法使用
项。获取(位置)
,因为位置将溢出。尝试将以下方法添加到适配器:
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public Item getItem(int position) {
return items.get(position % items.size());
}
替换
final Item i = items.get(position);
与
首先,您应该使用更有效的方法来绘制列表项 Android开发者资源网站提供了一个“API演示”SDK,您可以下载。 对于使用操作系统的不同方式,有大量的示例和配方。一个是专门为处理包含数千个条目或占用内存的图像的列表而创建的,称为
EfficientAdapter
,它扩展了BaseAdapter
类,允许您在getView
回调期间循环视图
而不是膨胀新的xml布局
我在src.com.example.api.view
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.view;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ImageView;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import com.example.android.apis.R;
/**
* Demonstrates how to write an efficient list adapter. The adapter used in this example binds
* to an ImageView and to a TextView for each row in the list.
*
* To work efficiently the adapter implemented here uses two techniques:
* - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary
* - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary
*
* The ViewHolder pattern consists in storing a data structure in the tag of the view returned by
* getView(). This data structures contains references to the views we want to bind data to, thus
* avoiding calls to findViewById() every time getView() is invoked.
*/
public class List14 extends ListActivity {
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Bitmap mIcon1;
private Bitmap mIcon2;
public EfficientAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
// Icons bound to the rows.
mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_1);
mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_2);
}
/**
* The number of items in the list is determined by the number of speeches
* in our array.
*
* @see android.widget.ListAdapter#getCount()
*/
public int getCount() {
return DATA.length;
}
/**
* Since the data comes from an array, just returning the index is
* sufficent to get at the data. If we were using a more complex data
* structure, we would return whatever object represents one row in the
* list.
*
* @see android.widget.ListAdapter#getItem(int)
*/
public Object getItem(int position) {
return position;
}
/**
* Use the array index as a unique id.
*
* @see android.widget.ListAdapter#getItemId(int)
*/
public long getItemId(int position) {
return position;
}
/**
* Make a view to hold each row.
*
* @see android.widget.ListAdapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}
static class ViewHolder {
TextView text;
ImageView icon;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new EfficientAdapter(this));
}
private static final String[] DATA = Cheeses.sCheeseStrings;
}
首先,您应该使用更有效的方法来绘制列表项 Android开发者资源网站提供了一个“API演示”SDK,您可以下载。 对于使用操作系统的不同方式,有大量的示例和配方。一个是专门为处理包含数千个条目或占用内存的图像的列表而创建的,称为
EfficientAdapter
,它扩展了BaseAdapter
类,允许您在getView
回调期间循环视图
而不是膨胀新的xml布局
我在src.com.example.api.view
/*
* Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.view;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.ImageView;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import com.example.android.apis.R;
/**
* Demonstrates how to write an efficient list adapter. The adapter used in this example binds
* to an ImageView and to a TextView for each row in the list.
*
* To work efficiently the adapter implemented here uses two techniques:
* - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary
* - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary
*
* The ViewHolder pattern consists in storing a data structure in the tag of the view returned by
* getView(). This data structures contains references to the views we want to bind data to, thus
* avoiding calls to findViewById() every time getView() is invoked.
*/
public class List14 extends ListActivity {
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Bitmap mIcon1;
private Bitmap mIcon2;
public EfficientAdapter(Context context) {
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
// Icons bound to the rows.
mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_1);
mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon48x48_2);
}
/**
* The number of items in the list is determined by the number of speeches
* in our array.
*
* @see android.widget.ListAdapter#getCount()
*/
public int getCount() {
return DATA.length;
}
/**
* Since the data comes from an array, just returning the index is
* sufficent to get at the data. If we were using a more complex data
* structure, we would return whatever object represents one row in the
* list.
*
* @see android.widget.ListAdapter#getItem(int)
*/
public Object getItem(int position) {
return position;
}
/**
* Use the array index as a unique id.
*
* @see android.widget.ListAdapter#getItemId(int)
*/
public long getItemId(int position) {
return position;
}
/**
* Make a view to hold each row.
*
* @see android.widget.ListAdapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.text.setText(DATA[position]);
holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}
static class ViewHolder {
TextView text;
ImageView icon;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new EfficientAdapter(this));
}
private static final String[] DATA = Cheeses.sCheeseStrings;
}
美丽的!正如您所建议的,此解决方案有效且干净。谢谢美丽的!正如您所建议的,此解决方案有效且干净。谢谢您好,我们如何创建双向循环列表视图,当向上滚动时,它应该显示最后一项。你们能给我一些提示吗?您好,我们如何创建一个双向循环列表视图,当向上滚动时,它应该显示最后一项。你能给我一些提示吗。