在写这篇文章之前,xxx已经写过了几篇关于改界面效果主题的文章,想要了解的朋友可以去翻一下之前的文章
后面已经实现了滑动界面的计划部份,但详细怎么运行起来还没有说明
下面先看一下效果
让界面真正的滑动起来,并且有这样的效果,需要对左边和右边都实行侦听
左边滑动时,悬浮关键字键不显示,而滑动右边时,显示悬浮关键字键,同时滑动列表快速滑动。
Friend.java
每日一道理 爱,有的时候不需要山盟海誓的承诺,但她一定需要细致入微的关怀与问候;爱,有的时候不需要梁祝化蝶的悲壮,但她一定需要心有灵犀的默契与投合;爱,有的时候不需要雄飞雌从的追随,但她一定需要相濡以沫的支持与懂得。
package com.example.test_intent;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.regex.Pattern;import android.app.Activity;import android.content.AsyncQueryHandler;import android.content.ComponentName;import android.content.ContentResolver;import android.content.ContentValues;import android.content.Context;import android.content.Intent;import android.database.Cursor;import android.graphics.PixelFormat;import android.net.Uri;import android.os.Bundle;import android.os.Handler;import android.provider.ContactsContract;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.view.Window;import android.view.WindowManager;import android.widget.AbsListView;import android.widget.AbsListView.OnScrollListener;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import com.example.widget.MyLetterListView;import com.example.widget.MyLetterListView.OnTouchingLetterChangedListener;public class Friend extends Activity{ private BaseAdapter adapter; private ListView listview; private TextView overlay; private ImageView suspend_search; private MyLetterListView letterListView; private AsyncQueryHandler asyncQuery; private static final String NAME = "name", NUMBER = "number", SORT_KEY = "sort_key"; private HashMapalphaIndexer; private String[] sections; public List list = new ArrayList (); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.friend); asyncQuery = new MyAsyncQueryHandler(getContentResolver()); listview = (ListView) findViewById(R.id.list_view); letterListView = (MyLetterListView) findViewById(R.id.my_list_view); letterListView.setOnTouchingLetterChangedListener(new LetterListViewListener()); alphaIndexer = new HashMap (); new Handler(); new OverlayThread(); initOverlay(); initSuSearch(); if(list.size()>0){ } listview.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { suspend_search.setVisibility(View.VISIBLE); } @Override public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) { suspend_search.setVisibility(View.GONE); } }); suspend_search.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ComponentName friendcName=new ComponentName(Friend.this,"com.example.test_intent.FriendSearch"); Intent friend_viewIntent=new Intent(); friend_viewIntent.setComponent(friendcName); startActivity(friend_viewIntent); Toast.makeText(getApplicationContext(), R.string.search, Toast.LENGTH_LONG).show(); // TODO Auto-generated method stub } }); } @SuppressWarnings("deprecation") public void getContent() { Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); startManagingCursor(cur); } @Override protected void onResume() { super.onResume(); Uri uri = Uri.parse("content://com.android.contacts/data/phones"); String[] projection = { "_id", "display_name", "data1", "sort_key" }; asyncQuery.startQuery(0, null, uri, projection, null, null,"sort_key COLLATE LOCALIZED asc"); } private class MyAsyncQueryHandler extends AsyncQueryHandler { public MyAsyncQueryHandler(ContentResolver cr) { super(cr); } @Override protected void onQueryComplete(int token, Object cookie, Cursor cursor) { cursor.moveToFirst(); Log.d("ccccc", cursor.getString(0)+" 000 "+cursor.getString(1)+" 000 "+cursor.getString(2)+" 000 "+cursor.getString(3)); while (cursor.moveToNext()) { ContentValues cv = new ContentValues(); cv.put(NAME, cursor.getString(1)); cv.put(NUMBER, cursor.getString(2)); cv.put(SORT_KEY, cursor.getString(3)); list.add(cv); } if (list.size() > 0) { setAdapter(list); } } } private void setAdapter(List list) { adapter = new ListAdapter(this, list); listview.setAdapter(adapter); } private class ListAdapter extends BaseAdapter { private LayoutInflater inflater; private List list; public ListAdapter(Context context, List list) { this.inflater = LayoutInflater.from(context); this.list = list; alphaIndexer = new HashMap (); sections = new String[list.size()]; for (int i = 0; i < list.size(); i++) { String currentStr = getAlpha(list.get(i).getAsString(SORT_KEY)); String previewStr = (i - 1) >= 0 ? getAlpha(list.get(i - 1).getAsString(SORT_KEY)) : " "; if (!previewStr.equals(currentStr)) { String name = getAlpha(list.get(i).getAsString(SORT_KEY)); alphaIndexer.put(name, i); sections[i] = name; } } } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = inflater.inflate(R.layout.list_item, null); holder = new ViewHolder(); holder.alpha = (TextView) convertView.findViewById(R.id.alpha); holder.name = (TextView) convertView.findViewById(R.id.name); holder.number = (TextView) convertView.findViewById(R.id.number); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } ContentValues cv = list.get(position); holder.name.setText(cv.getAsString(NAME)); holder.number.setText(cv.getAsString(NUMBER)); String currentStr = getAlpha(list.get(position).getAsString(SORT_KEY)); String previewStr = (position - 1) >= 0 ? getAlpha(list.get(position - 1).getAsString(SORT_KEY)) : " "; if (!previewStr.equals(currentStr)) { holder.alpha.setVisibility(View.VISIBLE); holder.alpha.setText(currentStr); } else { holder.alpha.setVisibility(View.GONE); } return convertView; } private class ViewHolder { TextView alpha; TextView name; TextView number; } } private void initSuSearch()//搜索 { LayoutInflater inflater = LayoutInflater.from(this); suspend_search = (ImageView) inflater.inflate(R.layout.suspend_search, null); WindowManager.LayoutParams lp = new WindowManager.LayoutParams(80,80,170,-280, WindowManager.LayoutParams.TYPE_APPLICATION, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); windowManager.addView(suspend_search, lp); } private void initOverlay() { LayoutInflater inflater = LayoutInflater.from(this); overlay = (TextView) inflater.inflate(R.layout.overlay, null); WindowManager.LayoutParams lp = new WindowManager.LayoutParams(120,120,100,0, WindowManager.LayoutParams.TYPE_APPLICATION, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, PixelFormat.TRANSLUCENT); WindowManager windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE); windowManager.addView(overlay, lp); } private class LetterListViewListener implements OnTouchingLetterChangedListener { @Override public void onTouchingLetterChanged(final String s,float y,float x) { if(alphaIndexer.get(s) != null) { int position = alphaIndexer.get(s); listview.setSelection(position); overlay.setText(sections[position]); overlay.setVisibility(View.VISIBLE); } } @Override public void onTouchingLetterEnd() { overlay.setVisibility(View.GONE); } @Override public void onMoveSuspendSearch() { suspend_search.setVisibility(View.GONE); // TODO Auto-generated method stub } @Override public void onShowSuspendSearch() { suspend_search.setVisibility(View.VISIBLE); // TODO Auto-generated method stub } } private class OverlayThread implements Runnable { @Override public void run() { overlay.setVisibility(View.GONE); } } private String getAlpha(String str) { if (str == null) { return "#"; } if (str.trim().length() == 0) { return "#"; } char c = str.trim().substring(0, 1).charAt(0); Pattern pattern = Pattern.compile("^[A-Za-z]+$"); if (pattern.matcher(c + "").matches()) { return (c + "").toUpperCase(); } else { return "#"; } } }
滑动界面的计划部份应该来讲还是Android的UI部份比较简单的,多懂得多实际基本上就可以弄懂。
文章结束给大家分享下程序员的一些笑话语录: 这年头的互联网真是娱乐了中国,网民们从各种各样的“门”里钻来钻去,又有好多“哥”好多“帝”,值得大家品味不已……网络经典语录,关于IT与互联网,经典与您分享!