请帮助我对此感到震惊 2 天。我想用 fastscroll 和 albhabet 索引器实现 ListView,如联系人应用程序/下图所示。我正在使用 SimpleAdapter 来填充 ListView。从图中可以看出,通过从右侧的字母索引器中选择一个字母,listView 的选择会转到相应的 ListItem。请分享一些例子。提前谢谢你。EX:下面是我的代码,它只是对字母进行排序,当我点击 E 时,与 E 相关的字母数据是 gng 到 diaplay ..但是当我触摸字母时,它没有在中间显示 E 文本框。
如何在中间显示 E 文本框..当我触摸 fastscroll 中间文本框时必须显示。
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);
}
高效适配器.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();
}
}