Android ListFragment and listfragment

Source: Internet
Author: User

Android ListFragment and listfragment


Android introduced Fragment in Android 3.0 (API level 11) (to be compatible with earlier versions of devices using the support library class ). Fragment can be regarded as a module in the Activity. This module has its own layout and has its own lifecycle (the managed activity calls its periodic method), and processes its input separately, you can load or remove the Fragment module when the Activity is running.


ListFragment is a subclass of Fragment. It supports built-in list display. ListFragment displays the data bound to it through the built-in ListView. The following is an example of the specific usage:


1. model layer Day and DayLab

Use the getDays () method of the DayLab object to obtain the bound data.

Day. java

package com.example.showdays;import java.util.ArrayList;public class Day {private String mTitle;public String getTitle() {return mTitle;}public void setTitle(String mTitle) {this.mTitle = mTitle;}}

DayLab. java

package com.example.showdays;import java.util.ArrayList;public class DayLab {private ArrayList<Day> mDays;public DayLab() {mDays = new ArrayList<Day>();for (int i = 1; i <= 10; i++) {Day day = new Day();day.setTitle("Title #" + i);mDays.add(day);}}public ArrayList<Day> getDays() {return mDays;}}

2. Create a DayListFragment class

Inherits the ListFragment class. And set the Adapter for its built-in ListView

DayListFragment. java

Package com. example. showdays; import java. util. arrayList; import android. OS. bundle; import android. support. v4.app. listFragment; import android. widget. arrayAdapter; public class DayListFragment extends ListFragment {private ArrayList <Day> days; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); DayLab dayLab = new DayLab (); this. days = dayLab. getDays (); ArrayAdapter <Day> adapter = new ArrayAdapter (getActivity (), android. r. layout. simple_list_item_1, days); // The first parameter is the Context object. The resource ID of the second parameter requires the Context object // The second parameter: Resource ID, you can find the ArrayAdapter used to create the View object layout. The real parameter here is the predefined layout resource provided by the Android SDK. // The third parameter is the dataset setListAdapter (adapter ); // set the adapter for the built-in ListView of DayListFragment }}

3. Create a DayActivity class

Inherits the FragmentActivity class. Hosting DayListFragment object

The layout is as follows:

Activity_day.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/fragmentContainer"    android:layout_width="match_parent"    android:layout_height="match_parent" ></FrameLayout>
Reference this layout file in the class and place the DayListFragment object in the FrameLayout container view with the id of fragmentContainer.

DayActivity. java

Package com. example. showdays; import android. support. v4.app. fragment; import android. support. v4.app. fragmentActivity; import android. support. v4.app. fragmentManager; import android. OS. bundle; import android. util. log; import android. view. menu; import android. view. menuItem; public class DayActivity extends FragmentActivity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_day); FragmentManager fm = getsuppfrfragmentmanager (); // inherits the methods that support the Library Class FragmentActivity to obtain the FragmentManager object. // If the inherited Activity uses getFragmentManager () Fragment fragment = fm. findFragmentById (R. id. fragmentContainer); if (fragment = null) {// check whether the fragment transaction queue contains this transaction fragment = new DayListFragment (); fm. beginTransaction (). add (R. id. fragmentContainer, fragment ). commit (); // submit a transaction (resource ID is a unique identifier and the Notification View location )}}}


Now the program runs properly. The running effect is as follows:


As you can see, the list is displayed, but the content to be displayed is not what I want.

This is because the layout of the predefined layout resources (Android. R. layout. simple_list_item_1) provided by the android SDK is as follows:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/text1"    style="?android:attr/listItemFirstLineStyle"    android:paddingTop="2dip"    android:paddingBottom="3dip"    android:layout_width="match_parent"    android:layout_height="wrap_content" ></TextView>

In this way, when the ListView is instantiated and session with the adapter, The ArrayAdapter <Day>. getView () method will call the toString () method of the Day object, and then pass the return value to TextView.

So override the toString () method of the Day object:

Day. java

package com.example.showdays;import java.util.ArrayList;public class Day {private String mTitle;public String getTitle() {return mTitle;}public void setTitle(String mTitle) {this.mTitle = mTitle;}@Overridepublic String toString() {return mTitle;}}
Run the program again. The running effect is as follows:


4. Custom list items

The pre-defined layout resources provided by the Android SDK are used to directly display the data in the TextView component.

Next, I will customize a list item layout. The information to be displayed will be displayed using the Button component. The layout is as follows:

My_list_item.xml

<Button xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/button"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:enabled="false" ></Button>
Because the ArrayAdapter <Day>. getView () method must be called to obtain the View when the ListView updates the View. Therefore, if you want to add a custom list item to ListView, You can override the getView () method and return a custom View. Make the following changes:

DayListFragment. java

Package com. example. showdays; import java. util. arrayList; import android. OS. bundle; import android. support. v4.app. listFragment; import android. view. view; import android. view. viewGroup; import android. widget. arrayAdapter; import android. widget. button; public class DayListFragment extends ListFragment {private ArrayList <Day> days; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); DayLab dayLab = new DayLab (); this. days = dayLab. getDays (); DayAdapter adapter = new DayAdapter (days); setListAdapter (adapter);} private class DayAdapter extends ArrayAdapter <Day> {public DayAdapter (ArrayList <Day> days) {super (getActivity (), 0, days) ;}@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {if (convertView = null) {// whether the list item exists. If it does not exist, create convertView = getActivity (). getLayoutInflater (). inflate (R. layout. my_list_item, null);} Day day = getItem (position); // obtain the Day object of the current position. Button button = (Button) convertView. findViewById (R. id. button); button. setText (day. toString (); return convertView ;}}}
Finally, run the command. The running effect is as follows:



# DONE #






How can I replace android, fragment, and listfragment?

I tried it. No problem. It's all right.
Or you can use FrameLayout to try the layout control that displays Fragment or add a background color. I have also encountered several interfaces that do not overlap with each other. I don't know why, we recommend you paste the code you wrote.

How to Implement long buttons and contextmenu for Android listfragment

Is it true if you set LongClickListener?
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.