我有一个活动让用户点击他所处的任何心情。我创建了一个gridView并用包含imageView和textView的相对布局对其进行了充气。我已经为它设置了一个customAdaptor。我在gridView上设置了onItemClickListener但它没有响应。
Java类和适配器类
<div class="input-wrapper">
<input type="radio" id='custom-radio' class='custom-radio'>
<label for="custom-radio"></label>
</div>
<div class="input-wrapper">
<input type="radio" id='custom-radio' class='custom-radio'>
<label for="custom-radio"></label>
</div>
XML文件
public class SelectMoodActivity extends Activity {
protected ArrayList<Moods> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_mood);
data = MoodProvider.getData();
ArrayAdapter<Moods> moodsArrayAdapter = new CustomGridViewAdapter(this, R.layout.row_grid, data);
GridView gridView = findViewById(R.id.moodSelect);
gridView.setAdapter(moodsArrayAdapter);
/*
Tried to setOnClickEvent for MOOD Select */
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "You selected a Mood", Toast.LENGTH_LONG).show();
}
});
}
class CustomGridViewAdapter extends ArrayAdapter<Moods> {
Context context;
ArrayList<Moods> moodsArrayList;
int resource;
public CustomGridViewAdapter(Context context, int resource, ArrayList<Moods> objects) {
super(context, resource, objects);
this.context = context;
this.moodsArrayList = objects;
this.resource = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if(view == null){
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(resource, parent, false);
holder = new ViewHolder();
holder.moodTitle = view.findViewById(R.id.item_text);
holder.moodIcon = view.findViewById(R.id.item_image);
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
Moods mood = moodsArrayList.get(position);
holder.moodTitle.setText(mood.getMoodTitle());
int res = context.getResources().getIdentifier(mood.getMoodIcon(), "drawable", context.getPackageName());
holder.moodIcon.setImageResource(res);
/*
Tried to setOnClickEvent for MOOD Select inside the adaptor but this also didn't work.
view.setId(position);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Please Enter All Details", Toast.LENGTH_LONG).show();
}
});
*/
return view;
}
}
static class ViewHolder {
TextView moodTitle;
ImageView moodIcon;
}
}
row_grid.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="net.moodsense.moodsense.SelectMoodActivity"
android:background="@color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textMood"
android:id="@+id/textMood"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:textSize="24sp"
android:textColor="@color/black" />
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/moodSelect"
android:layout_below="@+id/textMood"
android:layout_marginTop="15dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="20dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
</RelativeLayout>
答案 0 :(得分:1)
在自定义适配器类中创建一个接口,并在单击事件中使用,如下面的代码...
onItemClickListner onItemClickListner;
public void setOnItemClickListner(CustomGridViewAdapter.onItemClickListner onItemClickListner) {
this.onItemClickListner = onItemClickListner;
}
public interface onItemClickListner {
void onClick();// hear you can pass any type of data as a argument;
}
然后在适配器类中的getView()方法之后定义下面的单击事件处理代码。
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClickListner.onClick();
}
});
然后在为活动处理定义适配器对象时,在活动代码中使用下面的网格视图中绑定适配器。
moodsArrayAdapter.setOnItemClickListner(new CustomGridViewAdapter.onItemClickListner() {
@Override
public void onClick() {
Toast.makeText(getApplicationContext(),"Hello world",0).show();
}
});
答案 1 :(得分:0)
在rawGrid&gt; RelativLayout中添加 android:descendantFocusability =“blocksDescendants
并添加
focusable =“false”在rawGrid&gt; RelativLayout&gt; textview,rawGrid&gt; RelativLayout&gt; imageview
和
gridview 中focusable =“true”