Last active
December 29, 2015 17:39
-
-
Save ishitcno1/7705425 to your computer and use it in GitHub Desktop.
Android use ListFragment and SimpleAdapter. Creating event callbacks to the activity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/holder" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<fragment | |
android:name="package.name.SampleFragment" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
</fragment> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<ImageView | |
android:layout_width="wrap_content" | |
android:layout_height="match_parent" | |
android:id="@+id/icon" | |
android:src="@drawable/ic_launcher" /> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="26dip" | |
android:id="@+id/firstLine" | |
android:layout_toRightOf="@id/icon" | |
android:textSize="16sp" /> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="26dip" | |
android:id="@+id/secondLine" | |
android:layout_toRightOf="@id/icon" | |
android:layout_below="@id/firstLine" | |
android:textSize="12sp" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle; | |
import android.support.v4.app.FragmentActivity; | |
import android.widget.Toast; | |
public class MainActivity extends FragmentActivity implements SampleFragment.OnArticleSelectedListener { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
@Override | |
public void onArticleSelected() { | |
Toast.makeText(this, "Article selected.", Toast.LENGTH_SHORT).show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.os.Bundle; | |
import android.support.v4.app.ListFragment; | |
import android.view.View; | |
import android.widget.ListView; | |
import android.widget.SimpleAdapter; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
public class SampleFragment extends ListFragment { | |
private OnArticleSelectedListener mListener; | |
@Override | |
public void onActivityCreated(Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
String[] values = new String[]{"Android", "iPhone", "WindowsMobile", | |
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", | |
"Linux", "OS/2"}; | |
ArrayList list = new ArrayList(); | |
for (int i = 0; i < values.length; i++) { | |
HashMap hashMap = new HashMap(); | |
hashMap.put("name", values[i]); | |
hashMap.put("description", "some description"); | |
hashMap.put("icon", R.drawable.ic_launcher); | |
list.add(hashMap); | |
} | |
String[] from = new String[]{"name", "description", "icon"}; | |
int[] to = new int[]{R.id.firstLine, R.id.secondLine, R.id.icon}; | |
this.setListAdapter(new SimpleAdapter(getActivity(), list, R.layout.detail, from, to)); | |
} | |
@Override | |
public void onAttach(Activity activity) { | |
super.onAttach(activity); | |
try { | |
mListener = (OnArticleSelectedListener) activity; | |
} catch (ClassCastException e) { | |
} | |
} | |
@Override | |
public void onListItemClick(ListView l, View v, int position, long id) { | |
mListener.onArticleSelected(); | |
} | |
public interface OnArticleSelectedListener { | |
public void onArticleSelected(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment