我有customrecycleAdapter,当我点击元素时我会改变行的颜色,但如果我滚动视图背景更改位置或出现在其他元素上。 我已经阅读了有关Receycle的观点,但是,这个问题的工作原理是什么?
我的适配器是:
public class CustomRecycleAdapter extends RecyclerView.Adapter<CustomRecycleAdapter.ViewHolder> {
private JSONArray dataSource;
private AdapterCallback mAdapterCallback;
public static SparseBooleanArray selectedItems;
public CustomRecycleAdapter(JSONArray dataArgs, Context context){
dataSource = dataArgs;
this.mAdapterCallback = ((AdapterCallback) context);
}
public static interface AdapterCallback {
void onMethodCallback(View caller, JSONObject obj, JSONArray data, int position);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_view_activities, parent, false);
ViewHolder viewHolder = new ViewHolder(view, new CustomRecycleAdapter.ViewHolder.IMyViewHolderClicks() {
public void onClick(View caller, int position) {
try {
JSONObject itemClicked = dataSource.getJSONObject(position);
mAdapterCallback.onMethodCallback(caller, itemClicked, dataSource, position);
// Log.e("clicked", itemClicked.toString());
} catch (JSONException e) {
e.printStackTrace();
}
};
});
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
try {
JSONObject object = dataSource.getJSONObject(position);
holder.textView.setText(object.getString("description"));
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public int getItemCount() {
return dataSource.length();
}
public JSONObject getItem(int position) throws JSONException {
return dataSource.getJSONObject(position);
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public IMyViewHolderClicks mListener;
public TextView textView;
public TextView hours;
public ViewHolder(View itemView, IMyViewHolderClicks listener) {
super(itemView);
mListener = listener;
textView = (TextView) itemView.findViewById(R.id.activity);
hours = (TextView) itemView.findViewById(R.id.hours);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int position = getAdapterPosition();
Log.e("RecycleAdapterPosition", String.valueOf(getAdapterPosition()));
// mListener.onClick(v, position);
v.setBackgroundColor(Color.GREEN);
v.setSelected(true);
}
public interface IMyViewHolderClicks {
public void onClick(View caller, int position);
}
}
}
所以我点击了元素,并且正确地设置了背景,但如果我滚动,我会看到另一个元素背景设置,如果我向上滚动我的元素,我选择了,没有背景。
答案 0 :(得分:0)
RecyclerView重复使用视图来绘制列表中将要显示的下一个元素,因此视图集包含新数据,但继续使用旧样式,因为您会看到列表中其他元素的新颜色。
你应该创建一个数组存储是否检查了这个元素,并检查是否在调用var numberOfRows = 0
var snusBrandsArray = [String]()
override func viewDidLoad() {
super.viewDidLoad()
parseJSON()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func parseJSON(){
let path: String = NSBundle.mainBundle().pathForResource("snuses", ofType: "json") as String!
let jsonData = NSData(contentsOfFile: path) as NSData!
let readableJSON = JSON(data: jsonData, options: NSJSONReadingOptions.MutableContainers, error: nil)
var brands = readableJSON["Snuses", "Brands"]
NSLog("\(brands)")
numberOfRows = readableJSON["Snuses"].count
for i in 1...numberOfRows{
var brands = "Snuses"
brands += "\(i)"
var name = readableJSON["Snuses", "Brands"].string as String!
snusBrandsArray.append(name)
}
}
时检查了
onBindViewHolder()