简体   繁体   中英

Android Fabric Twitter Timeline

I am trying to show a user timeline in an android fragment. My HomeFragment.java onCreate() looks like this:

public class HomeFragment extends ListFragment {

@InjectView(android.R.id.list) ListView mTimeline;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mSectionNumber = getArguments().getInt(ARG_SECTION_NUMBER);
    }

    final UserTimeline userTimeline = new UserTimeline.Builder().screenName("fabric").build();
    final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter(getActivity(), userTimeline);
    setListAdapter(adapter);

}

And my fragment_home.xml has this listview:

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@android:id/list"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"/>

HomeFragment is a fragment of HomeActivity. This code just shows an empty list. How do I fix this problem? I would also like to know how setListAdapter(adapter); works. How does it know which list to set the adapter to since it can't be called on any particular list. I used this twitter fabric page for reference when writing this code.

Inflate your custom layout fragment_home.xml in the ListFragment by overriding onCreateView .

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_home, container, false);
}

Your layout contains a ListView with the special @android:id/list id which identifies the ListView ListFragment's setListAdapter will use.

Also, TweetTimelineListAdapter is just a ListAdapter . If you'd like to find your own ListView and set the adapter yourself, use an ordinary Fragment (not a ListFragment ).

mTimeline.setListAdapter(adapter)  // mTimeline is a ListView

The docs include a ListFragment example .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM