我有一个从字符串数组填充的碎片列表视图。我所追求的是当我点击选定的行时,它会变成绿色并保持绿色。只要您不滚动,就可以使用我的作品。只要您滚动尚未选择的行,背景就会发生变化。据我了解,这是由于列表视图回收其视图的方式。我花了很多时间尝试不同的方法来实施以阻止这种情况的发生,但我缺乏如何做到这一点的知识。
public class AreaListFragment extends ListFragment {
private static final String STATE_ACTIVATED_POSITION = "activated_position";
private Callbacks mCallbacks = sDummyCallbacks;
private CentreDataContent mContent;
private int mActivatedPosition = ListView.INVALID_POSITION;
public interface Callbacks {
public void onItemSelected(String id);
}
private static Callbacks sDummyCallbacks = new Callbacks() {
@Override
public void onItemSelected(String id) {
}
};
public AreaListFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mContent == null) {
Resources res = getResources();
mContent = new CentreDataContent(R.integer.UnknownID,
res.getStringArray(R.array.undefinedArea),
res.getStringArray(R.array.undefinedChecklist));
setListAdapter(new ArrayAdapter<CentreDataContent.CentreArea>(
getActivity(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
CentreDataContent.AREAS));
}
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Restore the previously serialized activated item position.
if (savedInstanceState != null
&& savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Activities containing this fragment must implement its callbacks.
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException("Activity must implement fragment's callbacks.");
}
mCallbacks = (Callbacks) activity;
}
@Override
public void onDetach() {
super.onDetach();
mCallbacks = sDummyCallbacks;
}
@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
super.onListItemClick(listView, view, position, id);
view.setBackgroundColor(Color.GREEN);
mCallbacks.onItemSelected(CentreDataContent.AREAS.get(position).id.toString());
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mActivatedPosition != ListView.INVALID_POSITION) {
// Serialize and persist the activated item position.
outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
}
}
/**
* Turns on activate-on-click mode. When this mode is on, list items will be
* given the 'activated' state when touched.
*/
public void setActivateOnItemClick(boolean activateOnItemClick) {
// When setting CHOICE_MODE_SINGLE, ListView will automatically
// give items the 'activated' state when touched.
getListView().setChoiceMode(activateOnItemClick
? ListView.CHOICE_MODE_SINGLE
: ListView.CHOICE_MODE_NONE);
}
private void setActivatedPosition(int position) {
if (position == ListView.INVALID_POSITION) {
getListView().setItemChecked(mActivatedPosition, false);
} else {
getListView().setItemChecked(position, true);
}
mActivatedPosition = position;
}
}
我对android很陌生,正在努力学习。所以这方面的任何帮助都会非常感谢