我有一个列表视图,它由来自单独布局文件的两个文本视图组成。我用一个BaseAdapter
从 JSON 文件构建列表。
我希望第一个文本视图(标题)可单击,如果单击它会显示第二个文本视图(文本),如果再次单击它会隐藏它。
当我使用onClick
(android:onClick="ClText"
)我收到错误。我想我应该使用一些onClickListener
,但由于我是 Android 新手,我不太确定如何使用它。
有人可以帮我解决代码吗?
您只需为扩展 BaseAdapter 的适配器类的 getView 方法中的第一项设置 onClickListener 即可。这是一个示例来说明您正在尝试执行的操作。
public class CustomAdapter extends BaseAdapter{
private ArrayList<Thing> mThingArray;
public CustomAdapter(ArrayList<Thing> thingArray) {
mThingArray = thingArray;
}
// Get the data item associated with the specified position in the data set.
@Override
public Object getItem(int position) {
return thingArray.get(position);
}
// Get a View that displays the data at the specified position in the data set.
// You can either create a View manually or inflate it from an XML layout file.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
// LayoutInflater class is used to instantiate layout XML file into its corresponding View objects.
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.one_of_list, null);
}
TextView captionTextView = (TextView) convertView.findViewById(R.id.caption);
TextView txt2 = (TextView)findViewById(R.id.text);
captionTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(txt2.getVisibility() == View.INVISIBLE){
txt2.setVisibility(View.VISIBLE);
} else {
txt2.setVisibility(View.INVISIBLE);
}
}
});
return convertView;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)