我有一个带有81个按钮的GridView。我想将clicklistener添加到此gridview,但它不可用。我添加了OnItemClickListener,但它无法正常工作,我无法理解为什么。代码没有错误。唯一不起作用的是OnItemClickListener。
我的gridview子节点上有一个按钮(gridview_members.xml);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/city"
android:layout_width="183dp"
android:layout_height="90dp"
android:textSize="19sp"
android:textStyle="bold"
android:text="Code\n\nCity"
android:gravity="center"
android:background="@drawable/city_btn_tablet" />
</RelativeLayout>
我的ImageAdapter类;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return 81;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
gridView =new View(mContext);
gridView = inflater.inflate(R.layout.gridview_members, null);
Button city = (Button) gridView.findViewById(R.id.city);
return gridView;
}
}
activity_main.xml上的Gridview实现;
<GridView
android:id="@+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:gravity="center"
android:stretchMode="columnWidth"
android:numColumns="6" >
</GridView>
最后是我的MainActivity.java文件;
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
答案 0 :(得分:8)
GridView是可点击的,无需在GridView中放置按钮。
要向网格添加侦听器,请添加以下代码:
// Implement On Item click listener
gridView1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
break;
case 1:
break;
}
}
});
依此类推,您可以添加案例中的所有数字。
答案 1 :(得分:4)
您要求点击该项目(而不是项目内的按钮),因此需要更改您的子XML格式
来自
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/city"
android:layout_width="183dp"
android:layout_height="90dp"
android:textSize="19sp"
android:textStyle="bold"
android:text="Code\n\nCity"
android:gravity="center"
android:background="@drawable/city_btn_tablet" />
</RelativeLayout>
到
<?xml version="1.0" encoding="utf-8"?>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/city"
android:layout_width="183dp"
android:layout_height="90dp"
android:textSize="19sp"
android:clickable="true"
android:textStyle="bold"
android:text="Code\n\nCity"
android:gravity="center"
android:background="@drawable/city_btn_tablet" />
或者您可以在适配器
中添加ClickListener@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
gridView =new View(mContext);
gridView = inflater.inflate(R.layout.gridview_members, null);
Button city = (Button) gridView.findViewById(R.id.city);
city.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast here
}});
return gridView;
}
答案 2 :(得分:1)
如果您想获得点击项目的文字,可以使用此
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String item = ((TextView)view.findViewById(R.id.ID_OF_TEXTVIEW)).getText().toString();
Toast.makeText(showMissions.this, "" + item, Toast.LENGTH_SHORT).show();
}
});
答案 3 :(得分:0)
我建议你在getview方法中添加点击监听器,而不是添加活动类。 试试这段代码
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
gridView =new View(mContext);
gridView = inflater.inflate(R.layout.gridview_members, null);
Button city = (Button) gridView.findViewById(R.id.city);
city.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//Do your task here
}
});
return gridView;
}
答案 4 :(得分:0)
在CustomAdapter中
onBindViewHolder(...
{
yourViewHolder.gridview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Do Something
}
});
}
答案 5 :(得分:0)
您可以通过这种方式将点击侦听器添加到Kotlin中的网格视图项中
grdlyt_merchant_color_code.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
loyaltyColor = parent.getItemAtPosition(position) as LoyaltyColor?
colorCode= LoyaltyUtils.getLoyaltyColorById(loyaltyColor!!.colorId).colorCode
val color = ContextCompat.getColor(requireContext(), colorCode!!)
shape.setColor(color)
loyalty_card_img.background = shape
uncheckAllColors()
loyalty_card_img.visibility = View.GONE
loyaltyColor!!.isColorSelected = true
mAdapter.notifyDataSetChanged()
}