android API demo中的实例,滚动列表时显示一个类似Toast的提示,用于表示滚动到了不同的区域了。
list_position.xml
CollapsedListView可折叠的ListView:
从下往上添加item的ListView:
public class List9 extends ListActivity implements ListView.OnScrollListener { private final class RemoveWindow implements Runnable { public void run() { removeWindow(); } } private RemoveWindow mRemoveWindow = new RemoveWindow(); Handler mHandler = new Handler(); private WindowManager mWindowManager; private TextView mDialogText; private boolean mShowing; private boolean mReady; private char mPrevLetter = Character.MIN_VALUE; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mWindowManager = (WindowManager)getSystemService(Context.WINDOW_SERVICE); // Use an existing ListAdapter that will map an array // of strings to TextViews setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings)); getListView().setOnScrollListener(this); LayoutInflater inflate = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); mDialogText = (TextView) inflate.inflate(R.layout.list_position, null); mDialogText.setVisibility(View.INVISIBLE); mHandler.post(new Runnable() { public void run() { mReady = true; WindowManager.LayoutParams lp = new WindowManager.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); mWindowManager.addView(mDialogText, lp); }}); } @Override protected void onResume() { super.onResume(); mReady = true; } @Override protected void onPause() { super.onPause(); removeWindow(); mReady = false; } @Override protected void onDestroy() { super.onDestroy(); mWindowManager.removeView(mDialogText); mReady = false; } public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { int lastItem = firstVisibleItem + visibleItemCount - 1; if (mReady) { char firstLetter = mStrings[firstVisibleItem].charAt(0); if (!mShowing && firstLetter != mPrevLetter) { mShowing = true; mDialogText.setVisibility(View.VISIBLE); } mDialogText.setText(((Character)firstLetter).toString()); mHandler.removeCallbacks(mRemoveWindow); mHandler.postDelayed(mRemoveWindow, 3000); mPrevLetter = firstLetter; } } public void onScrollStateChanged(AbsListView view, int scrollState) { } private void removeWindow() { if (mShowing) { mShowing = false; mDialogText.setVisibility(View.INVISIBLE); } } private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", ....... "Aubisque Pyrenees", "Autun", "Avaxtskyr", "Baby Swiss", "Babybel", "Baguette Laonnaise", "Bakers", "Baladi", "Balaton", "Bandal", ....... "Button (Innes)", "Buxton Blue", "Cabecou", "Caboc", "Cabrales", "Cachaille", "Caciocavallo", "Caciotta", "Caerphilly", ....... } }
list_position.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:textSize="50sp" android:textColor="#99FFFFFF" android:background="#BB000000" android:minWidth="70dip" android:maxWidth="70dip" android:padding="10dip" android:gravity="center" />
CollapsedListView可折叠的ListView:
import android.app.ListActivity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; /** * A list view example where the * data comes from a custom * ListAdapter */ public class List6 extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Use our own list adapter setListAdapter(new SpeechListAdapter(this)); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { ((SpeechListAdapter)getListAdapter()).toggle(position); } /** * A sample ListAdapter that presents content * from arrays of speeches and text. * */ private class SpeechListAdapter extends BaseAdapter { public SpeechListAdapter(Context context) { mContext = context; } /** * The number of items in the list is determined by the number of speeches * in our array. * * @see android.widget.ListAdapter#getCount() */ public int getCount() { return mTitles.length; } /** * Since the data comes from an array, just returning * the index is sufficent to get at the data. If we * were using a more complex data structure, we * would return whatever object represents one * row in the list. * * @see android.widget.ListAdapter#getItem(int) */ public Object getItem(int position) { return position; } /** * Use the array index as a unique id. * @see android.widget.ListAdapter#getItemId(int) */ public long getItemId(int position) { return position; } /** * Make a SpeechView to hold each row. * @see android.widget.ListAdapter#getView(int, android.view.View, android.view.ViewGroup) */ public View getView(int position, View convertView, ViewGroup parent) { SpeechView sv; if (convertView == null) { sv = new SpeechView(mContext, mTitles[position], mDialogue[position], mExpanded[position]); } else { sv = (SpeechView)convertView; sv.setTitle(mTitles[position]); sv.setDialogue(mDialogue[position]); sv.setExpanded(mExpanded[position]); } return sv; } public void toggle(int position) { mExpanded[position] = !mExpanded[position]; notifyDataSetChanged(); } /** * Remember our context so we can use it when constructing views. */ private Context mContext; /** * Our data, part 1. */ private String[] mTitles = { "Henry IV (1)", "Henry V", "Henry VIII", "Richard II", "Richard III", "Merchant of Venice", "Othello", "King Lear" }; /** * Our data, part 2. */ private String[] mDialogue = { "So shaken as we are, so wan with care," + ...... "In forwarding this dear expedience.", "Hear him but reason in divinity," + ...... "From open haunts and popularity.", "I come no more to make you laugh: things now," + ...... "A man may weep upon his wedding-day.", "First, heaven be the record to my speech!" + ...... "What my tongue speaks my right drawn sword may prove.", "Now is the winter of our discontent" + ...... "Clarence comes.", "To bait fish withal: if it will feed nothing else," + ...... "will better the instruction.", "Virtue! a fig! 'tis in ourselves that we are thus" + ...... "you call love to be a sect or scion.", "Blow, winds, and crack your cheeks! rage! blow!" + ...... "That make ingrateful man!" }; /** * Our data, part 3. */ private boolean[] mExpanded = { false, false, false, false, false, false, false, false }; } /** * We will use a SpeechView to display each speech. It's just a LinearLayout * with two text fields. * */ private class SpeechView extends LinearLayout { public SpeechView(Context context, String title, String dialogue, boolean expanded) { super(context); this.setOrientation(VERTICAL); // Here we build the child views in code. They could also have // been specified in an XML file. mTitle = new TextView(context); mTitle.setText(title); addView(mTitle, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); mDialogue = new TextView(context); mDialogue.setText(dialogue); addView(mDialogue, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); mDialogue.setVisibility(expanded ? VISIBLE : GONE); } /** * Convenience method to set the title of a SpeechView */ public void setTitle(String title) { mTitle.setText(title); } /** * Convenience method to set the dialogue of a SpeechView */ public void setDialogue(String words) { mDialogue.setText(words); } /** * Convenience method to expand or hide the dialogue */ public void setExpanded(boolean expanded) { mDialogue.setVisibility(expanded ? VISIBLE : GONE); } private TextView mTitle; private TextView mDialogue; } }
从下往上添加item的ListView:
import android.app.ListActivity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.widget.ArrayAdapter; import android.widget.EditText; import com.example.android.apis.R; import java.util.ArrayList; /** * Demonstrates the using a list view in transcript mode * */ public class List12 extends ListActivity implements OnClickListener, OnKeyListener { private EditText mUserText; private ArrayAdapter<String> mAdapter; private ArrayList<String> mStrings = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list_12); mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings); setListAdapter(mAdapter); mUserText = (EditText) findViewById(R.id.userText); mUserText.setOnClickListener(this); mUserText.setOnKeyListener(this); } public void onClick(View v) { sendText(); } private void sendText() { String text = mUserText.getText().toString(); mAdapter.add(text); mUserText.setText(null); } public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_CENTER: case KeyEvent.KEYCODE_ENTER: sendText(); return true; } } return false; } }
<?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" android:paddingLeft="8dip" android:paddingRight="8dip"> <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" android:stackFromBottom="true" android:transcriptMode="normal"/> <EditText android:id="@+id/userText" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>