我通过实现名称的自定义列表视图来制作Android应用程序。我希望列表以字典顺序显示,并且在名字中第一个字符的更改时,相应的字符应该在以该字符开头的名称开始之前显示。但这没有正确发生。我无法理解原因。 其次,无论生成什么列表,在快速滚动时,那些第一个字母也会消失,而我还是带有排序列表,但没有第一个字母标记。 请帮助..提前致谢。 花絮: Before scrolling After scrolling multiple times
row_item_speaker.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_first_alphabet"
android:paddingTop="10dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="A"
android:textStyle="bold"
android:textColor="@color/colorPrimary"/>
<View
android:id="@+id/horizontalLine"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorPrimary"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<com.pkmmte.view.CircularImageView
android:id="@+id/civ_speaker_photo"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/ic_speakers"
app:border="true"
app:border_color="#EEEEEE"
app:border_width="2dp"
android:paddingLeft="5dp"
android:paddingStart="5dp"
app:shadow="true" />
<TextView
android:id="@+id/tv_speaker_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/heading_size"
android:textColor="#000"
android:textStyle="bold"
android:paddingLeft="10dp"
android:paddingStart="10dp"
android:layout_toEndOf="@id/civ_speaker_photo"
android:layout_toRightOf="@id/civ_speaker_photo"
android:layout_alignTop="@id/civ_user_pic"
android:paddingTop="8dp"/>
<TextView
android:id="@+id/tv_speaker_position"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/civ_speaker_photo"
android:layout_toEndOf="@id/civ_speaker_photo"
android:layout_below="@id/tv_speaker_name"
android:textSize="@dimen/subheading_size"
android:textColor="#777"
android:ellipsize="end"
android:singleLine="true"
android:paddingLeft="10dp"
android:paddingStart="10dp"
/>
</RelativeLayout>
</LinearLayout>
SpeakerAdapter.java
package com.test.rajat.a10times.Adapter;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.pkmmte.view.CircularImageView;
import com.squareup.picasso.Picasso;
import com.test.rajat.a10times.Model.SpeakerModel;
import com.test.rajat.a10times.R;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
/**
* Created by Rajat on 1/20/2016.
*/
public class SpeakersAdapter extends BaseAdapter {
private Context context;
private ArrayList<SpeakerModel> speakersList;
public SpeakersAdapter(Context context, ArrayList<SpeakerModel> speakersList) {
this.context = context;
this.speakersList = speakersList;
sortedList();
}
private ArrayList<SpeakerModel> sortedList(){
Collections.sort(speakersList, new Comparator<SpeakerModel>() {
@Override
public int compare(SpeakerModel s1, SpeakerModel s2) {
return s1.getName().compareToIgnoreCase(s2.getName());
}
});
return speakersList;
}
@Override
public int getCount() {
return speakersList.size();
}
@Override
public Object getItem(int position) {
return speakersList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.row_item_speaker,null);
}
TextView name=(TextView) convertView.findViewById(R.id.tv_speaker_name);
TextView speaker_position=(TextView) convertView.findViewById(R.id.tv_speaker_position);
TextView first_alphabet=(TextView) convertView.findViewById(R.id.tv_first_alphabet);
View horizontalLine= convertView.findViewById(R.id.horizontalLine);
if(setFirstAlphabet(position)){
first_alphabet.setText((speakersList.get(position).getName().subSequence(0, 1).toString()));
}
else{
first_alphabet.setVisibility(View.GONE);
horizontalLine.setBackgroundColor(Color.parseColor("#d3d3d3"));
}
CircularImageView image=(CircularImageView) convertView.findViewById(R.id.civ_speaker_photo);
Picasso.with(context).load(speakersList.get(position).getPic_url()).placeholder(R.drawable.ic_person).error(R.drawable.ic_person).into(image);
name.setText(speakersList.get(position).getName());
speaker_position.setText(speakersList.get(position).getPosition());
return convertView;
}
private boolean setFirstAlphabet(int position){
String currentFirstAlphabet=speakersList.get(position).getName().subSequence(0,1).toString();
String nextFirstAlphabet=speakersList.get(position+1).getName().subSequence(0,1).toString();
if(position<speakersList.size()-1) {
if ((currentFirstAlphabet.compareToIgnoreCase(nextFirstAlphabet) == 0)) {
Log.d("TAG3",speakersList.get(position).getName().subSequence(0,1).toString()+speakersList.get(position+1).getName().subSequence(0,1).toString());
return false;
}
}
return true;
}
}