我在应用程序中遇到的一个问题是,当我在android中的ListView
底部添加Button
时,onItemClick
不起作用,我无法单击ListView
的任何项目。为了参考,我提供了以下代码:-
Custom Adapter Class :
Context context;
String [] Heading= {"Triceps"};
String [] Subheading= {"Triceps the essintial part of an exercise it will increase you muscle power..."};
int [] gif = {R.drawable.triceps };
public custom_adapter(Context ctx) {
this.context=ctx;
}
@Override
public int getCount() {
return Heading.length;
}
@Override
public Object getItem(int position) {
return Heading[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if(view==null){
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.customlayout,null);
}
ImageView imageView=(ImageView) view.findViewById(R.id.imgId);
TextView view1=(TextView) view.findViewById(R.id.Heading);
TextView view2=(TextView) view.findViewById(R.id.Subheading);
imageView.setImageResource(gif[position]);
view1.setText(Heading[position]);
view2.setText(Subheading[position]);
return view;
}}
主要Java类:
ListView listView;
Button btn_start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
custom_adapter adapter = new custom_adapter(this);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getApplicationContext(), workout_detail.class);
intent.putExtra("Position", position);
startActivity(intent);
}
});
btn_start = (Button) findViewById(R.id.btn_start);
btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}}
Xml文件
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#abf5f5">
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="650dp"
android:background="#abf5f5"/>
<Button
android:id="@+id/btn_start"
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_below="@+id/list"
android:layout_height="65dp"
android:text="Start Workout"
android:background="#40f716"/>
</RelativeLayout>
帮我摆脱困境。提前感谢。
好的!我已经找到了答案,实际上用户必须删除自定义布局中的任何按钮(如果有的话(,并使用线性布局或约束布局