If you wanna the application to respond on a long click you have to implement Context Menu.
Here is an example how to create and register Context Menu.
Responding to an action is left empty to implement what needed.
I did use some code of the previous blog: Android : The scroll list. If you need the activity which started this frame go to this blog.
This is the result of the following code.
package com.mamutek.context.menu.android; import java.util.ArrayList; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.util.Log; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.ArrayAdapter; import android.widget.CheckBox; import android.widget.ListView; import android.widget.RadioButton; import android.widget.TextView; /** * @author Peter Gostincar */ public class ScrollFragment extends ListFragment{ private ArrayListmItems; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); getActivity().setTitle("Title"); mItems = createSomeItems(); CostumiseAdapter adapter = new CostumiseAdapter(mItems); setListAdapter(adapter); } /** implementing ContextMenu */ @Override public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo){ getActivity().getMenuInflater().inflate(R.menu.context_resource, menu); } /** registering ContextMenu*/ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View view = super.onCreateView(inflater, container, savedInstanceState); ListView list = (ListView)view.findViewById(android.R.id.list); registerForContextMenu(list); return view; } /**responding to Click*/ @Override public boolean onContextItemSelected(MenuItem item){ AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo(); int position = info.position; CostumiseAdapter adapter = (CostumiseAdapter)getListAdapter(); ImagineItem i = adapter.getItem(position); //TODO operation that need to be done int id = item.getItemId(); if(id == R.id.item_edit){ //do what it's need to be done } return super.onContextItemSelected(item); } private ArrayList createSomeItems() { ArrayList ret = new ArrayList (); for(int i = 0; i < 30; i++){ ret.add(new ImagineItem(i)); } return ret; } /** * @author Peter Gostincar * costumise adapter to show more than just Strings */ private class CostumiseAdapter extends ArrayAdapter { public CostumiseAdapter(ArrayList items) { super(getActivity(), 0, items); } @Override public View getView(int position, View convertView, ViewGroup parent){ if(convertView == null){ convertView = getActivity().getLayoutInflater() .inflate(R.layout.scroll_fragment, null); } ImagineItem item = getItem(position); TextView text = (TextView) convertView.findViewById(R.id.text_view); text.setText(item.mString); RadioButton b1 = (RadioButton) convertView.findViewById(R.id.radio_b1); b1.setChecked(item.mB1); RadioButton b2 = (RadioButton) convertView.findViewById(R.id.radio_b2); b2.setChecked(item.mB2); CheckBox cB = (CheckBox) convertView.findViewById(R.id.check_box); cB.setChecked(item.mB2); return convertView; } } /** * @author Peter Gostincar * class just for creating list of objects */ private class ImagineItem{ String mString = "Tralalal: "; boolean mCheckBox; boolean mB1; boolean mB2; public ImagineItem(int i) { mString += i; if(i % 2 == 0){ mCheckBox = true; } int x = (int) (Math.random()*3+1); if(x == 1) mB1 = true; if(x == 2) mB2 = true; } } }
menu/context_resource.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/item_edit"
android:icon="@android:drawable/ic_menu_edit"
android:title="@string/update"
/>
</menu>
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
scroll_fragmnet.xml
<?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" >
<CheckBox
android:id="@+id/check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:focusable="false" />
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/check_box"
android:padding="8dp" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/text_view"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio_b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_one"
android:focusable="false" />
<RadioButton
android:id="@+id/radio_b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_two"
android:focusable="false" />
</RadioGroup>
</RelativeLayout>
No comments:
Post a Comment