简体   繁体   English

如何在android中滑动时隐藏/显示listview项目上隐藏的组件?

[英]How to hide/show hidden components on items of listview when swipe in android?

This is what I have done. 这就是我所做的。 I have displayed the hidden components on item of listview when you swipe the item of listview now I need to hide the components again when other item gets swiped or the listview is scrolled . 当你刷一下listview的项目时,我已经在listview的项目上显示了隐藏的组件,我需要在刷新其他项目或滚动列表视图时再次隐藏组件。

public class MainActivity extends Activity {

    ListView lstView1;
    ArrayList<HashMap<String, String>> MyArrList;
    GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 100;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyArrList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;

        /*** Rows 1 ***/
        map = new HashMap<String, String>();
        map.put("ImageID", "1");
        map.put("ImageDesc", "Sea View1");
        map.put("ImagePath", "pic_a");
        map.put("status", "false");
        MyArrList.add(map);

        /*** Rows 2 ***/
        map = new HashMap<String, String>();
        map.put("ImageID", "2");
        map.put("ImageDesc", "Sea View2");
        map.put("ImagePath", "pic_b");
        map.put("status", "false");
        MyArrList.add(map);

        /*** Rows 3 ***/
        map = new HashMap<String, String>();
        map.put("ImageID", "3");
        map.put("ImageDesc", "Sea View 3");
        map.put("ImagePath", "pic_c");
        map.put("status", "false");
        MyArrList.add(map);

        /*** Rows 4 ***/
        map = new HashMap<String, String>();
        map.put("ImageID", "4");
        map.put("ImageDesc", "Sea View 4");
        map.put("ImagePath", "pic_d");
        map.put("status", "false");
        MyArrList.add(map);

        // listView1
        lstView1 = (ListView) findViewById(R.id.listView1);

        lstView1.setAdapter(new ImageAdapter(this));
        // lstView1.setEnabled(false);

        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
        lstView1.setOnTouchListener(gestureListener);
        //lstView1.setOnScrollListener(l)
    }

    class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {

                    int pos = YourSlideRightToLeft(lstView1.pointToPosition(
                            (int) e1.getX(), (int) e1.getY()));
                    MyArrList.get(pos).put("status", "true");
                    ((BaseAdapter) lstView1.getAdapter())
                            .notifyDataSetChanged();

                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    // left to right swipe
                    YourSlideLeftToRight(lstView1.pointToPosition(
                            (int) e1.getX(), (int) e1.getY()));
                    int pos = YourSlideRightToLeft(lstView1.pointToPosition(
                            (int) e1.getX(), (int) e1.getY()));
                    MyArrList.get(pos).put("status", "true");
                    ((BaseAdapter) lstView1.getAdapter())
                            .notifyDataSetChanged();

                }
            } catch (Exception e) {
                // nothing
                return true;
            }

            return true;

        }
    }

    // Command for Slide Right to Left (<-----)
    public int YourSlideRightToLeft(int position) {
        Toast.makeText(
                MainActivity.this,
                "<-- Your Slide Right to Left ImgID = "
                        + MyArrList.get(position).get("ImageID"),
                Toast.LENGTH_SHORT).show();
        return position;
    }

    // Command for Slide Left to Right (<-----)
    public int YourSlideLeftToRight(int position) {
        Toast.makeText(
                MainActivity.this,
                "---> Your Slide Left to Right ImgID = "
                        + MyArrList.get(position).get("ImageID"),
                Toast.LENGTH_SHORT).show();
        return position;
    }

    public class ImageAdapter extends BaseAdapter {
        private Context context;

        public ImageAdapter(Context c) {
            // TODO Auto-generated method stub
            context = c;
        }

        public int getCount() {
            // TODO Auto-generated method stub
            return MyArrList.size();
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_column, null);
            }

            // ColImage
            ImageView imageView = (ImageView) convertView
                    .findViewById(R.id.ColImgPath);
            imageView.getLayoutParams().height = 80;
            imageView.getLayoutParams().width = 80;
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(5, 5, 5, 5);
            int ResID = context.getResources().getIdentifier(
                    MyArrList.get(position).get("ImagePath"), "drawable",
                    context.getPackageName());
            imageView.setImageResource(ResID);

            // ColImgID
            TextView txtPosition = (TextView) convertView
                    .findViewById(R.id.ColImgID);
            txtPosition.setPadding(10, 0, 0, 0);
            txtPosition.setText(MyArrList.get(position).get("ImageID") + ".");

            // ColPicname
            TextView txtPicName = (TextView) convertView
                    .findViewById(R.id.ColImgDesc);
            txtPicName.setPadding(5, 0, 0, 0);
            txtPicName.setText(MyArrList.get(position).get("ImageDesc"));
            String value = MyArrList.get(position).get("status");
            if (value.contentEquals("true")) {
                ((TableRow) convertView.findViewById(R.id.tableRow4))
                        .setVisibility(View.VISIBLE);
                ((Button) convertView.findViewById(R.id.btn)).setTag(Integer
                        .valueOf(position));
                ((ToggleButton) convertView.findViewById(R.id.tg))
                        .setTag(Integer.valueOf(position));
            } else {
                ((LinearLayout) convertView.findViewById(R.id.tableRow4))
                        .setVisibility(View.GONE);
            }

            return convertView;

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

这个'SwipeToDismissUndoList'代码可能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当您停止在android中滚动listview / recyclerview时,如何隐藏或显示部分隐藏的工具栏/操作栏 - How to hide or show the partially hidden toolbar/actionbar when you stop scrolling the listview/recyclerview in android Android如何在单击ListView项目时隐藏和显示ListView中的项目 - Android How to hide and display items in a ListView when a ListView item is clicked 在Android ListView项目上滑动手势 - Swipe gestures on Android ListView items 在Android中如何将组件放置在带有项目的列表视图中? - In android how to place components in a listview with items? 添加Listview项时,如何使ListView控件滚动以显示隐藏项,而工具栏始终显示? - How can make ListView control to scroll to show the hidden items and toolbar always show when I add the listview item? 如何滑动列表视图-Android - How to swipe the listview — android Android ListView在加载ListView时隐藏按钮项 - Android listview hide button items when load listview Android:根据ListView中的项目数显示/隐藏menuitem - Android: show/hide menuitem based on number of items in the ListView 如何在Android ListView中动态隐藏/显示按钮 - How to hide/show button in Android ListView dynamically 如何在Android的ListView中隐藏和显示图像? - how to hide and show image in listview in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM