Fragment is used in the dark 3 Ladder rankings.

Source: Internet
Author: User

Fragment is used in the dark 3 Ladder rankings.
Fragment's use cases in Diablo 3 ladder

The Android programming authority Guide adopted a radical attitude towards Fragment, that is, "The fragment principle is always used ". Android Programming authority guide (English name: Android Programming the big nerd ranch guide). Many technical articles are based on this book, I blindly followed this radical idea. I developed my App "dark three-day ladder list".

The fact is that this radical view has brought me great benefits.

Overview

I used fragment in the following places

  • Progress bar (pop-up screen ?).
  • Select page for data to be returned.
  • Data list page.

Specifically, each of my interfaces is fragment. The most intuitive advantage of fragment is its excellent adaptability to the UI. It can be said that many problems are blocked in advance. Arguments is used to transmit data. The following code is everywhere in the App:

ProgressbarFragment newFragment = new ProgressbarFragment();Bundle args = new Bundle();args.putInt(ProgressbarFragment.ARG_POSITION, position);newFragment.setArguments(args);FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();transaction.replace(R.id.fragmentContainer, newFragment,"progress");transaction.commit();

The above is a piece of code used to pass the progress data of the progress bar. poison represents a number of the Progress 100 types. In ProgressbarFragment, use the following method to obtain the data passed in the past.


int position = getArguments().getInt(ARG_POSITION);

It may be common till now and no problems have been found. There is no problem when you look left and right on your phone. Yes, there will be no problems now, and there will be no problems in the future. The reason for all this is that the Android programming authority guide has helped you avoid many problems.

When the Activity is re-created, it will re-build the Fragment it manages. The original Fragment field values will all be lost, but through Fragment. the Bundle set by the setArguments (bundle) method is retained. Therefore, try to use the Fragment. setArguments (Bundle bundle) method to pass parameters.

Use Fragment in the selection interface for data to be returned

First, we need to create a Fragment. In this Fragment, we want to return data. We need to declare an interface and rewrite it where we need to accept data.

public class ChoseServerFragment extends Fragment {    private ArrayAdapter<String> mAdapter;    private String[] servers;    public static String FRAGMENT = "com.example.lijing.diablo3armory.choseserverfragment";    OnArticleSelectedListener mListener;    public interface OnArticleSelectedListener {        public void onServerSelected(String servername);    }    @Override    public void onAttach(Activity activity) {        super.onAttach(activity);        try {            mListener = (OnArticleSelectedListener) activity;        } catch (ClassCastException e) {            throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");        }    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {        // Inflate the layout for this fragment        View v = inflater.inflate(R.layout.server_fragment, container, false);        iniListView(v);        return v;    }    private void iniListView(View v) {        servers = getActivity().getResources().getStringArray(R.array.servers);        ListView serverListView = (ListView)v.findViewById(R.id.choseServerListView);        mAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1,servers);        serverListView.setAdapter(mAdapter);        serverListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {                String servername = servers[i];                mListener.onServerSelected(servername);            }        });    }}

In the code, the interface is declared in this way.

OnArticleSelectedListener mListener;public interface OnArticleSelectedListener {    public void onServerSelected(String servername);}

In onItemClick (), call

mListener.onServerSelected(servername);

In this Fragment, We need to display a Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:layout_width="match_parent"              android:layout_height="match_parent">    <TextView        android:gravity="center"        android:text="@string/choseyourserver"        android:textSize="20dp"        android:layout_width="fill_parent"        android:layout_height="wrap_content"/>    <ListView        android:id="@+id/choseServerListView"        android:layout_width="fill_parent"        android:layout_height="fill_parent">        </ListView></LinearLayout>

Here, a server list is displayed, which shows the effect.

In this App, I call this Fragment in the Activity. First, implements.

public class MainActivity extends FragmentActivity    implements ChoseServerFragment.OnArticleSelectedListener{

Then rewrite this interface in this Activity.

@Overridepublic void onServerSelected(String servername) {    ChoseServerFragment fragment = (ChoseServerFragment)getSupportFragmentManager()            .findFragmentByTag(ChoseServerFragment.FRAGMENT);    if(fragment != null) {        savePreferences(servername);        ChoseServerFragment removeFragment = (ChoseServerFragment)getSupportFragmentManager().findFragmentByTag(ChoseServerFragment.FRAGMENT);        getSupportFragmentManager().beginTransaction().remove(removeFragment);        iniApp();    }}

This Fragment only needs to be rewritten, whether in Activity or Fragment.

Summary

This chapter describes the second way to use Fragment (the first way to see http://www.cnblogs.com/canglin/p/4439666.html ). It has the following advantages:

  • The UI has better adaptability.
  • Bundle data will not be lost.
  • Highly reusable.

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.