[英]Remove element from arraylist on checking and then unchecking the checkbox of checkbox listview
[英]checking or unchecking a checkbox to move items from listview to the top or bottom
我有一個列表視圖,其中每個項目旁邊都包含一個復選框。 我想通過選中或取消選中項目將其移動到列表的頂部或底部。 如果我選中該項目,請移至頂部,如果我取消選中,請移至底部。
我試圖實現不同的代碼集,包括setSelection(int position)
。 我一直關注適配器上的if和else語句,因為它們與您選中和取消選中復選框時發生的情況有關。 當我嘗試實現setSelection(int position)
方法時,出現錯誤消息。 有沒有辦法修復代碼,以便setSelection(int position)
與我的代碼兼容?
MainActivity.java
public class MainActivity extends AppCompatActivity {
ArrayList<String> selectedItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
selectedItems = new ArrayList<String>();
ListView chl = (ListView) findViewById(R.id.cityselector);
chl.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
String[] items = {"Berlin", "New York", "Paris", "Mexico City", "Asana", "Tangier"};
Adapter adapter=new Adapter(this,items);
chl.setAdapter(adapter);
chl.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
int item_position=i;
}
});
}
Adapter.java
public class Adapter extends BaseAdapter {
String[] items;
LayoutInflater inflater;
ArrayList selectedItems;
Activity activity;
Adapter(Activity activity, String[] items){
this.items=items;
inflater=activity.getLayoutInflater();
selectedItems = new ArrayList<String>();
this.activity=activity;
}
@Override
public int getCount() {
return items.length;
}
@Override
public String getItem(int i) {
return items[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
View v = view;
if(v == null){
v = inflater.inflate(R.layout.checkboxlayout,null);
}
TextView textview_Cities=(TextView)v.findViewById(R.id.Textview_Cities);
textview_Cities.setText(items[i]);
final CheckedTextView checkedTextView=(CheckedTextView)v.findViewById(R.id.checkbox);
checkedTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!checkedTextView.isChecked()){
checkedTextView.setChecked(true);
selectedItems.add(items[i]);
//This is where I put the setSelection(int position) method////
}
else {
checkedTextView.setChecked(false);
selectedItems.remove(items[i]);
}
}
});
return v;
}
}
你可以試試看
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
int finalPosition = isChecked ? 0 : list.size() - 1;
int currentPosition = getAdapterPosition();
moveItem(currentPosition, finalPosition);
}
});
void moveItem(int currentPosition, int finalPosition) {
if (currentPosition < finalPosition) {
for (int i = currentPosition; i < finalPosition; i++) {
Collections.swap(list, i, i + 1);
}
} else {
for (int i = currentPosition; i > finalPosition; i--) {
Collections.swap(list, i, i - 1);
}
}
notifyItemMoved(currentPosition, finalPosition);
}
這里的OnCheckedChangeListener
是內部ViewHolder
中的RecyclerView
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.