在父单击的展开列表中,我有一个孩子,其中有 3 个文本视图,例如,当我单击不同的文本视图时,我想触发不同的事件。
for text view 1 output is hello
for text view 2 output is hi
for text view 3 output is heya
在父单击的展开列表中,我有一个孩子,其中有 3 个文本视图,例如,当我单击不同的文本视图时,我想触发不同的事件。
for text view 1 output is hello
for text view 2 output is hi
for text view 3 output is heya
正如edwin所说,您可以制作一个服装适配器。您可以在其中对每个视图设置 OnClickListner() 方法。就像我在这里做的一样..
class CustomAdapter extends ArrayAdapter<Contact>
{
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public CustomAdapter(Context context, int textViewResourceId,
ArrayList<Contact> strings) {
//let android do the initializing :)
super(context, textViewResourceId, strings);
}
//class for caching the views in a row
private class ViewHolder
{
TextView id,name,phn_no;
}
ViewHolder viewHolder;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
//inflate the custom layout
convertView=inflater.inflate(R.layout.activity_main, null);
viewHolder=new ViewHolder();
//cache the views
viewHolder.id=(TextView) convertView.findViewById(R.id.contact_id_txt);
viewHolder.name=(TextView) convertView.findViewById(R.id.contact_name_txt);
viewHolder.phn_no=(TextView) convertView.findViewById(R.id.contact_ph_no_txt);
viewHolder.id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Hi!!", Toast.LENGTH_SHORT).show();
}
});
viewHolder.name.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Hello!!", Toast.LENGTH_SHORT).show();
}
});
viewHolder.phn_no.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Heya!!!", Toast.LENGTH_SHORT).show();
}
});
//link the cached views to the convertview
convertView.setTag(viewHolder);
}
else
viewHolder=(ViewHolder) convertView.getTag();
//set the data to be displayed
viewHolder.id.setText(contacts.get(position).get_id()+"");
viewHolder.name.setText(contacts.get(position).get_name());
viewHolder.phn_no.setText(contacts.get(position).get_phn_no());
//return the view to be displayed
return convertView;
}
}
ExpandableListView expandableListView;
expandableListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
//////////////////////////Change your child text values here.
return false;
}
});
试试这种方式:
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater infalInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.layout, null); } tv_ChildFieldId = (TextView) convertView.findViewById(R.id.tv1); tv_ChildFieldId.setText(getChildIdFromDB(groupPosition, childPosition).toString()); tv_ChildFieldId.setOnClickListener(new ID_OnClickListener()); //And so on return convertView; } /**Onclick Listener for the Textview*/ private class ID_OnClickListener implements OnClickListener { @Override public void onClick(View v) { } });
因此,您必须为可扩展列表创建自定义适配器。然后onClickListener
在自定义适配器类中为您的不同小部件接口实现
在您的自定义适配器类中
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_layout, null);
}
//Give your On click Implementations Here
return convertView;
}
实施您的活动OnChildClickListener
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(MainActivity.this, "Clicked On Child",
Toast.LENGTH_SHORT).show();
return true; //or false
}
看看这里 您可以定义您的自定义可扩展适配器
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
在此方法中,您可以确定相对于父级的子级位置并设置所需的 oc click listener
试试这个它会有所帮助
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if (groupPosition == 0){
if (childPosition == 0){
Intent intent = new Intent(MarketJaipur.this, ClothesMarket.class);
startActivity(intent);
}
}
return false;
}
});
上面@edwin 的方法失败了!在活动中实现ExpandableListView.onchildItemClick不会发出任何调用。
如果您有自定义且复杂的 ChildView,则该问题会特别出现。
对我来说,它通过在convertView本身上设置一个 onclickListener来工作。我从适配器内部获得了完美的点击。
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(_context, groupPosition + " clicked "
+ childPosition, Toast.LENGTH_SHORT).show();
}
});
确保你把它写在外面
if(convertView==null){.....layout inflation}
插入语。