简体   繁体   English

在每个“网格视图”列表上设置onclick侦听器

[英]Set onclick listener on every Grid View list

UPDATE: I already found the answer by myself, by using if conditions or switch case, thank you! 更新:我已经通过使用if条件或切换大小写找到了答案,谢谢!

I want to make onclick listener on every grid view list. 我想在每个网格视图列表上创建onclick侦听器。

When i click on the list, it'll make an Intent to a class, but when i click on the other list, it'll make an Intent to another class, and the other lists too. 当我单击列表时,它将使一个类成为一个意图,但是当我单击另一个列表时,将对另一个类做出一个意图,另一个列表也将成为一个意图。

FirstFragment.java FirstFragment.java

public class FirstFragment extends Fragment {

    View myView;
    GridView gridView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.first_layout, container, false);

        gridView = (GridView) myView.findViewById(R.id.operview);
        gridView.setAdapter(new FirstAdapter(myView.getContext())); // uses the view to get the context instead of getActivity().

        return myView;
    }

}

FirstAdapter.java FirstAdapter.java

public class FirstAdapter extends BaseAdapter
{
    private List<Item> items = new ArrayList<Item>();
    private LayoutInflater inflater;

    public FirstAdapter(Context context)
    {
        inflater = LayoutInflater.from(context);

        items.add(new Item("Indosat", R.drawable.ic_indosat));
        items.add(new Item("Telkomsel ", R.drawable.ic_telkomsel));
        items.add(new Item("XL", R.drawable.ic_xl));
        items.add(new Item("Axis", R.drawable.ic_axis));
        items.add(new Item("Tri", R.drawable.ic_tri));
        items.add(new Item("Tri", R.drawable.ic_smartfren));
    }

    @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).drawableId;
    }

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

        if(v == null)
        {
            v = inflater.inflate(R.layout.first_layout_item, viewGroup, false);
            v.setTag(R.id.picture, v.findViewById(R.id.picture));
            v.setTag(R.id.text, v.findViewById(R.id.text));
        }

        picture = (ImageView)v.getTag(R.id.picture);
        name = (TextView)v.getTag(R.id.text);

        Item item = (Item)getItem(i);

        picture.setImageResource(item.drawableId);
        name.setText(item.name);

        return v;
    }

    private class Item
    {
        final String name;
        final int drawableId;

        Item(String name, int drawableId)
        {
            this.name = name;
            this.drawableId = drawableId;
        }
    }
}

first_layout.xml first_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <GridView
        android:id="@+id/operview"
        android:layout_marginTop="32dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:verticalSpacing="0dp"
        android:horizontalSpacing="0dp"
        android:stretchMode="columnWidth"
        android:numColumns="2" />


</FrameLayout>

first_layout_item.xml first_layout_item.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.blogspot.tigaenamdelapanmobile.SquareImageView
        android:id="@+id/picture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        />
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"
        android:layout_gravity="bottom"
        android:textColor="@android:color/white"
        android:background="#55000000"
        />
</FrameLayout>

thank you.. 谢谢..

FIXED.. 固定..

I added this code to my FirstFragment.java to make a onclick listener on every gridview item. 我将此代码添加到FirstFragment.java中,以在每个gridview项上创建onclick侦听器。

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> a, View v, int position,
                            long id) {

        if (position == 0) {
            Intent def = new Intent(getActivity(), PulsaIndosat.class);
            startActivity(def);
        } else if (position == 1) {
            Intent abc = new Intent(getActivity(), PulsaTelkomsel.class);
            startActivity(abc);
        } else if (position == 2) {
            Intent abc = new Intent(getActivity(), PulsaXlAxis.class);
            startActivity(abc);
        } else if (position == 3) {
            Intent abc = new Intent(getActivity(), PulsaXlAxis.class);
            startActivity(abc);
        } else if (position == 4) {
            Intent abc = new Intent(getActivity(), PulsaTri.class);
            startActivity(abc);
        } else if (position == 5) {
            Intent abc = new Intent(getActivity(), PulsaSmartfren.class);
            startActivity(abc);
        }

    }
});

Thanks! 谢谢!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM