关闭。这个问题需要详细或明确。它目前不接受答案。
**想改进这个问题吗?**编辑这篇文章,添加细节并澄清问题。
两天前关门了。
改进这个问题
我想在显示textview时将IsItemViewSwipEnabled设置为true,在显示复选框时将其设置为false。我将SwipEnabled作为参数传递,并设置IsItemViewSwipEnabled return SwipEnabled,当我启动活动调用show_road(false)时,我滑动项目并IsItemViewSwipEnabled return false false false…,滑动项目不起作用,然后我单击按钮a调用show_road(true)并滑动项目IsItemViewSwipEnabled return false false。。。,滑动项目工作成功,然后我单击按钮b再次调用show_road(flase),IsItemViewSwipEnabled调用false true false…,我滑动项目,它工作成功。怎样才能做到这一点?
activity.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_road);
tv_all_road = findViewById(R.id.tv_all_road);
tv_selected_road = findViewById(R.id.tv_selected_raod);
tv_add_to_selected_road = findViewById(R.id.tv_add_to_selected_road);
edt_search = findViewById(R.id.edt_search);
imgv_clear = findViewById(R.id.imgv_clear);
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setNavigationOnClickListener(v -> this.finish());
show_road(0);
tv_all_road.setOnClickListener(v -> show_road(0));
tv_selected_road.setOnClickListener(v -> show_road(1));
tv_add_to_selected_road.setOnClickListener(v -> {
AlertDialog.Builder builder = new AlertDialog.Builder(SetRoadActivity.this);
builder.setTitle("提示");
builder.setMessage("確定新增路段");
builder.setCancelable(false);
builder.setNegativeButton("否", null);
builder.setPositiveButton("是", (dialog, which) -> add_road());
AlertDialog alertDialog = builder.create();
alertDialog.show();
});
edt_search.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) {
myAdapter.getFilter().filter(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
imgv_clear.setOnClickListener(v -> edt_search.setText(""));
}
private void show_road(int view_type){
DBUtil dbUtil = new DBUtil(this);
mData.clear();
String sqlCommand = "";
if(view_type == 0){
tv_all_road.setBackground(getDrawable(R.drawable.edittext_set_road_underline));
tv_selected_road.setBackground(null);
tv_add_to_selected_road.setVisibility(View.VISIBLE);
sqlCommand = "SELECT * FROM tb_all_road WHERE addr_c NOT IN (SELECT addr_c FROM tb_selected_Road)";
}
if(view_type == 1){
tv_all_road.setBackground(null);
tv_selected_road.setBackground(getDrawable(R.drawable.edittext_set_road_underline));
tv_add_to_selected_road.setVisibility(View.GONE);
sqlCommand = "SELECT * FROM tb_selected_road";
}
if(dbUtil.is_sdcard_exists()){
cursor = dbUtil.get_db_main_sdcard().rawQuery(sqlCommand, null);
}else {
cursor = dbUtil.get_db_main_local().rawQuery(sqlCommand, null);
}
if (cursor.moveToFirst()){
do {
mData.add(cursor.getString(0) + ", " + cursor.getString(1));
}while (cursor.moveToNext());
}
recyclerView = findViewById(R.id.recyclerview_road);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
myAdapter = new MyAdapter(mData, view_type);
recyclerView.setAdapter(myAdapter);
ItemTouchHelper helper;
helper = new ItemTouchHelper(new MyItemTouchCallBack(myAdapter, (view_type == 0)?false:true));
helper.attachToRecyclerView(recyclerView);
}
myadapter.java:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> mData;
private List<String> mFilterList;
private List<String> mCheckList = new ArrayList<>();
private boolean[] flag;
private int mViewType;
public MyAdapter(List<String> data, int viewType){
mData = data;
mFilterList = data;
mViewType = viewType;
flag = new boolean[getItemCount()];
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.road_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if(mViewType == 0){
holder.checkBox.setText(mFilterList.get(position));
holder.checkBox.setOnCheckedChangeListener(null);
holder.checkBox.setChecked(flag[position]);
holder.checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> flag[position] = isChecked);
}else if(mViewType == 1){
holder.textView.setText(mFilterList.get(position));
}
}
public int getItemCount(){
return mFilterList.size();
}
public List<String> getmCheckList(){
for(int i = 0; i < getItemCount(); i ++){
if(flag[i]){
mCheckList.add(mData.get(i));
}
}
Log.d("mCheckListSize", mCheckList.size() + "");
return mCheckList;
}
public List<String> getmFilterList(){
return mFilterList;
}
public Filter getFilter(){
return new Filter(){
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if(charString.isEmpty()){
mFilterList = mData;
}else {
List<String> filterList = new ArrayList<>();
for(String str : mData){
if(str.contains(charString)){
filterList.add(str);
}
}
mFilterList = filterList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = mFilterList;
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mFilterList = (ArrayList<String>) results.values;
notifyDataSetChanged();
}
};
}
class ViewHolder extends RecyclerView.ViewHolder{
private TextView textView;
private CheckBox checkBox;
ViewHolder(View view){
super(view);
textView = view.findViewById(R.id.tv_road_item);
checkBox = view.findViewById(R.id.checkbox_road_item);
if(mViewType == 0){
textView.setVisibility(View.GONE);
checkBox.setVisibility(View.VISIBLE);
}
if(mViewType == 1){
textView.setVisibility(View.VISIBLE);
checkBox.setVisibility(View.GONE);
}
}
}
}
myitemtouchcallback.java:
public class MyItemTouchCallBack extends ItemTouchHelper.Callback {
private final MyAdapter myAdapter;
private final boolean swipeEnabled;
public MyItemTouchCallBack(MyAdapter adapter, Boolean swipeEnabled){
myAdapter = adapter;
this.swipeEnabled = swipeEnabled;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
return makeMovementFlags(0, ItemTouchHelper.START|ItemTouchHelper.END);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
if((direction == ItemTouchHelper.END||direction == ItemTouchHelper.START)){
int position = viewHolder.getAdapterPosition();
myAdapter.getmFilterList().remove(position);
myAdapter.notifyItemRemoved(position);
}
}
@Override
public boolean isItemViewSwipeEnabled() {
System.out.println(swipeEnabled);
return swipeEnabled;
}
@Override
public boolean isLongPressDragEnabled() {
return false;
}
}
road_item.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_road_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:textAlignment="center"
android:textSize="22sp" />
<CheckBox
android:id="@+id/checkbox_road_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:textSize="22sp" />
暂无答案!
目前还没有任何答案,快来回答吧!