onTouchListener检测错误的动态创建的cardview

时间:2017-08-14 15:49:26

标签: android android-cardview

我刚学会了如何动态创建cardview。我正在使用按钮按下来创建一个cardview,我已经设置了onTouchListener。当我只创建一张牌onTouchListener时效果很好,但是一旦我再创建一张onTouchListener卡就不能正常工作了。当我有2张牌并且我试图移动第一张牌时它会移动第二张而不是第一张。

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

Context context;
ImageView imageView;
RelativeLayout relativeLayout;
static int pos=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    context=this;
    relativeLayout=(RelativeLayout)findViewById(R.id.rl_layout);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}



@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {

        CreateCardViewWithoutImageView();

        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    return true;
}

public void CreateCardViewWithoutImageView(){

    final CardView cardview=new CardView(context);

    cardview.setId(++pos);
    cardview.setLayoutParams((new CardView.LayoutParams(600, 600)));
    cardview.setContentPadding(25,25,25,25);
    cardview.setCardBackgroundColor(Color.MAGENTA);

    relativeLayout.addView(cardview);

}

private final View.OnTouchListener mListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View card, MotionEvent event) {
        // The View (v) here is a CardView as the onTouchListener is set to it
        RelativeLayout.LayoutParams layoutParamsRL = (RelativeLayout.LayoutParams) ((CardView) card).getLayoutParams();
        float dx = 0, dy = 0, x = 0, y = 0;

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                card.bringToFront();
                dx = event.getRawX() - layoutParamsRL.leftMargin;
                dy = event.getRawY() - layoutParamsRL.topMargin;
            }
            break;
            case MotionEvent.ACTION_MOVE: {
                x = event.getRawX();
                y = event.getRawY();
                layoutParamsRL.leftMargin = (int) (x - dx);
                layoutParamsRL.topMargin = (int) (y - dy);
                card.setLayoutParams(layoutParamsRL);
            }
            break;
            case MotionEvent.ACTION_UP: {

            }
            break;
        }
        return true;
    }
};

}

3 个答案:

答案 0 :(得分:1)

这是因为您的<script src="@routes.Assets.versioned("javascripts/hello.js")" type="text/javascript"></script> 变量是全局变量。因此,每次创建新的CardView时,它都会被替换,因此侦听器始终使用最后创建的CardView。 你应该做点什么:

cardview

// Note: By the way, you should NOT use a capital letter to name a function public void createCardViewWithoutImageView(){ //CardView final CardView cardview = new CardView(context); //.... } 您将遇到同样的问题,因为它也应该是本地问题。

修改

layoutParamsRL

答案 1 :(得分:0)

1-变量是全局的,所以你需要在内部方法中定义,如上所述 2-您可以使用其他方法轻松更改其属性,并将cardview对象作为参数传递给它 3-你可以保持全局对象代表所选的cardview,因为在touchevent方法中会将fireview事件分配给全局并执行你想要的事情

答案 2 :(得分:0)

//创建Global对象以在分配新属性时使用它,如你所愿

int a[1];
int tmp = a[5];

//将创建内部对象

CardView selectedCardview;
@Override
protected void onCreate(Bundle savedInstanceState) { //....... }

现在选择卡片使用将代表卡片浏览你触摸它并将很容易设置其属性为全球

public void CreateCardViewWithoutImageView(){
    CardView cardview = new CardView(context);
    //Setting the default property u want:)
    cardview.setId(++pos);
    cardview.setLayoutParams((new CardView.LayoutParams(600, 600)));
    .......
    final CardView tempCardView=cardview;
    cardview.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction())
        {
            case MotionEvent.ACTION_DOWN :
            {
              selectedCardview=tempCardView;
              .........and adding action as u want
            }
             ........... add another cases 
        }

    }
    ...........
}