Android拖放,API< 11

时间:2014-05-07 20:58:07

标签: android api drag

我是android的新手(直到现在3周),我有一个问题(当然)。 我经常搜索并找到关于这个问题的几个主题,但没有得到正确的答案。

我无法在api< 11中使用clipdata,所以我完全从我的代码中删除了该部分。 并将view.startDrag中的clipdata替换为null。一切都适用于API> = 11。 但对于低于11的API级别,我收到错误。

我不知道为什么(posible dragEvent ???)因为我没有得到任何方法的消息 我的代码与API级别2.2或其他东西不兼容。我的代码是:

public class GlavnaAktivnost extends Activity {
String linije[] = { "141 - SAMOBOR - Rakov Potok - JAGNJIĆ DOL",
        "142 - SAMOBOR - Klake - TERIHAJI",
        "143 - SAMOBOR - Rude - BRASLOVJE",
        "144 - SAMOBOR - Smerovišće - LIPOVEC",
        "145 - SAMOBOR - SLANI DOL",
        "146 - SAMOBOR - Otruševac - VRHOVČAK", "147 - SAMOBOR - BREGANA",
        "148 - SAMOBOR - OTOK", "149 - SAMOBOR - MEDSAVE",
        "150 - SAMOBOR - Grdanjci - Kršlin - GORNJA VAS",
        "150A - SAMOBOR - Grdanjci - NORŠIĆ SELO",
        "151 - SAMOBOR - Sv. Nedelja - ZAGREB (ČRNOMEREC)",
        "153 - SAMOBOR - Sv. Nedelja - ZAGREB (LJUBLJANICA)",
        "155 - SAMOBOR - Sv. Nedelja - ZAGREB (AUTOBUSNI KOLODVOR)",
        "157 - SAMOBOR - Sv. Martin - GALGOVO",
        "158 - SAMOBOR - Cerje - BUKOVJE",
        "159 - ZAGREB (SAVSKI MOST) - Galgovo - DREŽNIK / SV. MARTIN",
        "160 - MALA GORICA - Kerestinec - ZAGREB (A. K.)",
        "162 - SAMOBOR - Sv. Nedjelja - Novaki - Rakitje - ČRNOMEREC",
        "260 - LULIĆ - Jastrebarsko - ZAGREB (SAVSKI MOST)",
        "260A - JASTREBARSKO - Krašić - PRIBIĆ",
        "261 - KLINČA SELA - Repišće - NOVO SELO",
        "262 - PISAROVINA - KLINČA SELO",
        "263 - ZAGREB (SAVSKI MOST) - Kupinečki Kraljevac - DONJA KUPČINA",
        "264 - JASTREBARSKO - Klinča Selo - ZAGREB (A. K.)",
        "265 - PISAROVINA - LIJEVO SREDIČKO",
        "266 - PISAROVINA - Lučelnica - Vukomerić - DUBRANEC",
        "267 - KLINČA SELA - BETER", "KALJE - KOSTANJEVAC - KALJE",
        "SOŠICE - JASTREBARSKO - SOŠICE",
        "SVETA JANJA - JASTREBARSKO - SLAVETIĆ - SVETA JANA" };
String favoriti[] = { "Dodaj favorit +", "", "" };
MyDragEventListener myDragEventListener = new MyDragEventListener();
ListView list1, list2;
int i = 0;
int pozicija;
ArrayAdapter<String> adapter1, adapter2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pozadina);
    inicijalizacijaIinterface();

    list2.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            MyDragShadowBuilder myShadow = new MyDragShadowBuilder(view);
            try{view.startDrag(null, // ClipData
                    myShadow, // View.DragShadowBuilder
                    linije[position], 0);// Object myLocalState
            }
            catch(Exception e){e.printStackTrace();}


            pozicija = adapter2.getPosition(linije[position].toString());

            return true;
        }
    });
    list1.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            if (position == 0) {
                Intent intent = new Intent(
                        "samobor.samoborcek.RasporedVoznje");
                intent.putExtra("LIN", 143);
                startActivity(intent);
            }
        }
    });
}

private void inicijalizacijaIinterface() {
    // TODO Auto-generated method stub
    list1 = (ListView) findViewById(R.id.list1);
    list2 = (ListView) findViewById(R.id.list2);
    adapter1 = new ArrayAdapter<String>(GlavnaAktivnost.this,
            R.layout.listview, favoriti);

    adapter2 = new ArrayAdapter<String>(GlavnaAktivnost.this,
            R.layout.listview, linije);
    list2.setAdapter(adapter2);

    list1.setAdapter(adapter1);
    list1.setOnDragListener(myDragEventListener);
    list2.setOnDragListener(myDragEventListener);
    list1.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if (MotionEvent.ACTION_MOVE == event.getAction())
                return true;
            return false;
        }
    });

}

public void paraf() {
    // TODO Auto-generated method stub

    if (i == 1) {
        LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT, 82);
        params3.bottomMargin = 20;// Jedna linija
        list1.setLayoutParams(params3);
        LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT, 18);
        list2.setLayoutParams(params4);
    }
    if (i == 2) {
        LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT, 66);
        params3.bottomMargin = 20;// Dvije linije
        list1.setLayoutParams(params3);
        LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT, 34);
        list2.setLayoutParams(params4);
    }
    if (i == 3) {
        LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT, 49);
        params5.bottomMargin = 20;// 3 linije
        list1.setLayoutParams(params5);
        LinearLayout.LayoutParams params6 = new LinearLayout.LayoutParams(
                android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                android.view.ViewGroup.LayoutParams.MATCH_PARENT, 51);
        list2.setLayoutParams(params6);
    }

}

private static class MyDragShadowBuilder extends View.DragShadowBuilder {
    private static Drawable shadow;

    public MyDragShadowBuilder(View v) {
        super(v);
        shadow = new ColorDrawable(Color.argb(140, 102, 255, 255));
    }

    @Override
    public void onProvideShadowMetrics(Point size, Point touch) {
        int width = getView().getWidth();
        int height = getView().getHeight();

        shadow.setBounds(0, 0, width, height);
        size.set(width, height);
        touch.set(width / 2, height / 2);
    }

    @Override
    public void onDrawShadow(Canvas canvas) {
        shadow.draw(canvas);
    }

}

private class MyDragEventListener implements View.OnDragListener {
    @Override
    public boolean onDrag(View v, DragEvent event) {
        final int action = event.getAction();

        switch (action) {
        case DragEvent.ACTION_DRAG_ENTERED:
            if (v == list1) {
                i++;
                paraf();
            }
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            if (v == list1) {
                i--;
                paraf();
            }
            break;
        case DragEvent.ACTION_DROP:
            // Gets the item containing the dragged data

            // If apply only if drop on buttonTarget
            if (v == list1 && i <= 3) {

                favoriti[i - 1] = linije[pozicija].toString();

                adapter1.notifyDataSetChanged();

                while (pozicija + 1 < linije.length) {
                    linije[pozicija] = linije[pozicija + 1].toString();
                    if (pozicija + 1 == linije.length) {
                        linije[pozicija] = " ";
                    }
                    pozicija++;

                }

                adapter2.notifyDataSetChanged();
            }
            if (v != list1 || i > 3)
                Toast.makeText(getApplicationContext(), "Nedozvoljeno!",
                        Toast.LENGTH_SHORT).show();

            break;
        }
        return true;
    }
}

问题:出了什么问题?或者你能告诉我,如果API级别高于11,如何做,如果没有做其他事情?  因为MyDragEventListener是在任何方法之外构造的,并且如果我在示例onCreat中构造它,它不会是全局的,因此它不会在任何其他方法中可见。

我现在解决它,我创建了2个其他活动,第一个活动是主屏幕...如果api级别是示例2.2启动此活动(在此活动中我使用如果list2上的项目被长期点击发送数据到list1(NO DRAG AND DROP))如果是4.3则启动其他活动(DRAG AND DROP)。

P.S。我知道这段代码很蹩脚,而且我不知道为什么我只是不看那个paraf()方法,我稍后会修复它:) 谢谢, Matija

1 个答案:

答案 0 :(得分:0)

View.OnDragListener was introduced with API level 11。尝试创建MyDragEventListener类的实例(从中派生)将在预蜂窝设备上崩溃。

您可以检查API级别并使用android.os.Build.VERSION.SDK_INT字段进行相应操作,例如

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB)
{
    MyDragEventListener myDragEventListener = new MyDragEventListener();
    ...
}

与View.DragShadowBuilder相同,&amp; c。