这是我的主要档案。这里我在Listview上设置一个onClickListener,它会导致任何操作 公共类出席扩展AppCompatActivity {
private ArrayList<attendance_pojo> arrayList = new ArrayList<>();
private Attendance_adaptwr adapter;
private int present,absent,datecntr,presents,absents;
private String year,month,date;
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendance);
init();
fetchData();
adapter = new Attendance_adaptwr(this, R.layout.card_attendance, arrayList);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(Attendance.this, "Hello from ROy "+position, Toast.LENGTH_SHORT).show();
}
});
}
Arrayadapter文件在这里
public class Attendance_adaptwr extends ArrayAdapter{
private Context context;
private int layres;
private ArrayList<attendance_pojo> arrayList;
private LayoutInflater inflater;
public Attendance_adaptwr(@NonNull Context context, @LayoutRes int resource, @NonNull ArrayList<attendance_pojo> objects) {
super(context, resource, objects);
this.context = context;
this.layres = resource;
this.arrayList = objects;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View view = inflater.inflate(layres, null);
TextView month, workdays;
Button present,absent;
month= view.findViewById(R.id.tv_month);
workdays= view.findViewById(R.id.work_Days);
present = view.findViewById(R.id.tv_present);
absent= view.findViewById(R.id.tv_absent);
attendance_pojo pojo= arrayList.get(position);
String months= pojo.getMonth();
Log.d("months",months);
months= checkmonth(months);
Log.d("datess",""+pojo.getDate());
month.setText(months+" "+pojo.getYear());
workdays.setText(""+pojo.getDatecounter());
present.setText("Present "+pojo.getPresent());
absent.setText("Absent "+pojo.getAbsent());
return view;
}
private String checkmonth(String month) {
String monthString;
switch (month) {
case "1": monthString = "January"; break;
case "2": monthString = "February"; break;
case "3": monthString = "March"; break;
case "4": monthString = "April"; break;
case "5": monthString = "May"; break;
case "6": monthString = "June"; break;
case "7": monthString = "July"; break;
case "8": monthString = "August"; break;
case "9": monthString = "September"; break;
case "10": monthString = "October"; break;
case "11": monthString = "November"; break;
case "12": monthString = "December"; break;
default: monthString = "Invalid month"; break;
}
return monthString;
}
}
单击时我无法获得任何Toast,并且也没有触发任何Intent。我想确保,它也应该让我回到原位。
这是我夸大的布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_marginBottom="15dp"
android:background="#000"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#9714b3cf"
android:orientation="vertical">
<TextView
android:id="@+id/tv_month"
android:background="#b51992b7"
android:text="January 2017"
android:gravity="center"
android:textSize="25sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_marginTop="40dp"
android:layout_height="wrap_content"
android:text="WORK DAYS"
android:gravity="center"
android:textSize="25sp"
/>
<TextView
android:id="@+id/work_Days"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="wrap_content"
android:text="18"
android:gravity="center"
android:textSize="45sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="@+id/tv_present"
android:layout_width="wrap_content"
android:background="#d3edfd07"
android:text="PRESENT 18"
android:textSize="12sp"
android:layout_marginEnd="5dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/tv_absent"
android:text="ABSENT 0"
android:background="#90e775d2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
答案 0 :(得分:0)
如果listView的行项包含可聚焦视图,则OnItemClickListener无法工作。将以下属性添加到列表视图项的根布局中。
android:descendantFocusability="blocksDescendants"
这样做的结果是它不再允许您使用硬件按钮专注于内部按钮。
答案 1 :(得分:0)
因为您在列表项中使用按钮,这就是OnItemClick无法正常工作的原因
您需要在listview_item.xml中执行两个步骤
android:descendantFocusability="blocksDescendants"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
尝试在按钮中添加这三行
<Button
android:id="@+id/button1"
android:layout_width=""
android:layout_height=""
//...........Add this three line in your every button................
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
/>