Android ListView and fragment, similar to the implementation of a certain e-commerce, can be used to get, detailed labeling for the novice

Source: Internet
Author: User

Reprint please indicate the source of Wang 亟亟 's Daniel Road

An implementation similar to that of an e-commerce allows novices to understand how the parameters between activity and fragment interact.

Package Structure:

Post-run effects


Analysis:
The left ListView can be dragged up and down, and clicking on a different item will affect the content on the right fragment.

Nonsense not much to say, on the code (detailed callout)
Mainactivity

 Public  class mainactivity extends fragmentactivity  implements  Onitemclicklistener {            /*listview Fill with * /    PrivateString[] STRs = {"Common Classification","Apparel underwear","Pet","mobile","Home Appliances","Digital","Computer Office","A care and make -up","book","Shoe Boots"};PrivateListView ListView;PrivateMyadapter adapter;PrivateContentfragment myfragment;/* Number of digits of the selected item * /     Public Static intMposition;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);/ * Initialize * /Initview (); }/** * Initialize view * /    Private void Initview() {//Matching controlsListView = (ListView) Findviewbyid (R.id.listview);//Pass the referenceadapter =NewMyadapter ( This, STRs); Listview.setadapter (adapter);//Monitoring EventsListview.setonitemclicklistener ( This);//Create Myfragment ObjectMyfragment =NewContentfragment ();        Fragmenttransaction fragmenttransaction = Getsupportfragmentmanager (). BeginTransaction (); Fragmenttransaction.replace (R.id.fragment_container, myfragment);//Pass value to Myfragment by bundleBundle bundle =NewBundle ();        Bundle.putstring (Contentfragment.tag, strs[mposition]);        Myfragment.setarguments (bundle);    Fragmenttransaction.commit (); }@Override     Public void Onitemclick(adapterview<?> parent, view view,intPositionLongID) {//Get current positionMposition = position;//Even refresh adapterAdapter.notifydatasetchanged (); for(inti =0; i < strs.length; i++) {myfragment =NewContentfragment ();            Fragmenttransaction fragmenttransaction = Getsupportfragmentmanager (). BeginTransaction ();            Fragmenttransaction.replace (R.id.fragment_container, myfragment); Bundle bundle =NewBundle ();            Bundle.putstring (Contentfragment.tag, strs[position]);            Myfragment.setarguments (bundle);        Fragmenttransaction.commit (); }    }}

Contentfragment

 Public  class contentfragment extends Fragment {     Public Static FinalString TAG ="Myfragment";PrivateString str;@Override     PublicViewOncreateview(Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {//TODO auto-generated method stubView view = Inflater.inflate (R.layout.myfragment,NULL); TextView Tv_title = (TextView) View.findviewbyid (r.id.tv_title);//Get Datastr = getarguments (). getString (TAG); Tv_title.settext (str);returnView }}

The adapter for the ListView (simple optimizations have been made)

 Public  class myadapter extends baseadapter {    PrivateContext context;PrivateString[] strings; Public Static intMposition; Public Myadapter(context context, string[] strings) { This. Context =context; This. strings = Strings; }@Override     Public int GetCount() {//TODO auto-generated method stub        returnStrings.length; }@Override     PublicObjectGetItem(intPosition) {//TODO auto-generated method stub        returnStrings[position]; }@Override     Public Long Getitemid(intPosition) {//TODO auto-generated method stub        returnPosition }@Override     PublicViewGetView(intPosition, View Convertview, ViewGroup parent) {Viewholder viewholder=NewViewholder ();if(convertview==NULL{Convertview = Layoutinflater.from (context). Inflate (R.layout.listview_item,NULL);             viewholder.tv= (TextView) Convertview.findviewbyid (r.id.tv);             Mposition = position; ViewHolder.tv.setText (Strings[position]);if(Position = = mainactivity.mposition)                {Convertview.setbackgroundresource (R.DRAWABLE.TONGCHENG_ALL_BG01); }Else{Convertview.setbackgroundcolor (Color.parsecolor ("#f4f4f4"));          } convertview.settag (Viewholder); }Else{Viewholder = (Viewholder) convertview.gettag (); } viewHolder.tv.setText (Strings[position]);if(Position = = mainactivity.mposition)            {Convertview.setbackgroundresource (R.DRAWABLE.TONGCHENG_ALL_BG01); }Else{Convertview.setbackgroundcolor (Color.parsecolor ("#f4f4f4")); }returnConvertview; }Private Static  class viewholder {TextView TV; }}

Main layout file:

<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"  Android:layout_width="Match_parent"android:layout_height="Match_parent" android:orientation="vertical" >                <view  android:layout_ Width  = "match_parent"  android:layout_height< /span>= "1DP"  android:background  = "#cdcdcd" />     <textview  android:layout _width  = "match_parent"  android:layout_height  = "40DP"  android:background  = "#ededed"  android:gravity  =         "center"  android:text  = "all kinds"  android:textcolor  =" #BF3EFF " android:textsize  = "17SP" />     <linearlayout  android: Layout_width  = "match_parent"  android:layout_ Height  = "match_parent"  android:background
      = "#fbfbfb"  android:orientation  = "horizontal" ;         <listview  android:id= "@+id/listview"  android:layout_width  = "0DP"  android:layout_height  =" match_parent " android:scrollbars  ="            None " android:layout_weight  =" 1.0 " android:background  = "#f4f4f4" />         <framelayoutandroid:id="@+id/fragment_container"android:layout_width ="0DP"android:layout_height="Match_parent"android:layout_weight=" 3.0 " />                                                    </linearlayout></linearlayout>
Because, belongs to the novice to understand the first 2 things. So there is not too much explanation, the comments are very thin (self-think), also suitable for lazy ghosts to use.

Source Address: Http://yunpan.cn/cdkX7pQSCamJM access password 82C9

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android ListView and fragment, similar to the implementation of a certain e-commerce, can be used to get, detailed labeling for the novice

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.