过滤数据后如何重新绑定SwipeMenuListView onMenuItemClick项目?

时间:2018-08-27 16:33:39

标签: android listview

我有SwipeMenuListView的片段。数据来自API。我在片段中绑定SwipeMenuList的按钮并将对象发送到Adapter以获取 this

一切都很好

但是过滤列表视图按钮后坏了。当我在过滤后的列表视图中按“个人资料”按钮时,应用打开错误的个人资料 Filtered data

所以我的问题是如何更新按钮的位置?

notifyDataSetChanged和invalidateViews对我不起作用。

public void setupSwipeList() {
    adapter = new SearchCardAdapter(getContext(), arrayList, a);
    final SwipeMenuCreator creator = new SwipeMenuCreator() {

        @Override
        public void create(SwipeMenu menu) {
            // create "open" item
            SwipeMenuItem profileItem = new SwipeMenuItem(
                    getContext());
            // set item background
            profileItem.setBackground(new ColorDrawable(Color.rgb(38, 153, 251)));
            // set item width
            profileItem.setWidth(300);
            // set item title
            profileItem.setTitle("Profile");
            // set item title fontsize
            profileItem.setTitleSize(18);
            profileItem.setIcon(R.drawable.ic_profile);
            // set item title font color
            profileItem.setTitleColor(Color.WHITE);
            // add to menu
            menu.addMenuItem(profileItem);
            SwipeMenuItem phoneItem = new SwipeMenuItem(
                    getContext());
            // set item background
            phoneItem.setBackground(new ColorDrawable(Color.rgb(38, 153, 251)));
            // set item width
            phoneItem.setWidth(300);
            // set item title
            phoneItem.setTitle("Mobile call");
            // set item title fontsize
            phoneItem.setTitleSize(18);
            phoneItem.setIcon(R.drawable.ic_phone);
            // set item title font color
            phoneItem.setTitleColor(Color.WHITE);
            // add to menu
            menu.addMenuItem(phoneItem);

            // create "delete" item
            SwipeMenuItem requestItem = new SwipeMenuItem(
                    getContext());
            // set item background
            requestItem.setBackground(new ColorDrawable(Color.rgb(38, 153, 251)));
            // set item width
            requestItem.setWidth(300);
            // set item title
            requestItem.setTitle("Request");
            // set item title fontsize
            requestItem.setTitleSize(18);
            requestItem.setIcon(R.drawable.ic_request);
            // set item title font color
            requestItem.setTitleColor(Color.WHITE);
            // add to menu
            menu.addMenuItem(requestItem);
        }
    };


    // Right
    listView.setSwipeDirection(SwipeMenuListView.DIRECTION_RIGHT);
    // Left
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            listView.smoothOpenMenu(i);
        }
    });
    listView.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);
    listView.setMenuCreator(creator);
    listView.setAdapter(adapter);
    final Bundle bundle = new Bundle();
    listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
            switch (index) {
                case 0:
                    // open
                    SpecialistFragment postobj = new SpecialistFragment();
                    bundle.putString("spec_id", arrayList.get(position).getId());
                    bundle.putString("phone", arrayList.get(position).getPhone());
                    postobj.setArguments(bundle);
                    android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.content_frame, postobj).addToBackStack(null).commit();
                    break;
                case 1:
                    Toast.makeText(getContext(), "CALL", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:" + "+" + arrayList.get(position).getPhone()));
                    getActivity().getApplicationContext().startActivity(intent);
                    break;
                case 2:
                    SendRequestFragment obj = new SendRequestFragment();
                    bundle.putString("spec_id", arrayList.get(position).getId());
                    bundle.putString("spec_name", arrayList.get(position).getName());
                    bundle.putSerializable("categories", arrayList.get(position).getCategories());
                    bundle.putSerializable("districts", arrayList.get(position).getDistricts());
                    obj.setArguments(bundle);
                    fragmentManager = getActivity().getSupportFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.content_frame, obj).addToBackStack(null).commit();
                    break;
            }
            // false : close the menu; true : not close the menu
            return false;
        }
    });

1 个答案:

答案 0 :(得分:0)

此问题是因为我打电话给arrayList.get(position)而不是adapter.getItem(position)

过滤listView时,适配器具有另一个过滤数据,并且未与SwipeMenuListView连接。因此,我们应该致电adapter.getItem(position)以获得相关数据。

listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
            switch (index) {
                case 0:
                    // open
                    SpecialistFragment postobj = new SpecialistFragment();
                    bundle.putString("spec_id", adapter.getItem(position).getId());
                    bundle.putString("phone", adapter.getItem(position).getPhone());
                    postobj.setArguments(bundle);
                    android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.content_frame, postobj).addToBackStack(null).commit();
                    break;
                case 1:
                    Toast.makeText(getContext(), "CALL", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:" + "+" + adapter.getItem(position).getPhone()));
                    getActivity().getApplicationContext().startActivity(intent);
                    break;
                case 2:
                    SendRequestFragment obj = new SendRequestFragment();
                    bundle.putString("spec_id", adapter.getItem(position).getId());
                    bundle.putString("spec_name", adapter.getItem(position).getName());
                    bundle.putSerializable("categories", adapter.getItem(position).getCategories());
                    bundle.putSerializable("districts", adapter.getItem(position).getDistricts());
                    obj.setArguments(bundle);
                    fragmentManager = getActivity().getSupportFragmentManager();
                    fragmentManager.beginTransaction().replace(R.id.content_frame, obj).addToBackStack(null).commit();
                    break;
            }
            // false : close the menu; true : not close the menu
            return false;
        }
    });