滑动/(甩动)动态视图
我想要的:
我想向我的应用程序添加滑动操作或我了解到的在 android fling 上命名的操作。 我有一个动态数量的视图,该数字是我解析的 ICS 文件中的日期量,我想对所有这些视图一起进行滑动效果。 但如果我有 12 个,每个都有一个包含 8 个项目(最多)的 ListView,我猜它会使用大量内存。所以我希望只有当前所选视图之前的 2 个和之后的 2 个被初始化。
我尝试过的:
在我的搜索中,我遇到了这个 stackoverflow 问题其中提到了 HorizontalPager。但我不知道如何让它与许多 ListView 一起工作并动态加载它们。
我尝试了一个ViewGroup,然后添加和删除一个ListView,但它不起作用,它显示的是ViewGroup而不是ListView
public class HourView extends ViewGroup
{
private ListView listView;
/**
* @param context
*/
public HourView(Context context)
{
super(context);
init(false);
}
/**
*
* @param context
* @param day the current day
*/
public HourView(Context context, String day,
boolean shouldBeVisible)
{
super(context);
this.day = day;
init(shouldBeVisible);
}
private void init(boolean shouldBeVisible)
{
if (shouldBeVisible)
{
listView = new ListView(getContext());
if (day == null)
{
day = Event.dtFormatDay.format(new Date());
}
new GetEvents(day).execute();
addView(listView);
}
else
{
removeAllViews();
listView = null;
}
}
}
GetEvents()是一个AsyncTask(viewgroup类中的一个类),它从本地数据库获取一些事件,代码onPostExecute 如下
protected void onPostExecute(String errMsg)
{
if (errMsg != null)
{
listView.setAdapter(new SkemaHoursRowAdapter(getContext(), eventItems));
listView.requestLayout();
addView(listView, layoutParams);
}
}
eventItems 是我解析为自定义 rowadapter 的数组(我知道它有效)。显示视图组,但不显示列表视图。
有什么建议吗?
What i want to:
I want to add a swipe or what i learned it's named on android fling, to my app.
I have a dynamic number of views, the number is the amount of dates from an ICS file which i parse, i want to make a swipe effekt on all of these views together.
But if i have let's say 12 of these each having a ListView with 8 items (max) it would use a lot of memory i guess. So i would like that only the 2 before current selected view and the 2 after to be initialized.
What i have tried:
In my search i stumpled around this stackoverflow question which mentions HorizontalPager. But i dont know to make it work with a number of ListView's and load them dynamically.
I tried a ViewGroup and then add and remove a ListView but it's not working, it display's the ViewGroup but not the ListView
public class HourView extends ViewGroup
{
private ListView listView;
/**
* @param context
*/
public HourView(Context context)
{
super(context);
init(false);
}
/**
*
* @param context
* @param day the current day
*/
public HourView(Context context, String day,
boolean shouldBeVisible)
{
super(context);
this.day = day;
init(shouldBeVisible);
}
private void init(boolean shouldBeVisible)
{
if (shouldBeVisible)
{
listView = new ListView(getContext());
if (day == null)
{
day = Event.dtFormatDay.format(new Date());
}
new GetEvents(day).execute();
addView(listView);
}
else
{
removeAllViews();
listView = null;
}
}
}
The GetEvents() is a AsyncTask (a class inside the viewgroup class) that gets some events from a local database, the code is the onPostExecute is as follows
protected void onPostExecute(String errMsg)
{
if (errMsg != null)
{
listView.setAdapter(new SkemaHoursRowAdapter(getContext(), eventItems));
listView.requestLayout();
addView(listView, layoutParams);
}
}
eventItems is an array i parse to my custom rowadapter (which i know works). The view group is displayed but the listView is not.
Any suggestions??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终没有使用视图组,而是制作了一个加载器列表适配器,如果正确的列表尚未更新,我会显示该适配器。
因此,它不是扩展
ViewGroup
,而是扩展ListView
,它的适配器首先设置为加载适配器,加载时将其设置为正确的列表视图。I ended up not using a viewgroup instead i made a loader-listadapter which i display if the correct list has not been updated yet.
So instead of extending
ViewGroup
it extendsListView
and it's adapter get set at first to the load-adapter and when loaded it sets it to the correct listview.