Tuesday, June 3, 2014

Create Android list fragment step by step according to slidenerd video



other examples

I think slidenerd video is the best, and there are other references below:


Watch  video => here (below is step by step of the video, if you watch video it is too quick, you need screen shots to use afterwards)

Create a project
add string-array as the data


create Java 


package com.gregchu.f1.app.dummy;

/**
 * Created by greg on 6/4/2014.
 */
public class MyListFragment extends ListFragment{{
}

create layout




add listview to layout just created above



add a text view (skipped picture)

then change the listview XML to android:id/list

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@android:id/list" />
</LinearLayout>

Change text view

 <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" />



Add onActivity created


public class MyListFragment extends ListFragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.my_list_fragment, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
}

Create Adaptor to use data from string-array

public class MyListFragment extends ListFragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.my_list_fragment, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
       
      ArrayAdapter adapter=  ArrayAdapter.createFromResource(getActivity(),R.array.chu,android.R.layout.simple_list_item_1);
    }
}

Add setListAdapter

public class MyListFragment extends ListFragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.my_list_fragment, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

      ArrayAdapter adapter=  ArrayAdapter.createFromResource(getActivity(),R.array.chu,android.R.layout.simple_list_item_1);
        setListAdapter(adapter);
    }
}

Add item click listener
/**
 * Created by greg on 6/4/2014.
 */
public class MyListFragment extends ListFragment implements AdapterView.OnItemClickListener{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.my_list_fragment, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

      ArrayAdapter adapter=  ArrayAdapter.createFromResource(getActivity(),R.array.chu,android.R.layout.simple_list_item_1);
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long id) {
        Toast.makeText(getActivity(),"Item "+i,Toast.LENGTH_SHORT).show();
    }
}




You can run on your device now. 
It took me more than 2 hours to create this step by step instruction according the video.

No comments:

Post a Comment