如何实现与fastscroll和albhabet索引一个ListView [英] How to implement a ListView with fastscroll and albhabet indexer
问题描述
请帮我打这个2 days.I想实现与fastscroll和albhabet索引一个ListView作为联系人应用程序/下面的图片。我使用的是SimpleAdapter来填充ListView。由于从图像中看出,在恰当的选择,从字母索引一个字母,列表视图选择进入相应的列表项。请分享一些例子。感谢ü在前进。 EX:下面是我的code,它只是排序字母表,当我的onclick就与E E字母数据GNG当我触摸字母来diaplay..But它没有显示ê文本框的中间。
如何middle..When显示E文中我抚摸fastscroll中间的文本框必须显示。
MainActivity.java
公共类MainActivity扩展活动实现SearchView.OnQueryTextListener,SearchView.OnCloseListener {
私人的ListView ListView的;
私人搜索查看搜索;
EfficientAdapter objectAdapter;
EfficientAdapter2 objectAdapter1;
INT长度限制:Textlength = 0;
私人复选框checkStat,checkRoutine,checkTat;
私人GestureDetector mGestureDetector;
我们这边索引中// x和y坐标
私有静态浮动sideIndexX;
私有静态浮动sideIndexY;
//侧指数高度
私人诠释sideIndexHeight;
//在一旁索引项目数量
私人诠释indexListSize;
//名单与侧指标项目
私人的ArrayList<对象[] GT; indexList = NULL;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.homempleb);
Log.i(扫描,txtScanResult);
Arrays.sort(CountriesList.name);
ActionItem nextItem =新ActionItem();
最后QuickAction quickAction =新QuickAction(这一点,QuickAction.VERTICAL);
quickAction.addActionItem(nextItem);
quickAction.setOnDismissListener(新QuickAction.OnDismissListener(){
@覆盖
公共无效onDismiss(){
Toast.makeText(getApplicationContext(),驳回,Toast.LENGTH_SHORT).show();
}
});
ListView的=(的ListView)findViewById(R.id.homelistView);
listView.setTextFilterEnabled(真正的);
objectAdapter =新EfficientAdapter(本);
mGestureDetector =新GestureDetector(这一点,新SideIndexGestureListener());
listView.setAdapter(objectAdapter);
}
@覆盖
公共布尔的onTouchEvent(MotionEvent事件)
{
如果(mGestureDetector.onTouchEvent(事件))
{
返回true;
} 其他
{
返回false;
}
}
私人的ArrayList<对象[] GT;的createIndex(字符串[] strArr)
{
ArrayList的<对象[] GT; tmpIndexList =新的ArrayList<对象[] GT;();
[对象] tmpIndexItem = NULL;
INT tmpPos = 0;
字符串tmpLetter =;
字符串currentLetter = NULL;
字符串strItem = NULL;
对于(INT J = 0; J< strArr.length; J ++)
{
strItem = strArr [J]。
currentLetter = strItem.substring(0,1);
//每一次新的字母来
//保存到索引列表
如果(!currentLetter.equals(tmpLetter))
{
tmpIndexItem =新的对象[3]。
tmpIndexItem [0] = tmpLetter;
tmpIndexItem [1] = tmpPos - 1;
tmpIndexItem [2] = j的 - 1;
tmpLetter = currentLetter;
tmpPos = J + 1;
tmpIndexList.add(tmpIndexItem);
}
}
//还保存最后一封信
tmpIndexItem =新的对象[3]。
tmpIndexItem [0] = tmpLetter;
tmpIndexItem [1] = tmpPos - 1;
tmpIndexItem [2] = strArr.length - 1;
tmpIndexList.add(tmpIndexItem);
//并删除第一次临时空条目
如果(tmpIndexList =空&安培;!&安培; tmpIndexList.size()大于0)
{
tmpIndexList.remove(0);
}
返回tmpIndexList;
}
@覆盖
公共无效onWindowFocusChanged(布尔hasFocus)
{
super.onWindowFocusChanged(hasFocus);
最终的ListView ListView的=(的ListView)findViewById(R.id.homelistView);
的LinearLayout sideIndex =(的LinearLayout)findViewById(R.id.sideIndex);
sideIndexHeight = sideIndex.getHeight();
sideIndex.removeAllViews();
// TextView中的每一个可见项目
TextView的tmpTV = NULL;
//我们将创建的索引列表
indexList =的createIndex(CountriesList.name);
//索引列表中的项目数量
indexListSize = indexList.size();
//最大数目的项目的,它可以显示
INT indexMaxSize =(INT)Math.floor(sideIndex.getHeight()/ 20);
INT tmpIndexListSize = indexListSize;
//处理这种情况下,当indexListSize> indexMaxSize
而(tmpIndexListSize> indexMaxSize)
{
tmpIndexListSize = tmpIndexListSize / 2;
}
//计算增量(项只有一部分会显示保存
// 地点)
双Δ= indexListSize / tmpIndexListSize;
字符串tmpLetter = NULL;
[对象] tmpIndexItem = NULL;
//显示每第m个信
对于(双I = 1; I< = indexListSize; I = I +增量)
{
tmpIndexItem = indexList.get((INT)I - 1);
tmpLetter = tmpIndexItem [0]的ToString();
tmpTV =新的TextView(本);
tmpTV.setText(tmpLetter);
tmpTV.setGravity(Gravity.CENTER);
tmpTV.setTextSize(20);
的LayoutParams PARAMS =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,1);
tmpTV.setLayoutParams(PARAMS);
sideIndex.addView(tmpTV);
}
//并设置一个触摸监听器为它
sideIndex.setOnTouchListener(新OnTouchListener()
{
@覆盖
公共布尔onTouch(视图V,MotionEvent事件)
{
//现在你知道触摸的坐标
sideIndexX = event.getX();
sideIndexY = event.getY();
//并且可以显示正确的项目是国家列表
displayListItem();
返回false;
}
});
}
类SideIndexGestureListener扩展
GestureDetector.SimpleOnGestureListener
{
@覆盖
公共布尔onScroll(MotionEvent E1,E2 MotionEvent,
浮distanceX,浮动distanceY)
{
//我们已经知道了坐标第一次触球的
//我们知道还有一个滚动的距离
sideIndexX = sideIndexX - distanceX;
sideIndexY = sideIndexY - distanceY;
//当我们的身边指数中的用户滚动
//我们可以显示在它适当的每一个位置
//在国家列表项
如果(sideIndexX> = 0&安培;&安培; sideIndexY> = 0)
{
displayListItem();
}
返回super.onScroll(E1,E2,distanceX,distanceY);
}
}
公共无效displayListItem()
{
像素的每一个侧面索引项//计算数量
双pixelPerIndexItem =(双)sideIndexHeight / indexListSize;
//计算项目索引指定事件的位置属于
INT itemPosition =(INT)(sideIndexY / pixelPerIndexItem);
//计算极小位置为在列表中的项目
INT minPosition =(INT)(itemPosition * pixelPerIndexItem);
//获得该项目(我们可以做到这一点,因为我们知道项目索引)
[对象] indexItem = indexList.get(itemPosition);
//并计算在国内列表中选择合适的项目
INT indexMin =的Integer.parseInt(indexItem [1]的ToString());
INT indexMax =的Integer.parseInt(indexItem [2]的ToString());
INT indexDelta = Math.max(1,indexMax - indexMin);
双pixelPerSubitem = pixelPerIndexItem / indexDelta;
INT subitemPosition =(INT)(indexMin +(sideIndexY - minPosition)/ pixelPerSubitem);
ListView控件的ListView =(ListView控件)findViewById(R.id.homelistView);
listView.setSelection(subitemPosition);
}
EfficientAdapter.java
公共类EfficientAdapter扩展了BaseAdapter {
私人LayoutInflater mInflater;
私人上下文的背景下;
公共EfficientAdapter(上下文的背景下){
mInflater = LayoutInflater.from(上下文);
this.context =背景;
}
公众诠释getCount将(){
返回CountriesList.name.length;
}
公共对象的getItem(INT位置){
返回的位置;
}
众长getItemId(INT位置){
返回的位置;
}
公共查看getView(INT位置,查看convertView,ViewGroup中父){
ViewHolder持有人;
如果(convertView == NULL){
convertView = mInflater.inflate(R.layout.homemplebrowview,NULL);
持有人=新ViewHolder();
holder.text1 =(TextView中)convertView
.findViewById(R.id.name);
holder.text2 =(TextView中)convertView
.findViewById(R.id.mrn);
holder.text3 =(TextView中)convertView
.findViewById(R.id.date);
holder.text4 =(TextView中)convertView
.findViewById(R.id.age);
holder.text5 =(TextView中)convertView
.findViewById(R.id.gender);
holder.text6 =(TextView中)convertView
.findViewById(R.id.wardno);
holder.text7 =(TextView中)convertView
.findViewById(R.id.roomno);
holder.text8 =(TextView中)convertView
.findViewById(R.id.bedno);
holder.btnList =(按钮)convertView.findViewById(R.id.listbutton);
// holder.btnList.setOnClickListener(本);
holder.btnList.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
接下来的意图=新的意图(背景下,SeviceDetails.class);
context.startActivity(下);
}
});
convertView.setTag(保持器);
} 其他 {
支架=(ViewHolder)convertView.getTag();
}
holder.text1.setText(CountriesList.name [位置]);
holder.text2.setText(CountriesList.mrn [位置]);
holder.text3.setText(CountriesList.actualstart [位置]);
holder.text4.setText(CountriesList.age [位置]);
holder.text5.setText(CountriesList.gender [位置]);
holder.text6.setText(CountriesList.wardNo [位置]);
holder.text7.setText(CountriesList.roomNo [位置]);
holder.text8.setText(CountriesList.bedNo [位置]);
返回convertView;
}
静态类ViewHolder {
公众按钮btnList;
公众的TextView text8;
公众的TextView text7;
公众的TextView text6;
公众的TextView text5;
公共TextView的文本4;
公众的TextView文本1;
公众的TextView文本2;
公众的TextView文本3;
}
@覆盖
公共无效notifyDataSetChanged()
{
super.notifyDataSetChanged();
}
}
您好:看到这些例子
<一个href="http://stackoverflow.com/questions/6475410/listview-with-alphabets-on-the-right-like-the-iphone-is-it-possible">Example1
<一个href="http://twistbyte.com/tutorial/android-listview-with-fast-scroll-and-section-index#$c$csyntax_1"相对=nofollow>例题
Please help I am struck in this for 2 days.I would like to implement a ListView with fastscroll and albhabet indexer as in the contacts application/below image. I'm using a SimpleAdapter to populate the ListView. As seen from the image, by selecting a letter from the alphabet indexer at the right, the listView selection goes to the corresponding ListItem. Please share some examples. Thank u In advance. EX: Below is my code it is just sorting the alphabet and when i onclick on E alphabet data related to E is gng to diaplay..But it is not showing E textbox in middle when i on touch of alphabet.
How to display E text box in middle..When i touch fastscroll middle text box must show.
MainActivity.java
public class MainActivity extends Activity implements SearchView.OnQueryTextListener,SearchView.OnCloseListener {
private ListView listView;
private SearchView search;
EfficientAdapter objectAdapter;
EfficientAdapter2 objectAdapter1;
int textlength=0;
private CheckBox checkStat, checkRoutine, checkTat;
private GestureDetector mGestureDetector;
// x and y coordinates within our side index
private static float sideIndexX;
private static float sideIndexY;
// height of side index
private int sideIndexHeight;
// number of items in the side index
private int indexListSize;
// list with items for side index
private ArrayList<Object[]> indexList = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.homempleb);
Log.i("scan"," txtScanResult ");
Arrays.sort(CountriesList.name);
ActionItem nextItem = new ActionItem();
final QuickAction quickAction = new QuickAction(this, QuickAction.VERTICAL);
quickAction.addActionItem(nextItem);
quickAction.setOnDismissListener(new QuickAction.OnDismissListener() {
@Override
public void onDismiss() {
Toast.makeText(getApplicationContext(), "Dismissed", Toast.LENGTH_SHORT).show();
}
});
listView = (ListView) findViewById(R.id.homelistView);
listView.setTextFilterEnabled(true);
objectAdapter = new EfficientAdapter(this);
mGestureDetector = new GestureDetector(this, new SideIndexGestureListener());
listView.setAdapter(objectAdapter);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
if (mGestureDetector.onTouchEvent(event))
{
return true;
} else
{
return false;
}
}
private ArrayList<Object[]> createIndex(String[] strArr)
{
ArrayList<Object[]> tmpIndexList = new ArrayList<Object[]>();
Object[] tmpIndexItem = null;
int tmpPos = 0;
String tmpLetter = "";
String currentLetter = null;
String strItem = null;
for (int j = 0; j < strArr.length; j++)
{
strItem = strArr[j];
currentLetter = strItem.substring(0, 1);
// every time new letters comes
// save it to index list
if (!currentLetter.equals(tmpLetter))
{
tmpIndexItem = new Object[3];
tmpIndexItem[0] = tmpLetter;
tmpIndexItem[1] = tmpPos - 1;
tmpIndexItem[2] = j - 1;
tmpLetter = currentLetter;
tmpPos = j + 1;
tmpIndexList.add(tmpIndexItem);
}
}
// save also last letter
tmpIndexItem = new Object[3];
tmpIndexItem[0] = tmpLetter;
tmpIndexItem[1] = tmpPos - 1;
tmpIndexItem[2] = strArr.length - 1;
tmpIndexList.add(tmpIndexItem);
// and remove first temporary empty entry
if (tmpIndexList != null && tmpIndexList.size() > 0)
{
tmpIndexList.remove(0);
}
return tmpIndexList;
}
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
final ListView listView = (ListView) findViewById(R.id.homelistView);
LinearLayout sideIndex = (LinearLayout) findViewById(R.id.sideIndex);
sideIndexHeight = sideIndex.getHeight();
sideIndex.removeAllViews();
// TextView for every visible item
TextView tmpTV = null;
// we'll create the index list
indexList = createIndex(CountriesList.name);
// number of items in the index List
indexListSize = indexList.size();
// maximal number of item, which could be displayed
int indexMaxSize = (int) Math.floor(sideIndex.getHeight() / 20);
int tmpIndexListSize = indexListSize;
// handling that case when indexListSize > indexMaxSize
while (tmpIndexListSize > indexMaxSize)
{
tmpIndexListSize = tmpIndexListSize / 2;
}
// computing delta (only a part of items will be displayed to save a
// place)
double delta = indexListSize / tmpIndexListSize;
String tmpLetter = null;
Object[] tmpIndexItem = null;
// show every m-th letter
for (double i = 1; i <= indexListSize; i = i + delta)
{
tmpIndexItem = indexList.get((int) i - 1);
tmpLetter = tmpIndexItem[0].toString();
tmpTV = new TextView(this);
tmpTV.setText(tmpLetter);
tmpTV.setGravity(Gravity.CENTER);
tmpTV.setTextSize(20);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);
tmpTV.setLayoutParams(params);
sideIndex.addView(tmpTV);
}
// and set a touch listener for it
sideIndex.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
// now you know coordinates of touch
sideIndexX = event.getX();
sideIndexY = event.getY();
// and can display a proper item it country list
displayListItem();
return false;
}
});
}
class SideIndexGestureListener extends
GestureDetector.SimpleOnGestureListener
{
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY)
{
// we know already coordinates of first touch
// we know as well a scroll distance
sideIndexX = sideIndexX - distanceX;
sideIndexY = sideIndexY - distanceY;
// when the user scrolls within our side index
// we can show for every position in it a proper
// item in the country list
if (sideIndexX >= 0 && sideIndexY >= 0)
{
displayListItem();
}
return super.onScroll(e1, e2, distanceX, distanceY);
}
}
public void displayListItem()
{
// compute number of pixels for every side index item
double pixelPerIndexItem = (double) sideIndexHeight / indexListSize;
// compute the item index for given event position belongs to
int itemPosition = (int) (sideIndexY / pixelPerIndexItem);
// compute minimal position for the item in the list
int minPosition = (int) (itemPosition * pixelPerIndexItem);
// get the item (we can do it since we know item index)
Object[] indexItem = indexList.get(itemPosition);
// and compute the proper item in the country list
int indexMin = Integer.parseInt(indexItem[1].toString());
int indexMax = Integer.parseInt(indexItem[2].toString());
int indexDelta = Math.max(1, indexMax - indexMin);
double pixelPerSubitem = pixelPerIndexItem / indexDelta;
int subitemPosition = (int) (indexMin + (sideIndexY - minPosition) / pixelPerSubitem);
ListView listView = (ListView) findViewById(R.id.homelistView);
listView.setSelection(subitemPosition);
}
EfficientAdapter.java
public class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Context context;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
this.context=context;
}
public int getCount() {
return CountriesList.name.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.homemplebrowview, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.name);
holder.text2 = (TextView) convertView
.findViewById(R.id.mrn);
holder.text3 = (TextView) convertView
.findViewById(R.id.date);
holder.text4 = (TextView) convertView
.findViewById(R.id.age);
holder.text5 = (TextView) convertView
.findViewById(R.id.gender);
holder.text6 = (TextView) convertView
.findViewById(R.id.wardno);
holder.text7 = (TextView) convertView
.findViewById(R.id.roomno);
holder.text8 = (TextView) convertView
.findViewById(R.id.bedno);
holder.btnList = (Button)convertView.findViewById(R.id.listbutton);
// holder.btnList.setOnClickListener(this);
holder.btnList.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent next=new Intent(context, SeviceDetails.class);
context.startActivity(next);
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(CountriesList.name[position]);
holder.text2.setText(CountriesList.mrn[position]);
holder.text3.setText(CountriesList.actualstart[position]);
holder.text4.setText(CountriesList.age[position]);
holder.text5.setText(CountriesList.gender[position]);
holder.text6.setText(CountriesList.wardNo[position]);
holder.text7.setText(CountriesList.roomNo[position]);
holder.text8.setText(CountriesList.bedNo[position]);
return convertView;
}
static class ViewHolder {
public Button btnList;
public TextView text8;
public TextView text7;
public TextView text6;
public TextView text5;
public TextView text4;
public TextView text1;
public TextView text2;
public TextView text3;
}
@Override
public void notifyDataSetChanged()
{
super.notifyDataSetChanged();
}
}
Hello See these examples
这篇关于如何实现与fastscroll和albhabet索引一个ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!