一个ListView可以包含片段片段、ListView

2023-09-09 21:07:48 作者:温暖的阳光照射不到我的心

作为,可以ListView的元素碎片。我知道,你可以一个TextView XML分配到ListView来改变它的样子,但你能添加碎片进入一个ListView。

As in, can the ELEMENTS of a ListView be Fragments. I know that you can assign a TextView XML to a ListView to change the way it looks, but can you add Fragments into a ListView.

例如:我有一个片段。对于所述片段XML包含一个ImageView的,几个大的风格TextViews,和一个小式的TextView。该片段类code接收到一个包,然后根据内容填充据此TextViews和ImageView的。无论是片段的XML和没有问题的片段code工作(我可以显示一个单独的片段就好了)。我有我想要显示的上述片段的列表FragmentActivity。这里是code我用尽量填充FragmentActivity的看法里面的ListView:

For instance: I have a Fragment. The XML for said Fragment contains an ImageView, a couple of large-style TextViews, and a small-style TextView. The Fragment class code receives a Bundle, then based on the contents populates the TextViews and ImageView accordingly. Both the Fragment XML and the Fragment code work without issue (I can display an individual Fragment just fine). I have a FragmentActivity in which I want to display the aforementioned list of Fragments. Here is the code I'm using to try to populate the ListView inside of the FragmentActivity's View:

    ArrayList<Fragment> fragList = new ArrayList<Fragment>();
    Fragment fragment = Fragment.instantiate(this, TileItem.class.getName());
    Bundle bundle = new Bundle();
    bundle.putInt("key", 0);
    fragment.setArguments(bundle);
    fragList.add(fragment);

    ArrayAdapter<Fragment> adapter = new ArrayAdapter<Fragment>(this, R.layout.tile_item, fragList);
    listItems.setAdapter(adapter);

下面是我在思考这个模式。我做片段的ArrayList来容纳所有我的意见实例化的。然后创建一个片段,创建一个包,将数据添加到包(这样的片段可以正确地封送数据到它的意见),加上捆绑的片段,然后最后片段添加到ArrayList。在那之后,我做出ArrayAdapter,添加的元素的布局我想用了,我做了片段列表;然后设置ListView控件从我的转接器读取。

Here's my mode of thinking on this. I make an ArrayList of Fragments to hold all of my instantiated Views. I then create a Fragment, create a Bundle, add data to the Bundle (so that the Fragment can marshal data into it's Views correctly), add the Bundle to the Fragment, then finally add the Fragment to the ArrayList. After that, I make an ArrayAdapter, add the element layout I want to use, and the list of Fragments I've made; then set the ListView to read from my adapter.

任何运行该code可能会得到的NPE @实例化一个ArrayAdapter。怎么办?这甚至可能?之前,我一直在货架我的大脑这可以有人告诉我,如果我只是浪费我的时间?有没有更好的办法?我一直在想用滚动型的,但这么多的ListView的功能将需要重新实现,我讨厌,讨厌,讨厌重复发明轮子时,它不是必需的。

Anyone running this code will likely get the NPE @ instantiating the ArrayAdapter. What gives? Is this even possible? Before I keep racking my brain on this can someone tell me if I'm just wasting my time? Is there a better way? I've been thinking of using a ScrollView, but so much of the functionality of a ListView would need to re-implemented and I hate-hate-hate reinventing the wheel when it's not necessary.

由于任何人阅读,特别感谢您的想法,如果你决定离开他们。我试图寻找周围建立回答这个,但我似乎找到正在使用片段的一个ListView INSIDE有关问题/网页;不使用片段作为一个ListView元素

Thanks to anyone reading, and especially thank you for your thoughts if you decide to leave them. I've tried searching around for an established answer to this but all I seem to find are questions/web pages concerning using a ListView INSIDE of a Fragment; not using Fragments AS THE ELEMENTS of a ListView

编辑:我把下面的建议,并开始调查了。从东西出现,我应该能够使用膨胀片段,而不是从XML只是平了建设一个自定义适配器的方式(因为缺乏一个更好的方式来描述这个过程),但我目前的实现正试图设置时抛出NPE该适配器。

I took the suggestions below and started investigating more. From the way things appear I should be able to use a custom adapter that inflates fragments instead of just flat out building from XML (for lack of a better way to describe the process) However, my current implementation is throwing an NPE when trying to set the adapter.

下面是我的自定义适配器code(简称为简洁起见):

Here is my custom adapter code (shortened for brevity):

public class AdapterItem extends ArrayAdapter<Fragment> {

Context c;
List<Fragment> f;

public AdapterItem(Context c, List<Fragment> f) {
    super(c, R.layout.tile_item, f);
    this.c = c;
    this.f = f;
}

@Override
public View getView(int pos, View v, ViewGroup vg) {
    LayoutInflater i = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    return i.inflate(R.layout.tile_item, vg, false);
}

}

和这里是我如何实现它:

and here is how I'm implementing it:

ArrayList<Fragment> fragList = new ArrayList<Fragment>();
    Fragment fragment = Fragment.instantiate(this, TileItem.class.getName());
    Bundle bundle = new Bundle();
    bundle.putInt("key", 0);
    fragment.setArguments(bundle);
    fragList.add(fragment);

    AdapterItem adapter = new AdapterItem(this, fragList);
    adapter.add(fragment);
    listItems.setAdapter(adapter);

所以它是一个几天,我pretty确保该线程已经下葬。不过,我以为我想补充最后一个更新,以防万一有人想尝试这一点,谷歌搜索给他们带来这里。所以在我实现时在ListView给出适配器我得到的NPE。它并不需要一个火箭外科医生弄清楚,它肯定适配器,而不是在ListView引发错误。对于我的生活中,我想不通为什么,虽然...

So it's been a few days and I'm pretty sure this thread has been buried. However, I thought I would add one last update just in case someone wants to try this and a google search brings them here. So in my implementation I'm getting an NPE when the ListView is given the adapter. It doesn't take a rocket surgeon to figure out that it's certainly the adapter and not the ListView throwing the error. For the life of me I can't figure out why though...

在任何情况下,我觉得我有一些想法,但。首先,一个小回的故事:前阵子我试图做一个FragmentDialog内FragmentTransactions。每次我试图这样做,我会得到一个NPE。最终,经过大量的研究,我发现,之所以涉及到的片段实例化的方式。当一个片段被调用,它需要从它的父的背景。由于Dialog的父母是启动它的活动,对话本身不符合标准的必要。我相信,试图片段添加到ListView的时候,这也是如此。因为ListView控件不实例化它抛出了NPE,因而一个片段满足协议,留下我挂和重返约定。 Ð@百万......我真希望我能做到这一点。利用碎片,而不是简单的XML会作出这么轻松地组织/通过搜索列表。哦...猜它不能在任何人的情况下完成的疑惑。

At any rate, I think I have some idea though. First, a little back story: A while back I was trying to make FragmentTransactions inside of a FragmentDialog. Everytime I attempted to do so, I would get an NPE. Eventually, through much research, I discovered that the reason pertained to the way that Fragments are instanced. When a Fragment is called it needs the context from it's parent. Since a Dialog's parent is the Activity that started it, the Dialog itself didn't meet the criteria necessary. I believe, that when attempting to add fragments to a ListView, this is also the case. Since the ListView doesn't meet the agreement with instancing a Fragment it throws the NPE and thus, leaves me hanging and going back to conventions. D@mn...I had really hoped I would be able to do this. Using Fragments instead of simple XML would have made it so much easier to organize/search through the list. Oh well... guess it can't be done in case anyone is wondering.

推荐答案

我说这是不可能做到像把一个片段在ListView将意味着片段可以跨多个容器成倍增加。当您使用FragmentManager创建一个片段,它被打上一个标识符,从而可以轻松刷新和重新排列的方向和其他配置更改。它还鼓励使用跨多个设备的configs。

I'd say this is not possible to do as putting a fragment in a ListView would mean the fragment can be multiplied across multiple containers. When you use the FragmentManager to create a fragment, it is tagged with an identifier, making it simple to reload and rearrange on orientation and other configuration changes. It also encourages uses across multiple device configs.

一个片段是一个真正的活动的一个子集。你会不会有一个活动作为一个列表的一部分?绝对不(应该是答案!)!

A Fragment is really a subset of an Activity. Would you ever have an Activity as part of a list? Definitely not (should be the answer!)!!!

此外,它也不是很有益的连续连接()和分离()的片段,因为它们在移动和选自图(细胞得到回收)。这些都是昂贵的操作是一个ListView不应与处理。清单应快速滚动。

Moreover, it is not very useful to attach() and detach() a fragment continuously as they move in and out of view (cells get recycled). These are all expensive operations that a ListView shouldn't deal with. Lists should scroll quickly.

从上的评论谈话中,我可以看到你想要达到好的code。与视图设置code和适配器在活动良好的分离。与这样做可以:

From the conversation on the comments, I can see you want to achieve nice code with a good separation of view setup code and adapter in the Activity. Do so with either:

重写查看类,做你的自定义绘制和安装在那里。创建一个新的类,在其中您提供所需的上下文和数据集为它你回来列表需要显示视图 - 这是我平时做。有一个utils的类来建立你的视频在别处(傻)。 Override the View class and do your custom drawing and setup there. Create a new class, in which you supply a context and data set required for it to get you back the view a list needs to show - this is what I usually do. Have a Utils class to build your video elsewhere (silly).

只是不要在列表中使用的碎片。不是他们的目标用例。 HTH。

Just don't use Fragments in Lists. Not the use case they are aiming for. HTH.