我有一个Sectioned ListView
标题,如" A" ," B" ,...当我第一次加载应用程序并向下滚动列表时," Z" 显示在prpoer位置。但每当我向上(向后移动)List
时,标题的位置都会发生变化,有时会与其他类别重叠。
我的自定义程序Adapter
是:
private class MySimpleCursorAdapter extends SimpleCursorAdapter {
Holder holder = null;
public MySimpleCursorAdapter(Context context, int layout, Cursor cur,
String[] from, int[] to, int flag) {
super(context, layout, cur, from, to, flag);
}
public String getTitle(String contName) {
return contName.substring(0, 1);
}
@Override
public void bindView(View view, Context context, Cursor mCursor) {
holder =new Holder();
final int curPosition = mCursor.getPosition();
String cont_Name = mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
holder.titleText= (TextView) view.findViewById(R.id.section_title);
holder.contactTitle= (TextView)view.findViewById(R.id.textView1);
holder.contactDetail = (TextView)view.findViewById(R.id.textView2);
holder.myImage = (ImageView) view.findViewById(R.id.imageView1);
Log.d("Cursor Postion " , ""+ curPosition);
if( curPosition == 0 ){
holder.titleText.setText(getTitle(cont_Name));
holder.titleText.setVisibility(View.VISIBLE);
keyWord = getTitle(cont_Name);
}
else if (keyWord.equalsIgnoreCase(getTitle(cont_Name))){
holder.titleText.setVisibility(View.GONE);
keyWord = getTitle(cont_Name);
}else if (keyWord != getTitle(cont_Name)){
holder.titleText.setVisibility(View.VISIBLE);
holder.titleText.setText(getTitle(cont_Name));
keyWord = getTitle(cont_Name);
}else{
holder.titleText.setVisibility(View.GONE);
}
holder.contactTitle.setText(mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
holder.contactDetail.setText(mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
}
}
然而,我的layout
是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/section_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textStyle="bold"
android:background="@android:color/holo_blue_bright"
style="?android:attr/listSeparatorTextViewStyle" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/section_title"
android:contentDescription="@string/contact_img" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1"
android:layout_marginLeft="17dp"
android:layout_below="@+id/section_title"
android:layout_toRightOf="@+id/imageView1"
android:text="@+id/textView1"
android:textColor="@color/Black"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginLeft="17dp"
android:layout_toRightOf="@+id/imageView1"
android:paddingBottom="8dp"
android:text="@+id/textView2" />
</RelativeLayout>
答案 0 :(得分:0)
最后两周后得到了我自己的答案......我的if-else
条件过于复杂,所以我根据一些不同的标准进行比较。
if (mCursor.getPosition() > 0 && mCursor.moveToPrevious())
{
preName = mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
mCursor.moveToNext();
}
else if(mCursor.getPosition() == 0)
{
preName = null;
}
else{
preName = null;
}
if(preName != null){
preTitle = getTitle(preName);
}
//===============================================================================
/*
* Setting Header And Contact Details
*/
//===============================================================================
if(mCursor.isFirst()){
holder.titleText.setVisibility(View.VISIBLE);
holder.titleText.setText(itemTitle);
}
else if(preName != null){
if(! itemTitle.equalsIgnoreCase(preTitle))
{
holder.titleText.setVisibility(View.VISIBLE);
holder.titleText.setText(itemTitle);
}else{
holder.titleText.setVisibility(View.GONE);
}
}
holder.contactTitle.setText(mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)));
holder.contactDetail.setText(mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));