gpt4 book ai didi

android - onItemClickListener 在 GridView 中没有响应

转载 作者:行者123 更新时间:2023-11-29 19:01:43 25 4
gpt4 key购买 nike

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 的 fragment

    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;
}
}

网格适配器.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>

自定义布局.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>

我已经给出了我尝试启动第三个 Activity 的代码。但是它没有任何响应所以我尝试使用代码但是 Toast 也没有出现。请帮忙。我对 stackover flow 很陌生。请忽略我的错误并提出建议。谢谢你

最佳答案

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

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

通过 Activity 中的界面处理 onItemClick,一切顺利。

关于android - onItemClickListener 在 GridView 中没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48824540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com