Gridview的Android setOnTouchListener仅适用于第一行

时间:2015-11-02 18:53:20

标签: android gridview ontouchlistener

我正在开发一个自定义按钮,当我触摸gridview项时,内容必须更改背景并移动图像视图和文本,模拟按下的按钮,当按钮被释放时,所有元素都必须恢复原状。使用setOnTouchListener此规范有效,但对于第一行,当我单击另一行时,效果将应用于第一行。这是我的代码:

MainActivity.java:

package sumare.sumare;

import android.app.ActionBar;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class MainActivity extends AppCompatActivity {

    GridView menu;
    String[] botoes={
            "Avisos",
            "Calendario",
            "Notas",
            "Grade",
            "Rematricula",
            "Financeiro",
            "Serviços",
            "Biblioteca"
    };


   int[] imagens = {
           R.drawable.ic_announcement_white_48dp,
           R.drawable.ic_event_white_48dp,
           R.drawable.ic_assignment_turned_in_white_48dp,
           R.drawable.ic_school_white_48dp,
           R.drawable.ic_border_color_white_48dp,
           R.drawable.ic_attach_money_white_48dp,
           R.drawable.ic_build_white_48dp,
           R.drawable.ic_local_library_white_48dp
   };

    private class Item {
        final String texto;
        final int imagem;
        final int id;

        Item(int Id,String Texto, int Imagem) {
            this.id = Id;
            this.texto = Texto;
            this.imagem = Imagem;

        }
    }

    private List<Item> items = new ArrayList<Item>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setTitle("Faculdade Sumaré");
        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.titlecolor));

        menu = (GridView)findViewById(R.id.grdMenu);
        menu.setAdapter(new MenuAdapter(this));
        menu.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                final ImageView imgatual = (ImageView) v.findViewById(R.id.imgButton);
                final RelativeLayout layout = (RelativeLayout) v.findViewById(R.id.Botao);
                final RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) imgatual.getLayoutParams();

                if(event.getAction() == MotionEvent.ACTION_DOWN) {
                    param.topMargin = 6;
                    layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.botao_pressionado));
                    imgatual.setLayoutParams(param);

                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    param.topMargin = 0;
                    layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.botao_normal));
                    imgatual.setLayoutParams(param);
                }


                return true;
            }
        });


        }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private class MenuAdapter extends BaseAdapter {

        private LayoutInflater inflater;

        public MenuAdapter(Context context) {
            inflater = LayoutInflater.from(context);
            int i=0;

            for (String des : botoes) {
                items.add(new Item(i,des,imagens[i]));
                i++;
            };


        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int i) {
            return items.get(i);
        }


        @Override
        public long getItemId(int i) {
            return items.get(i).id;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View v = view;
            ImageView imagem;
            TextView Texto;

            if (v == null) {
                v = inflater.inflate(R.layout.botao, viewGroup, false);
                v.setTag(R.id.txtBotao, v.findViewById(R.id.txtBotao));
                v.setTag(R.id.imgButton, v.findViewById(R.id.imgButton));
            }

            imagem = (ImageView) v.getTag(R.id.imgButton);
            Texto = (TextView) v.getTag(R.id.txtBotao);

            Item item = (Item) getItem(i);

            //picture.setImageResource(item.drawableId);
            imagem.setImageResource(item.imagem);
            Texto.setText(item.texto);

            return v;
        }



    }
}

有关如何以其他方式做到这一点的任何建议,或者这是如何运作的?

0 个答案:

没有答案