如何实现布尔监听器我有一个带有图像,文本和布尔值的自定义listView但是我无法弄清楚如何设置布尔监听器。这是一些图片:
我已经拥有全屏代码并且它完美无缺,但只有当我点击全屏文本而不是Switch时。有没有关于Switch监听器的想法?
这是customview类
public CustomListViewImageTextText(Activity context, String[]web, String[]web2, Integer[] imageId,boolean[] isChecked){
super(context, R.layout.custom_item_listview_image_text_text, web);
this.context = context;
this.web = web;
this.web2 = web2;
this.imageId = imageId;
this.isChecked = isChecked;
}
@Override
public View getView ( int position, View view, ViewGroup parent){
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.custom_item_listview_image_text_text, null, true);
txtTitle = (TextView) rowView.findViewById(R.id.txt);
txtSubTitle = (TextView) rowView.findViewById(R.id.subText);
imageView = (ImageView) rowView.findViewById(R.id.img);
mySwitch = (SwitchCompat) rowView.findViewById(R.id.newswitch);
txtTitle.setText(web[position]);
txtSubTitle.setText(web2[position]);
imageView.setImageResource(imageId[position]);
mySwitch.setChecked(isChecked[position]);
if (position != 2) {
mySwitch.setVisibility(View.GONE);
}
return rowView;
}
这是设置的一部分
CustomListViewImageTextText adapter = new CustomListViewImageTextText(SettingsActivity.this, settingsArray, settingsArray2, imageId, isChecked);
myListView = (ListView) findViewById(R.id.settingItems);
myListView.setAdapter(adapter);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
})
答案 0 :(得分:1)
你无法得到这样的事件。您正在onItemClick初始化开关,因此它不会在那时触发。 如果您想在活动中获得检查更改事件,那么您可以使用静态方法。这样,当开关状态改变时,您可以从适配器调用该方法。
在您的活动中制作此方法:
public static isChecked(boolean flag){
Log.i("TAG,"isChecked:"+flag);
}
并将以下代码添加到适配器的getView方法:
mySwitch = (SwitchCompat) rowView.findViewById(R.id.newswitch);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
<your_activity_name>.isChecked(isChecked);
}
});
注意:如果有多个,请确保它没有帮助 listview中提供的开关。为此你必须通过传递做一些改变 boolean以及int(作为索引值)到 isCheckd 方法。
答案 1 :(得分:0)
<script src="Scripts/angular.min.js"></script>
<script src="Scripts/angular-resource.min.js"></script>
<script src="Scripts/angular-animate/angular-animate.min.js"></script>
<script src="Scripts/angular-aria/angular-aria.min.js"></script>
<script src="Scripts/angular-messages.min.js"></script>
<script src="Scripts/angular-material/angular-material.min.js"></script>
<script src="Scripts/angular-material-icons/angular-material-icons.min.js"></script>
<script src="app/model.js"></script>
<script src="app/controllers/main-controller.js"></script>
<script src="app/app.js"></script>
答案 2 :(得分:0)
首先在点击监听器上使用List方法项来获取项目点击,然后确定第三个项目已分配一些值或分配了标签(单击全屏幕),更改开关的状态或对其进行一些处理< / p>