关于这个主题的文献很少,而google的文档没有考虑使用ListFragment扩展来定制片段列表的可能性(listviewanimation)。因此,我会问这个问题,然后尽可能地回答它,因为我也想要50个声望点,所以我最后可以通过评论感谢这个网站上的优秀解释者。如何创建一个没有ListFragment列表的片段
对于此评论的目的,我会从listviewanimation LIB在股价组件:
https://github.com/nhaarman/ListViewAnimations
答:
我们需要设置4个部件有一个适当的片段与列表视图组件
- 活动通过活动的片段管理器创建片段。
- Fragment类将是非常基本的片段东西,它将具有listview,并且它将链接该列表视图与arrayadapter。
- 适用于我们的目的只能处理字符串的Adapter类。
- 在ADAPTER CLASS中,最后的第四个组件将是一个viewholder类,它将允许更快地创建列表中的行,因为每行的单个组件将被包装在允许更快速的对象实例化的类中。
好的,首先将是活动的代码,这个代码可以通过点击按钮或其他事件来调用。当事件发生时,片段管理器将被创建,并且片段管理器将创建一个事务,这是一种奇特的说法,管理器将在活动和新形成的片段之间进行通信,以便正确设置所有内容。
这里就是事件发生时,应放置在你的活动代码:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
GenericFragment fragment = new GenericFragment();
fragmentTransaction.add(R.id.pager, fragment);
//Replace R.id.pager with the view that you want your fragment to go in.
fragmentTransaction.commit();
这就是它!不是很糟糕,是吗?现在让我们继续讨论GenericFragment类,您可以创建一个不同的名称。我不会发布这一切的代码,但我会通过你需要有一个ListView片段课堂上的一切步骤:
- 有你的片段类扩展片段
- 有一个空的构造这个类(谷歌需要它... -__-)
创建一个newInstance方法,该方法将处理从活动创建片段的“新实例”时将数据从活动传递到片段的传递:
我会帮你用这个:
public static GenericFragment newInstance(String StuffYouWantGetsPassedFromActivityToFragment) {
GenericFragment GenericFragment = new GenericFragment();
Bundle args = new Bundle();
GenericFragment.setArguments(args);
return GenericFragment;
}
再次没那么糟糕吧?我们还没有完成,我们仍然需要重写onCreateView和onCreate,然后我们将完成这个简单的步骤!
确定为onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.generic_fragment_layout, container, false);
addGoalButton = (Button) view.findViewById(R.id.btn_newRow); //Created for testing purposes
lv = (ListView) view.findViewById(R.id.GenericListView);
addGoalButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { //Created for testing purposes
genericAdapter.add("Goal");
genericAdapter.notifyDataSetChanged();
}
});
lv.setAdapter(genericAdapter);
return view;
}
,上面的代码可能看起来像一个怪物,你说得对!高层次的概述是,你得到了你想要的片段的布局文件。在这个布局文件中,你得到了listview并创建了一个变量来保存它。然后你调用listView的'setAdapter'方法来添加下一步,即适配器类。出于测试目的,我添加了该按钮,以便您可以在脑海中扩展本教程l8er。 (删除所有按钮代码,如果你只想列表)
好吧,片段类的最后一步:覆盖OnCreate!
OnCreate方法是你想要实例化所有你的私有变量,比如genericAdapter变量或者你想要在Fragment类的多个部分使用的任何东西。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> exampleItemList = new ArrayList<String>();
exampleItemList.add("item1");
exampleItemList.add("item2");
exampleItemList.add("item3");
exampleItemList.add("item4");
exampleItemList.add("item5");
exampleItemList.add("item6");
exampleItemList.add("item7");
exampleItemList.add("item8");
exampleItemList.add("item9");
exampleItemList.add("item10");
exampleItemList.add("item11");
exampleItemList.add("item12");
genericAdapter = new genericAdapter(getActivity(), 0, exampleItemList);
setHasOptionsMenu(true); // Allows the fragment to change the menu buttons
}
我将示例项添加到arrayList中,以使本教程对于数据来自哪里以及去哪里的位置更加透明。
就是这样!你的片段完成了!现在已经快结束了,我保证。
让我们敲最后两个步骤一起出去,建立一个扩展ArrayAdapter和私人内部ViewHolder类来包装中的所有布局组件GenericAdapter类:
public class GenericAdapter extends ArrayAdapter<String>
LayoutInflater layoutInflater;
//Used to get the correct LayoutInflater to inflate each row item
public GenericAdapter(Context context, int resource, List<String> objects) {
super(context, 0, objects);
layoutInflater = layoutInflater.from(context);
}
/**
* @param position The position in the list to get the data for that row item.
* @param convertView The view for the row item that will be shown in the list.
* @param parent Having this object allows you to use the LayoutInflater for the parent.
* @return
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final GenericViewHolder GenericViewHolder;
final String item = getItem(position);
if(convertView == null){
LinearLayout rootView = (LinearLayout) layoutInflater.inflate(R.layout.item_row, parent, false);
genericViewHolder = genericViewHolder.create(rootView);
rootView.setTag(genericViewHolder);
}
else{
genericViewHolder = (genericViewHolder) convertView.getTag();
}
genericViewHolder.textView.setText(item);
return genericViewHolder.rootView;
}
/**
* ViewHolder's allow for a single object to maintain a Goal row item, so that the row item
* doesn't have to create each individual component (textview layout etc.) each time the
* row object is created/recreated. Allows for fast scrolling with little latency.
*/
private static class GenericViewHolder {
public final LinearLayout rootView;
public final GripView gripView;
public final TextView textView;
private GoalViewHolder(LinearLayout rootView, GripView gripView, TextView textView) {
this.rootView = rootView;
this.gripView = gripView;
this.textView = textView;
}
public static GoalViewHolder create(LinearLayout rootView){
TextView textView = (TextView)rootView.findViewById(R.id.list_row_draganddrop_textview);
GripView gripView = (GripView)rootView.findViewById(R.id.list_row_draganddrop_touchview);
return new GenericViewHolder(rootView, gripView, textView);
}
}
}
这是再次,一个怪物,我们来看一下高级概述,我们创建了一个适配器类和一个适配器类使用的viewholder类。在适配器的构造函数中,我们得到了一个layoutinflater来帮助扩充每一行的项目。然后,我们创建了getView方法,该方法在您的应用程序中调用了数千次,因为它可以在用户可查看每行时显示。 getView方法可以查看要转换为行的视图是否为null。如果是,它会创建一个新的数据条目(一个viewholder),但是如果它不是null,那么这个viewholder已经被创建了,所以我们得到了viewholder里面的所有东西,这样我们就不必创建一个新的行项目。
phew!我不指望你明白这一点,但如果你这样做,恭喜你。
好吧,就这样吧。你应该被设置,并且当你的活动的事件被调用时,片段将显示在包含该片段的任何视图中。我会在我的答案中发布我的XML文件,以便我可以得到那些美味的upvotes(或不,我可能完全不正确,但是这对我有用!)
享受生活,不要放弃!
那么,这是什么,一个教程? – 2014-11-06 16:02:00
是的,非常。我找不到合适的资源来制作一个片段中的列表视图,因为谷歌用一些名为ListFragment的类淹没了教程小众,它不容易使用像listviewanimation这样的库进行定制: https://github.com/nhaarman/ListViewAnimations 另外,我在一个非常无聊的班级,所以我想我会记录我的方法。 另外,我真的想要那50个代表点,这样我就可以评论这篇文章:http://stackoverflow.com/a/18319050/2977650 感谢他们在做得好的工作 – slackbot39243 2014-11-06 16:04:02
在片段中的ListView或在Activity中是我最喜欢的提供ListViews的方式,因为多年以来。这是一件非常容易的事情:只需将一个ListView放置在xml布局中,然后使用该布局作为Activity或Fragment的contententView。 – 2014-11-06 16:08:13