我在网上搜索过,我的 ListView 需要一个自定义适配器来解决滚动和过滤问题。我正在使用 fragment ,因为我的主要 Activity 有一些选项卡。
我尝试实现我发现的代码的不同变体,但无法使其工作。
public class Symptoms extends Fragment {
String filename = "symptoms.txt";
private static final String TAG = "SymptomsFragment";
ReadFile rf = new ReadFile();
String[] lines;
ListView simpleList;
Context context;
private EditText filterText;
private ArrayAdapter<String> arrayAdapter;
ArrayList<String> listItems = new ArrayList<String>();
int count = 0;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View mView = inflater.inflate(R.layout.symptoms_layout,container,false);
simpleList = (ListView) mView.findViewById(R.id.list);
filterText = (EditText) mView.findViewById(R.id.editText);
try {
lines = rf.readLines(filename, getContext());
arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, lines);
simpleList.setAdapter(arrayAdapter);
}
catch(IOException e) {
// Print out the exception that occurred
Toast.makeText(getContext(), "Unable to create "+filename+": "+e.getMessage(), Toast.LENGTH_LONG).show();
}
filterText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
arrayAdapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) { }
});
simpleList.setCacheColorHint(Color.TRANSPARENT);
simpleList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
simpleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView tv = (TextView)view.findViewById(android.R.id.text1);
String selectedSymptom = tv.getText().toString();
if(!listItems.contains(selectedSymptom)) {
tv.setTextColor(Color.WHITE);
tv.setBackgroundColor(Color.CYAN);
listItems.add(selectedSymptom);
}else{
tv.setTextColor(Color.BLACK);
tv.setBackgroundColor(Color.TRANSPARENT);
listItems.remove(selectedSymptom);
}
System.out.println(listItems);
}
});
return mView;
}
}
我正在读取一个文本文件并将每一行保存到一个 arraylist 元素中,并且我正在为我的 ListView 使用 ArrayAdapter。我正在更改所选项目的背景和文本颜色。但是,当我使用“EditText”在 ListView 上搜索或滚动时,突出显示的元素会被打乱(仅在视觉上)。我在网上找到了许多解决方案,帮助那些在 ListView 中的复选框遇到同样问题的人,但我在实现这些解决方案时遇到了很大的困难。在这种情况下,自定义 ListView 适配器应该是什么样子?提前致谢!
最佳答案
尝试改变:
arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, lines);
致:
arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, lines){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv1 = (TextView)super.getView(position, convertView, parent);
String selectedSymptom = getItem(position);
if(listItems.contains(selectedSymptom)) {
tv1.setTextColor(Color.WHITE);
tv1.setBackgroundColor(Color.CYAN);
}else{
tv1.setTextColor(Color.BLACK);
tv1.setBackgroundColor(Color.TRANSPARENT);
}
return tv1;
}
};
希望有帮助!
关于java - 安卓 Java : Custom adapter for listview for scrolling and filtering issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57623980/