Alphabet Indexer中的Listview标题显示错误

时间:2014-05-04 16:48:20

标签: android listview indexer

我正在尝试使用字母索引来实现Listview。我成功地显示了它,但问题是Listview中的文本进入字母表而不是水平显示。请帮忙 。歌曲名称进入了字母索引器。粘贴在xml文件和代码下面:

列出项目xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="songPicked"
android:orientation="vertical"
>

    <LinearLayout
    android:id="@+id/sort_key_layout"
    android:layout_width="fill_parent"
    android:layout_height="18dip"
    android:background="#EAEAEA" >

    <TextView
        android:id="@+id/sort_key"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dip"
        android:textColor="#000000"
        android:textSize="13sp" />
</LinearLayout>

<TextView
    android:id="@+id/tv_song_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
     android:textColor="#000000"
    android:textSize="20sp"
    android:textStyle="bold" />

     </LinearLayout>

Listview xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/title_layout"
    android:layout_width="fill_parent"
    android:layout_height="18dip"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="5dp"
    android:background="#EAEAEA" >

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dip"
        android:textColor="#000000"
        android:textSize="13sp" />
</FrameLayout>

<LinearLayout
    android:id="@+id/indexer_layout"
    android:layout_width="wrap_content"
   android:layout_height="match_parent"
     android:layout_alignParentRight="true"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    android:gravity="center"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lv_song_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@null"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
       >
    </ListView>
  </LinearLayout>
   </RelativeLayout>

Adapterclass:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    Log.d(this.getClass().getName(), "inside getview");
    if(convertView == null) {
        Log.d(this.getClass().getName(), "inside if getview & position is"+position);
        convertView = songInf.inflate(R.layout.song, null);
        holder = new ViewHolder();
        holder.sortKeyLayout = (LinearLayout) convertView.findViewById(R.id.sort_key_layout);
        holder.sortKey = (TextView) convertView.findViewById(R.id.sort_key);
        holder.songName = (TextView) convertView.findViewById(R.id.tv_song_title);
        holder.songArtist = (TextView)convertView.findViewById(R.id.tv_song_artist);
        convertView.setTag(holder);
    } else {
        Log.d(this.getClass().getName(), "inside else getview & position is"+position);
        holder = (ViewHolder) convertView.getTag();
    }

    Song currSong = songs.get(position);
    holder.songName.setText(currSong.getTitle());
    holder.songArtist.setText(currSong.getArtist());
    int section = mIndexer.getSectionForPosition(position);
    if (position == mIndexer.getPositionForSection(section)) {

        holder.sortKey.setText(currSong.getSortKey());
        holder.sortKeyLayout.setVisibility(View.VISIBLE);
    } else {
        holder.sortKeyLayout.setVisibility(View.GONE);
    }
    return convertView;}![enter image description here][1]

1 个答案:

答案 0 :(得分:0)

我想问题是您使用Listview xml的布局relative layout它们显然会相互重叠,直到或除非您将它们相对于彼此放置。

您必须从lv_song_list

中取出indexer_layout

您可以参考

<FrameLayout
    android:id="@+id/title_layout"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="5dp"
    android:background="#a86bf0"
    android:visibility="gone" >

    <TextView
        android:id="@+id/title_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dip"
        android:textColor="#000000"
        android:textSize="13sp" />
</FrameLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:weightSum="10" >

    <ListView
        android:id="@+id/indexed_listView"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="9"
        android:cacheColorHint="@null"
        android:paddingLeft="5dp"
        android:paddingRight="5dp" />

    <LinearLayout
        android:id="@+id/indexer_layout"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" />
</LinearLayout>

 <RelativeLayout
    android:id="@+id/section_toast_layout"
    android:layout_width="50dip"
    android:layout_height="50dip"
    android:layout_centerInParent="true"
    android:background="@drawable/black_base_rounded_transparent_50_rad2"
    android:visibility="gone" >

    <TextView
        android:id="@+id/section_toast_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        style="@style/text_white_medium_franklin_bkcd"
        android:textSize="30sp" />
</RelativeLayout>