拖动和GridView控件拖放图像的android [英] Drag and drop images in gridview in android

查看:88
本文介绍了拖动和GridView控件拖放图像的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我'开发一个样本Android应用程序,以了解拖放放大器;下降的机器人。在应用程序的开始,我'上显示网格视图几张图片。现在我需要转移到另一个地方拖动一个图像的时间。下降的图像在另一个之后,图像应交换的场所。我怎样才能实现呢?请指导/帮助我。

I'am developing a sample android application to learn about drag & drop in android. On start of the app, i'am displaying few images on a grid view. Now i need to drag one image at a time over to the place of another. After dropping an image over another, the images should swap its places. How can i achieve it ? Please guide/help me.

推荐答案

您可以轻松地通过使用thquinn的 DraggableGridView

You can easily achieve this by using thquinn's DraggableGridView

您可以添加自定义布局

public class DraggableGridViewSampleActivity extends Activity {
    static Random random = new Random();
    static String[] words = "the of and a to in is be that was he for it with as his I on have at by not they this had are but from or she an which you one we all were her would there their will when who him been has more if no out do so can what up said about other into than its time only could new them man some these then two first may any like now my such make over our even most me state after also made many did must before back see through way where get much go well your know should down work year because come people just say each those take day good how long Mr own too little use US very great still men here life both between old under last never place same another think house while high right might came off find states since used give against three himself look few general hand school part small American home during number again Mrs around thought went without however govern don't does got public United point end become head once course fact upon need system set every war put form water took".split(" ");
    DraggableGridView dgv;
    Button button1, button2;
    ArrayList<String> poem = new ArrayList<String>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        dgv = ((DraggableGridView)findViewById(R.id.vgv));
        button1 = ((Button)findViewById(R.id.button1));
        button2 = ((Button)findViewById(R.id.button2));

        setListeners();
    }
    private void setListeners()
    {
        dgv.setOnRearrangeListener(new OnRearrangeListener() {
            public void onRearrange(int oldIndex, int newIndex) {
                String word = poem.remove(oldIndex);
                if (oldIndex < newIndex)
                    poem.add(newIndex, word);
                else
                    poem.add(newIndex, word);
            }
        });
        dgv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
                Toast.makeText(getApplicationContext(), "On clicked" +position, Toast.LENGTH_SHORT).show();
                dgv.removeViewAt(position);
                poem.remove(position);
            }
        });
        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                String word = words[random.nextInt(words.length)];
                addView();
                poem.add(word);
            }
        });
        button2.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                String finishedPoem = "";
                for (String s : poem)
                    finishedPoem += s + " ";
                new AlertDialog.Builder(DraggableGridViewSampleActivity.this)
                .setTitle("Here's your poem!")
                .setMessage(finishedPoem).show();
            }
        });
    }

    public void addView()
    {

        LayoutParams mLayoutParams= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        LinearLayout mLinearLayout= new LinearLayout(DraggableGridViewSampleActivity.this);
        mLinearLayout.setOrientation(LinearLayout.VERTICAL);
        mLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);

        ImageView mImageView = new ImageView(DraggableGridViewSampleActivity.this);

        if(dgv.getChildCount()%2==0)
            mImageView.setImageResource(R.drawable.child1);
        else
            mImageView.setImageResource(R.drawable.child2);

        mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        mImageView.setLayoutParams(mLayoutParams);

        mImageView.setId(dgv.getChildCount());

        mImageView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), v.getId()+"clicked ", Toast.LENGTH_SHORT).show();
                return dgv.onTouch(v, event);
            }
        });


            TextView mTextView = new TextView(DraggableGridViewSampleActivity.this);

            mTextView.setLayoutParams(mLayoutParams);

            mTextView.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), v.getId()+"clicked text ", Toast.LENGTH_SHORT).show();
                    return dgv.onTouch(v, event);
                }
            });

            TextView mTextViewLabel = new TextView(DraggableGridViewSampleActivity.this);

            mTextViewLabel.setText(((dgv.getChildCount()+1)+""));
            mTextViewLabel.setLayoutParams(mLayoutParams);

            mTextViewLabel.setId(dgv.getChildCount());

            mTextViewLabel.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    Toast.makeText(getApplicationContext(), v.getId()+"clicked text ", Toast.LENGTH_SHORT).show();
                    return dgv.onTouch(v, event);
                }
            });

            mLinearLayout.setTag(mTextViewLabel);
            mLinearLayout.addView(mTextViewLabel);
            mLinearLayout.addView(mImageView);
            mLinearLayout.addView(mTextView);
        dgv.addView(mLinearLayout);

    }
}

这篇关于拖动和GridView控件拖放图像的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆