根据我正在处理的应用程序,我需要创建一个列表视图,然后在用户单击每个项目时处理事件。除了列表视图中的第一项外,一切正常。第一次加载列表视图时它不可点击,在我向上或向下滚动列表视图后它可以正常工作。还有一件事,第一次加载列表视图如果我单击第一个项目,然后单击第二个项目,则 toast 消息首先显示第二个项目的名称,然后显示第一个项目的名称。因此,在滚动或单击列表视图之前,列表视图中的第一项是不活动的。这是我的这个片段的代码。知道如何处理吗?
public EyeDiseasesFragment(File diseasesJsonFile) {
this.diseasesJsonFile = diseasesJsonFile;
mainUrl = Config.mainUrl;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
diseasesList = new ArrayList<Diseases>();
new LoadContent().execute();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
listener = (MainFragmentController) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnArticleSelectedListener");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.diseases, container, false);
mList = (ListView) view.findViewById(R.id.diseases);
return view;
}
class DiseasesAdapter extends BaseAdapter {
LayoutInflater inflater;
public DiseasesAdapter() {
inflater = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return diseasesList.size();
}
@Override
public Diseases getItem(int arg0) {
return diseasesList.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
final Diseases item;
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.diseases_item, null);
holder.title = (TextView) convertView.findViewById(R.id.disease_item_header);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
item = getItem(position);
holder.title.setText(item.diseaseName);
convertView.setLayoutParams(new ListView.LayoutParams(
LayoutParams.MATCH_PARENT, HelperStaticMethods.getDIP(
getActivity(), 50)));
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
listener.onDiseasItemClick(item);
}
});
return convertView;
}
}
class ViewHolder {
TextView title;
}
class LoadContent extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
setDiseasesInformation(getActivity());
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
mList.setCacheColorHint(Color.TRANSPARENT);
mList.setDivider(null);
mList.setVerticalFadingEdgeEnabled(false);
DiseasesAdapter adapter = new DiseasesAdapter();
mList.setAdapter(adapter);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
}
编辑
将下面的代码移动到 convertView 为 Null 的语句中解决了这个问题。
if (convertView == null) {
// ... after inflating the view
convertView.setLayoutParams(new ListView.LayoutParams(
LayoutParams.MATCH_PARENT, HelperStaticMethods.getDIP(getActivity(), 50)));
}