gpt4 book ai didi

java - 在平板电脑上滚动列表后意外重用了选定的列表项背景颜色

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:43:25 26 4
gpt4 key购买 nike

对于我在平板电脑上的 ListView ,我试图让我选择的列表项选择在选择时保持其状态,但不幸的是我看到了一些奇怪的行为。出于某种原因,每当我将列表滚动到所选项目不可见的位置,然后滚动回到所选项目可见的位置时,背景颜色意外地被重新使用。我相信 getView 方法中需要包含某些内容,但我不确定如何使用此方法。为了防止背景色被重复使用,必须做什么?

适配器类

public class VictoriaListAdapter extends BaseAdapter {

private List<Victoria> mData;
private LayoutInflater mInflater;

public VictoriaListAdapter (List<Victoria> data, Context context) {
mData = data;
mData = new ArrayList(mData);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return mData.size();
}

@Override
public String getItem(int position) {
return mData.get(position).getStation();
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_dualline, parent, false);
holder = new ViewHolder();

holder.title = (TextView) convertView.findViewById(R.id.item_station);
holder.description = (TextView) convertView.findViewById(R.id.item_zone);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.title.setText(mData.get(position).getStation());
holder.description.setText(mData.get(position).getZone());

return convertView;
}

/**
* View holder
*/
static class ViewHolder {
private TextView title;
private TextView description;
}
}

fragment 类

public class FragmentVictoriaLine extends ListFragment {

private VictoriaListAdapter mAdapter;

public FragmentVictoriaLine() {
}

/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
public boolean mTwoPane;

public static FragmentVictoriaLine newInstance() {
return new FragmentVictoriaLine();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_victoria_line, container, false);
initialize();
return view;
}

List<Victoria> list = new ArrayList<>();
private void initialize() {
String[] items = getActivity().getResources().getStringArray(R.array.victoria_stations);
String[] itemDescriptions = getActivity().getResources().getStringArray(R.array.victoria_zones);
for (int n = 0; n < items.length; n++){
Victoria victoria = new Victoria();
victoria.setID();
victoria.setStation(items[n]);
victoria.setZone(itemDescriptions[n]);
list.add(victoria);
}

mAdapter = new VictoriaListAdapter(list, getActivity());
setListAdapter(mAdapter);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
View v = getView();

mTwoPane = getActivity().findViewById(R.id.detail_container) != null;

assert v != null;
ListView lv = (ListView)v.findViewById(android.R.id.list);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
private Victoria selectedMain;
private View selectedView;

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
VictoriaListAdapter adapter = (VictoriaListAdapter) parent.getAdapter();
String station = adapter.getItem(position);

if (mTwoPane) {
setItemNormal();
View rowView = view;
setItemSelected(rowView);

Fragment newFragment;
if (station.equals(view.getResources().getString(R.string.bho))) {
newFragment = new FragmentVictoriaBHO();
} else if (station.equals(view.getResources().getString(R.string.brx))) {
newFragment = new FragmentVictoriaBRX();
} else if (station.equals(view.getResources().getString(R.string.eus))) {
newFragment = new FragmentVictoriaEUS();
} else if (station.equals(view.getResources().getString(R.string.fpk))) {
newFragment = new FragmentVictoriaFPK();
} else if (station.equals(view.getResources().getString(R.string.green_park))) {
newFragment = new FragmentVictoriaGreenPark();
} else if (station.equals(view.getResources().getString(R.string.hhy))) {
newFragment = new FragmentVictoriaHHY();
} else if (station.equals(view.getResources().getString(R.string.kxsp))) {
newFragment = new FragmentVictoriaKXSP();
} else {
newFragment = new FragmentVictoriaBHO();
}
VictoriaLineActivity activity = (VictoriaLineActivity) view.getContext();
FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.fade_out, R.anim.fade_in);
transaction.replace(R.id.detail_container, newFragment);
transaction.commit();
} else {
Intent intent;
if (station.equals(view.getResources().getString(R.string.bho))) {
intent = new Intent(getActivity(), VictoriaBHOActivity.class);
} else if (station.equals(view.getResources().getString(R.string.brx))) {
intent = new Intent(getActivity(), VictoriaBRXActivity.class);
} else if (station.equals(view.getResources().getString(R.string.eus))) {
intent = new Intent(getActivity(), VictoriaEUSActivity.class);
} else if (station.equals(view.getResources().getString(R.string.fpk))) {
intent = new Intent(getActivity(), VictoriaFPKActivity.class);
} else if (station.equals(view.getResources().getString(R.string.green_park))) {
intent = new Intent(getActivity(), VictoriaGreenParkActivity.class);
} else if (station.equals(view.getResources().getString(R.string.hhy))) {
intent = new Intent(getActivity(), VictoriaHHYActivity.class);
} else if (station.equals(view.getResources().getString(R.string.kxsp))) {
intent = new Intent(getActivity(), VictoriaKXSPActivity.class);
} else {
intent = new Intent(getActivity(), VictoriaBHOActivity.class);
}
startActivity(intent);
}
}

public void setItemSelected(View view) {
View rowView = view;
view.setBackgroundColor(Color.parseColor("#868F98"));

TextView tv0 = (TextView) rowView.findViewById(R.id.item_station);
tv0.setTextColor(Color.WHITE);

TextView tv1 = (TextView) rowView.findViewById(R.id.item_zone);
tv1.setTextColor(Color.WHITE);
}

public void setItemNormal() {
for (int i = 0; i < getListView().getChildCount(); i++) {
View v = getListView().getChildAt(i);
v.setBackgroundColor(Color.TRANSPARENT);

TextView tv0 = ((TextView) v.findViewById(R.id.item_station));
tv0.setTextColor(Color.WHITE);

TextView tv1 = ((TextView) v.findViewById(R.id.item_zone));
tv1.setTextColor(Color.parseColor("#B5B5B5"));
}
}
});

super.onActivityCreated(savedInstanceState);
}
}

数据类

public class Victoria {
public Victoria(){}

private String station;
private String zone;
private boolean selected;

public String getStation(){
return station;
}

public void setStation(String item){
this.station = item;
}

public String getZone(){
return zone;
}

public void setZone(String zone){
this.zone = zone;
}

private int _id;
public void getID(int _id){
this._id = _id;
}

public int setID(){
return _id;
}

public boolean isSelected() {
return selected;
}

public void setSelected(boolean selected) {
this.selected = selected;
}
}

最佳答案

当您滚动列表时,ListView 中的项/ View 将被重用以优化内存。因此,当您将选定状态设置为列表项时,您将看到该选定状态滚动浏览时在多个列表项中。防止这种情况的最佳方法是在您的数据模型中保留状态并在适配器getView函数中设置状态。

这是你可以做的 -

 @Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
VictoriaListAdapter adapter = (VictoriaListAdapter) parent.getAdapter();

//reverse the selected state in data model
for (int i = 0; i < adapter.getCount(); i++) {
Victoria victoria = (Victoria)adapter.getItem(i);
victoria.setSelected(i == position ? true : false);
}
Victoria victoria = (Victoria)adapter.getItem(position);

---
---

在适配器中 -

@Override
public Object getItem(int position) {
//Return full object, coz we need to access other
//member variables too
return mData.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;

if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_dualline, parent, false);
holder = new ViewHolder();

holder.title = (TextView) convertView.findViewById(R.id.item_station);
holder.description = (TextView) convertView.findViewById(R.id.item_zone);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

Victoria victoria = (Victoria)getItem(position);
holder.title.setText(victoria.getStation());
holder.description.setText(victoria.getZone());
if (victoria.isSelected()) {
setItemSelected(convertView);
} else {
setItemNormal(convertView);
}

return convertView;
}

public void setItemSelected(View view) {
View rowView = view;
view.setBackgroundColor(Color.parseColor("#868F98"));

TextView tv0 = (TextView) rowView.findViewById(R.id.item_station);
tv0.setTextColor(Color.WHITE);

TextView tv1 = (TextView) rowView.findViewById(R.id.item_zone);
tv1.setTextColor(Color.WHITE);
}

public void setItemNormal(View v) {

v.setBackgroundColor(Color.TRANSPARENT);

TextView tv0 = ((TextView) v.findViewById(R.id.item_station));
tv0.setTextColor(Color.WHITE);

TextView tv1 = ((TextView) v.findViewById(R.id.item_zone));
tv1.setTextColor(Color.parseColor("#B5B5B5"));

}

希望对您有所帮助!

关于java - 在平板电脑上滚动列表后意外重用了选定的列表项背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32724378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com