1

关于这个主题的文献很少,而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(或不,我可能完全不正确,但是这对我有用!)

享受生活,不要放弃!

+2

那么,这是什么,一个教程? – 2014-11-06 16:02:00

+0

是的,非常。我找不到合适的资源来制作一个片段中的列表视图,因为谷歌用一些名为ListFragment的类淹没了教程小众,它不容易使用像listviewanimation这样的库进行定制: https://github.com/nhaarman/ListViewAnimations 另外,我在一个非常无聊的班级,所以我想我会记录我的方法。 另外,我真的想要那50个代表点,这样我就可以评论这篇文章:http://stackoverflow.com/a/18319050/2977650 感谢他们在做得好的工作 – slackbot39243 2014-11-06 16:04:02

+1

在片段中的ListView或在Activity中是我最喜欢的提供ListViews的方式,因为多年以来。这是一件非常容易的事情:只需将一个ListView放置在xml布局中,然后使用该布局作为Activity或Fragment的contententView。 – 2014-11-06 16:08:13

回答

0

活动XML,大部分是无关的各位读者,但碎片的容器的观点是寻呼机:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity"> 

<!--Navigation Drawer Still Under Construction--> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <!-- As the main content view, the view below consumes the entire 
     space available using match_parent in both dimensions. --> 
    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <!-- android:layout_gravity="start" tells DrawerLayout to treat 
     this as a sliding drawer on the left side for left-to-right 
     languages and on the right side for right-to-left languages. 
     The drawer is given a fixed width in dp and extends the full height of 
     the container. A solid background is used for contrast 
     with the content view. --> 

    <ListView 
     android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="#ffff"/> 
</android.support.v4.widget.DrawerLayout> 
<!--Navigation Drawer Still Under Construction--> 


<!--Customizations on README at: https://github.com/astuetz/PagerSlidingTabStrip--> 
<com.astuetz.PagerSlidingTabStrip 
    android:id="@+id/tabs" 
    android:layout_width="wrap_content" 
    android:layout_height="48dip" 
    app:pstsShouldExpand="true" 
    app:pstsIndicatorHeight="5dip" 
    app:pstsDividerPadding="0dip" 
    app:pstsDividerColor="#ff6d00" 
    app:pstsUnderlineColor="#ff5722" 
    app:pstsIndicatorColor="#ff5722"/> 

<!--To scale the viewpager vertically, android:layout_above="@+id/[viewname]" --> 
<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/tabs" 
    tools:context=".MainActivity" /> 

</RelativeLayout> 

的XML布局为片段:

<?xml version="1.0" encoding="utf-8"?> 



<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="New Item" 
    android:id="@+id/btn_newItem" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 

<com.nhaarman.listviewanimations.itemmanipulation.DynamicListView 
    android:id="@+id/GenericListView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_above="@+id/btn_newGoal" /> 

的特定行项目:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    tools:ignore="UseCompoundDrawables"> 

<com.nhaarman.listviewanimations.itemmanipulation.dragdrop.GripView 
    android:id="@+id/list_row_draganddrop_touchview" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:color="@android:color/darker_gray" 
    android:paddingBottom="4dp" 
    android:paddingLeft="8dp" 
    android:paddingRight="8dp" 
    android:paddingTop="4dp" /> 

<TextView 
    android:id="@+id/list_row_draganddrop_textview" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:fontFamily="sans-serif-light" 
    android:gravity="center_vertical" 
    android:minHeight="48dp" 
    android:textColor="?android:attr/textColorSecondary" 
    android:textSize="20sp" 
    tools:ignore="UnusedAttribute" /> 
</LinearLayout> 

的第二代码段的布局部分得到切断,因此未AGR用我的ctrl K'ing进行操作,但是它的长短不一,是因为listview在那里,所以无论你把它放在线性布局还是相对布局都没关系。

祝你好运兄弟的快乐编码

相关问题