单击索引布局仅在列表滚动期间有效
Click on index layout works only during list scrolling
我创建了一个右侧有索引的列表,这要感谢 IndexableListView 个文件。这是我的 activity:
public class FindByname extends Activity {
private Database db = new Database(this);
private IndexableListView mListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.indexable);
SQLiteDatabase dbR = db.getReadableDatabase();
ArrayList<User> list = new ArrayList<User>();
list = createFullList(dbR);
ContentAdapter adapter = new ContentAdapter(this, R.layout.user_list_row, list);
mListView = (IndexableListView) findViewById(R.id.listview);
mListView.setAdapter(adapter);
mListView.setFastScrollEnabled(true);
}
private ArrayList<User> createFullList(SQLiteDatabase rw) {
ArrayList<User> list = new ArrayList<User>();
Cursor result = rw.rawQuery("SELECT _id, title, phone, city, category FROM users ORDER BY title ASC", null);
while (result.moveToNext()) {
list.add(new User(result.getString(0), result.getString(1), result.getString(2), result.getString(3), result.getString(4)));
}
result.close();
return list;
}
private class ContentAdapter extends MyAdapter implements SectionIndexer {
private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public ContentAdapter(Context context, int textViewResourceId, List<User> objects) {
super(context, textViewResourceId, objects);
}
@Override
public int getPositionForSection(int section) {
// If there is no item for current section, previous section will be selected
for (int i = section; i >= 0; i--) {
for (int j = 0; j < getCount(); j++) {
if (i == 0) {
// For numeric section
for (int k = 0; k <= 9; k++) {
if (StringMatcher.match(String.valueOf(getItem(j).getNome_user().charAt(0)), String.valueOf(k)))
return j;
}
} else {
if (StringMatcher.match(String.valueOf(getItem(j).getNome_user().charAt(0)), String.valueOf(mSections.charAt(i))))
return j;
}
}
}
return 0;
}
@Override
public int getSectionForPosition(int position) {
return 0;
}
@Override
public Object[] getSections() {
String[] sections = new String[mSections.length()];
for (int i = 0; i < mSections.length(); i++)
sections[i] = String.valueOf(mSections.charAt(i));
return sections;
}
}
}
我得到这样的结果:
问题是因为我的项目是可点击的,所以点击索引不起作用。索引上的字母只有在列表滚动时才可以点击!如果我在单击字母时停止滚动(在索引消失之前),它会触发对列表项的单击,而不是对索引字母的单击。
这里是 MyAdapter class 来处理列表项上的点击侦听器:
public class MyAdapter extends ArrayAdapter<User> {
private Context context;
private int layoutid;
private List<User> data;
public MyAdapter(Context context, int textViewResourceId, List<User> objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.layoutid = textViewResourceId;
data = objects;
}
public int getCount() {
return data.size();
}
@Override
public User getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutid, parent, false);
holder = new UserHolder();
holder.name = (TextView) row.findViewById(R.id.nameUser);
holder.city = (TextView) row.findViewById(R.id.cityUser);
holder.rating = (RatingBar) row.findViewById(R.id.ratingUser);
holder.distance = (TextView) row.findViewById(id.distanceKm);
holder.category = (TextView) row.findViewById(R.id.catUser);
row.setTag(holder);
} else {
holder = (UserHolder)row.getTag();
}
User user = data.get(position);
holder.name.setText(user.getNome_user());
holder.city.setText(user.getComune());
holder.rating.setRating(user.getVoto());
holder.distance.setText(user.getDistanza());
holder.category.setText(user.getCategory());
LayerDrawable stars = (LayerDrawable) holder.rating.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.rgb(255, 215, 0), android.graphics.PorterDuff.Mode.SRC_ATOP);
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
User merc = getItem(position);
Intent intent = new Intent(context, ViewUser.class);
intent.putExtra("id", merc.getId());
context.startActivity(intent);
}
});
return row;
}
static class UserHolder {
TextView name;
TextView city;
RatingBar rating;
TextView distance;
TextView category;
}
}
可能是什么问题?
对不起我的英语
这适用于 github 的 IndexableListViewActivity。
mListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
Toast.makeText(IndexableListViewActivity.this,
"position: " + position + "\n\n" + mItems.get(position), Toast.LENGTH_SHORT).show();
}});
对于您的 FindByName Activity 更像是:
mListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
User user = list.get(position);
Toast.makeText(FindByName.this,
"position: " + position + "\n\n" + user.getNome_user(), Toast.LENGTH_SHORT).show();
}});
我创建了一个右侧有索引的列表,这要感谢 IndexableListView 个文件。这是我的 activity:
public class FindByname extends Activity {
private Database db = new Database(this);
private IndexableListView mListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.indexable);
SQLiteDatabase dbR = db.getReadableDatabase();
ArrayList<User> list = new ArrayList<User>();
list = createFullList(dbR);
ContentAdapter adapter = new ContentAdapter(this, R.layout.user_list_row, list);
mListView = (IndexableListView) findViewById(R.id.listview);
mListView.setAdapter(adapter);
mListView.setFastScrollEnabled(true);
}
private ArrayList<User> createFullList(SQLiteDatabase rw) {
ArrayList<User> list = new ArrayList<User>();
Cursor result = rw.rawQuery("SELECT _id, title, phone, city, category FROM users ORDER BY title ASC", null);
while (result.moveToNext()) {
list.add(new User(result.getString(0), result.getString(1), result.getString(2), result.getString(3), result.getString(4)));
}
result.close();
return list;
}
private class ContentAdapter extends MyAdapter implements SectionIndexer {
private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public ContentAdapter(Context context, int textViewResourceId, List<User> objects) {
super(context, textViewResourceId, objects);
}
@Override
public int getPositionForSection(int section) {
// If there is no item for current section, previous section will be selected
for (int i = section; i >= 0; i--) {
for (int j = 0; j < getCount(); j++) {
if (i == 0) {
// For numeric section
for (int k = 0; k <= 9; k++) {
if (StringMatcher.match(String.valueOf(getItem(j).getNome_user().charAt(0)), String.valueOf(k)))
return j;
}
} else {
if (StringMatcher.match(String.valueOf(getItem(j).getNome_user().charAt(0)), String.valueOf(mSections.charAt(i))))
return j;
}
}
}
return 0;
}
@Override
public int getSectionForPosition(int position) {
return 0;
}
@Override
public Object[] getSections() {
String[] sections = new String[mSections.length()];
for (int i = 0; i < mSections.length(); i++)
sections[i] = String.valueOf(mSections.charAt(i));
return sections;
}
}
}
我得到这样的结果:
问题是因为我的项目是可点击的,所以点击索引不起作用。索引上的字母只有在列表滚动时才可以点击!如果我在单击字母时停止滚动(在索引消失之前),它会触发对列表项的单击,而不是对索引字母的单击。
这里是 MyAdapter class 来处理列表项上的点击侦听器:
public class MyAdapter extends ArrayAdapter<User> {
private Context context;
private int layoutid;
private List<User> data;
public MyAdapter(Context context, int textViewResourceId, List<User> objects) {
super(context, textViewResourceId, objects);
this.context = context;
this.layoutid = textViewResourceId;
data = objects;
}
public int getCount() {
return data.size();
}
@Override
public User getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutid, parent, false);
holder = new UserHolder();
holder.name = (TextView) row.findViewById(R.id.nameUser);
holder.city = (TextView) row.findViewById(R.id.cityUser);
holder.rating = (RatingBar) row.findViewById(R.id.ratingUser);
holder.distance = (TextView) row.findViewById(id.distanceKm);
holder.category = (TextView) row.findViewById(R.id.catUser);
row.setTag(holder);
} else {
holder = (UserHolder)row.getTag();
}
User user = data.get(position);
holder.name.setText(user.getNome_user());
holder.city.setText(user.getComune());
holder.rating.setRating(user.getVoto());
holder.distance.setText(user.getDistanza());
holder.category.setText(user.getCategory());
LayerDrawable stars = (LayerDrawable) holder.rating.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.rgb(255, 215, 0), android.graphics.PorterDuff.Mode.SRC_ATOP);
row.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
User merc = getItem(position);
Intent intent = new Intent(context, ViewUser.class);
intent.putExtra("id", merc.getId());
context.startActivity(intent);
}
});
return row;
}
static class UserHolder {
TextView name;
TextView city;
RatingBar rating;
TextView distance;
TextView category;
}
}
可能是什么问题?
对不起我的英语
这适用于 github 的 IndexableListViewActivity。
mListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
Toast.makeText(IndexableListViewActivity.this,
"position: " + position + "\n\n" + mItems.get(position), Toast.LENGTH_SHORT).show();
}});
对于您的 FindByName Activity 更像是:
mListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
User user = list.get(position);
Toast.makeText(FindByName.this,
"position: " + position + "\n\n" + user.getNome_user(), Toast.LENGTH_SHORT).show();
}});