Android Development notes (13) -- ListFragment, androidlistfragment

Source: Internet
Author: User

Android Development notes (13) -- ListFragment, androidlistfragment

Reprinted Please note: http://www.cnblogs.com/igoslly/p/6959108.html

ListFragment

ListFragment is a class inherited from Fragment. It is used to include the layout File Settings of ListView.

If you do not want to understand ListFragmentSetAdapter for normal FragmentSettings are also possible, for general ListView settings, see the previous chapter: http://www.cnblogs.com/igoslly/p/6947225.html

 

ListFragment configuration usually involves three Layout files:

1,Main Activity Layout: activity_main.xml that contains Fragment (fragment can be directly added statically or framelayout can be set to be dynamically added)

2,Layout: history_list.xml of ListFragment

ListFragment LayoutBy default, a listVew is included., Named "@ id/android: id" (different from the general naming syntax)

You can alsoSet TextView for display without data, Named "@ id/android: empty"

3,Layout: history_list_competition.xml

 


The following examples are used to dynamically add fragment and customize the BaseAdapter method.

-- The setting of ArrayAdapter & SimpleAdapter is simpler. See the previous chapter.

-- Static Method to add fragment, that is, the difference between a function findViewById and findViewByTag, you can also see the column of Su Bai: http://blog.csdn.net/kakaxi1o1/article/details/29368645

 

Activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <LinearLayout            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1"            android:id="@+id/history_list"            android:orientation="vertical">    </LinearLayout></LinearLayout>

 

HistoryFragment. java

In onCreateView (), call history_list.xml as the layout file of the ListFragment.

fragmentTranscation.replace(R.id.history_list, historyListFragment).commit();

Add historyListFragment dynamically and replace the original fragment

public class HistoryFragment extends Fragment {    private FragmentManager fragmentManager;    public HistoryFragment() {    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {        View rootView = inflater.inflate(R.layout.history_list, container, false);        return rootView;    }    @Override    public void onActivityCreated(Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        Button competition_selected = (Button) getActivity().findViewById(R.id.history_competition);        competition_selected.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                fragmentManager = getFragmentManager();                fragmentTranscation = fragmentManager.beginTransaction();                HistoryListFragment historyListFragment = new HistoryListFragment();                fragmentTranscation.replace(R.id.history_list, historyListFragment).commit();            }}        );    }}

 

History_list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:id="@+id/list_content">    <ListView        android:id="@id/android:list"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:padding="8dp"/>    <TextView android:id="@id/android:empty"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text=""/></LinearLayout>

 

History_list_competition.xml

Sets the layout format of each item in ListView.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal">    <LinearLayout        android:layout_width="0dp"        android:layout_weight="8"        android:layout_height="match_parent"        android:orientation="vertical">        <TextView            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1"            android:text="Null"            android:textSize="20sp"            android:padding="2dp"            android:textColor="@color/black"            android:id="@+id/list_competition_player"/>        <TextView            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1"            android:text="Null"            android:textSize="16sp"            android:padding="2dp"            android:id="@+id/list_competition_date"/>    </LinearLayout>    <LinearLayout        android:layout_width="0dp"        android:layout_weight="3"        android:layout_height="match_parent">        <TextView            android:layout_width="match_parent"            android:layout_height="match_parent"            android:text="100"            android:gravity="center"            android:textSize="24sp"            android:textColor="@color/black"            android:id="@+id/list_competition_scoreA"/>    </LinearLayout>    <LinearLayout        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="match_parent">        <TextView            android:layout_width="match_parent"            android:layout_height="match_parent"            android:text="—"            android:gravity="center"            android:textSize="28sp"/>    </LinearLayout>    <LinearLayout        android:layout_width="0dp"        android:layout_weight="3"        android:layout_height="match_parent">        <TextView            android:layout_width="match_parent"            android:layout_height="match_parent"            android:text="100"            android:gravity="center"            android:textSize="24sp"            android:textColor="@color/black"            android:id="@+id/list_competition_scoreB"/>    </LinearLayout></LinearLayout>

 

 

HistoryListFragment. java

In onCreate (), set R. layout. history_list_competition through setListAdapter. Or use the default R. layout. simple_list_item_1;

AddClick Event;Custom BaseAdapter;

Note! To use this Java code, add a List <Map <String, Object> value separately. Otherwise, an error is returned.

Public class HistoryListFragment extends ListFragment {private CompetitionListAdapter adapter; private List <Map <String, Object> competitionlist; // constructor public HistoryListFragment () {}@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); competitionlist = new ArrayList <Map <String, Object> (); adapter = new CompetitionListAdapter (getActivity (); // when binding an adapter, you must use ListFragment. setListAdapter () interface instead of ListView. setAdapter () or other methods this. setListAdapter (adapter);} // create a window @ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {return inflater. inflate (R. layout. history_list, container, false);} // sets the Click Event @ Override public void onListItemClick (ListView l, View v, int position, long id) {super. onListItemClick (l, v, position, id); HashMap <String, Object> item = (HashMap <String, Object>) adapter. getItem (position); String scoreA = (String) item. get ("scoreA"); String scoreB = (String) item. get ("scoreB"); String log = (String) item. get ("log") ;}// custom CompetitionListAdapter inherits from BaseAdapter public class CompetitionListAdapter extends BaseAdapter {private LayoutInflater mInflater = null; public CompetitionListAdapter (Context context) {this. mInflater = LayoutInflater. from (context) ;}@ Override public int getCount () {return competitionlist. size () ;}@ Override public Object getItem (int position) {return competitionlist. get (position) ;}@ Override public long getItemId (int position) {return position ;}@ Override public View getView (int position, View convertView, ViewGroup parent) {ViewHolder holder = null; if (convertView = null) {holder = new ViewHolder (); convertView = mInflater. inflate (R. layout. history_list_competition, null); holder. date = (TextView) convertView. findViewById (R. id. list_competition_date); holder. scoreA = (TextView) convertView. findViewById (R. id. list_competition_scoreA); holder. scoreB = (TextView) convertView. findViewById (R. id. list_competition_scoreB); holder. player = (TextView) convertView. findViewById (R. id. list_competition_player); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag ();} holder. date. setText (String) competitionlist. get (position ). get ("date"); holder. scoreA. setText (String) competitionlist. get (position ). get ("scoreA"); holder. scoreB. setText (String) competitionlist. get (position ). get ("scoreB"); holder. player. setText (String) competitionlist. get (position ). get ("player"); return convertView;} private class ViewHolder {public TextView date; public TextView player; public TextView scoreB; public TextView scoreA ;}}}

 

Overall:

 

 

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.