onItemClickListener在GridView中没有响应

时间:2018-02-16 10:17:56

标签: android android-fragments gridview android-tablayout

Main2Activity.java:

    package com.example.imalok.shopcart;
    public class Main2Activity extends AppCompatActivity {
    private BottomNavigationView mMainNav;
    ViewPager viewPager;
    Button love;
    GridLayout mainGrid;


private SectionsPagerAdapter mSectionsPagerAdapter;   
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
    BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
    mMainNav = (BottomNavigationView)findViewById(R.id.bottom_nav);
    mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()){
                case R.id.bot_home:

                    return true;
                case R.id.bot_offer:
                    //  mMainNav.setItemBackgroundResource(R.color.white);
                    return true;
                case R.id.bot_cart:
                    //  mMainNav.setItemBackgroundResource(R.color.white);
                    return true;
                case R.id.bot_search:
                    //mMainNav.setItemBackgroundResource(R.color.white);
                    return true;
                case R.id.bot_profile:
                    //mMainNav.setItemBackgroundResource(R.color.white);
                    //  transaction.replace(R.id.contain,new Profile()).commit();
                    return true;
                default:
                    return false;
            }
        }
    });
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                tab1 tab1=new tab1();
                return tab1;
            case 1:
                tab2 tab2=new tab2();
                return tab2;
            case 2:
                tab3 tab3=new tab3();
                return tab3;
        }
        return null;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "tab 1";
            case 1:
                return "tab 2";
            case 2:
                return "tab 3";
        }
        return null;
    }
}
       }

Tab1.java // Tab 1的片段

    package com.example.imalok.shopcart;
    public class tab1 extends Fragment{
GridView gridView;

String letterlist[]={"Alok","Alok","Alok","Alok","Alok","Alok","Alok","Alok","Alok"};

int letterIcon[]={R.drawable.p1,
        R.drawable.p2,
        R.drawable.p3,
        R.drawable.p4,
        R.drawable.p5,
        R.drawable.p6,
        R.drawable.p7,
        R.drawable.p8,
        R.drawable.p9};

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

    gridView=(GridView)rootView.findViewById(R.id.gridView);

    GridAdapter adapter=new GridAdapter(getContext(),letterIcon,letterlist);

    gridView.setAdapter(adapter);

    **gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent i = new Intent(getActivity(),Main3Activity.class);
            startActivity(i);
        }
    });**

    return rootView;
}
    }

GridAdapter.java

    public class GridAdapter extends BaseAdapter {

private int icons[];

private String letters[];
private Context context;

private LayoutInflater inflater;

public GridAdapter(Context context,int icons[],String letters[]){

    this.context=context;
    this.icons=icons;
    this.letters=letters;


}

public GridAdapter(){

}

@Override
public int getCount() {
    return letters.length;
}

@Override
public Object getItem(int position) {
    return letters[position];
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    convertView = null;

    if(convertView==null){
        inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView =inflater.inflate(R.layout.custom_layout,null);
    }

    ImageView icon=(ImageView) convertView.findViewById(R.id.icons);
    TextView letter=(TextView)convertView.findViewById(R.id.letters);

    icon.setImageResource(icons[position]);

    letter.setText(letters[position]);



    return convertView;

}
    }

Tab1.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.imalok.shopcart.Main2Activity">


<GridView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/gridView"
    android:columnWidth="150dp"
    android:clickable="true"
    android:numColumns="auto_fit"/>
    </RelativeLayout>

custom_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">


<ImageView
    android:layout_width="160dp"
    android:layout_height="160dp"
    android:id="@+id/icons"
    android:layout_marginTop="20dp"
    android:src="@drawable/p1"/>

<TextView
    android:id="@+id/letters"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/button2"
    android:layout_alignStart="@+id/icons"
    android:layout_marginStart="11dp"
    android:text="Alok"
    android:textSize="20sp" />

<Button
    android:id="@+id/button2"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignEnd="@+id/icons"
    android:layout_below="@+id/icons"
    android:background="@drawable/selector" />

    </RelativeLayout>

activity_main2.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context="com.example.imalok.shopcart.Main2Activity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#fff"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">



        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="wrap_content"
            android:layout_gravity="center"
            android:layout_height="?attr/actionBarSize"
            android:layout_weight="1"
            android:background="#fff"
            app:titleTextColor="#000"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:title="Women's Wear">


        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            app:tabTextColor="#000"
            android:layout_width="match_parent"
            app:tabSelectedTextColor="#00ffff"
            android:layout_height="wrap_content">

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tab_text_1" />

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tab_text_2" />

            <android.support.design.widget.TabItem
                android:id="@+id/tabItem3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/tab_text_3" />

        </android.support.design.widget.TabLayout>
    </android.support.design.widget.AppBarLayout>


    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_marginTop="113dp"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_nav"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    </android.support.v4.view.ViewPager>

    <android.support.design.widget.BottomNavigationView
        android:layout_width="match_parent"
        app:menu="@menu/bottom_menu"
        android:layout_alignParentBottom="true"
        android:id="@+id/bottom_nav"
        android:layout_height="wrap_content">
    </android.support.design.widget.BottomNavigationView>

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="60dp"
        android:layout_height="30dp"
        android:layout_alignBottom="@+id/appbar"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="60dp"
        app:srcCompat="@drawable/back" />
        </RelativeLayout>

我已经提供了我尝试启动第3个活动的代码。但它,没有回应任何东西,所以我尝试使用代码,但Toast也没有出现。请帮忙。我对stackover流程很新。请忽略我的错误并提出建议。谢谢

2 个答案:

答案 0 :(得分:2)

看看这个question。虽然它是关于ListViews的,但您应该能够采用。

我的建议是在返回之前在适配器的View.OnClickListener添加convertView

通过您的活动中的界面处理onItemClick,您就可以了。

答案 1 :(得分:0)

添加此行并尝试

View gridViewAndroid;
if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    gridViewAndroid = inflater.inflate(R.layout.landing_grid_brand_layout_design, null);
} else {
    gridViewAndroid = (View) convertView;
}

并点击使用

gridViewAndroid.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //your click action
    }
});