问题
I have a listview that contains two buttons, a text view and...
I want to set onclicklistener for one button and a textview to do whatever that I set in onitemclicklistener for this listview. but I don't know how can I do that.
these are my lists.xml codes
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_gravity="center_horizontal"
android:padding="15dp"
android:background="@android:drawable/dialog_holo_light_frame"
android:focusable="false"
android:focusableInTouchMode="false">
<FrameLayout
android:layout_width="132dp"
android:layout_height="120dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:background="@drawable/company1"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:clickable="true"
android:id="@+id/frameLayout">
</FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/id"
android:visibility="gone"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="35dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/company_name"
android:id="@+id/header_factory_list"
android:gravity="right"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="#21313f"
android:textSize="15dp"
android:singleLine="true"/>
<RatingBar
style="@android:style/Widget.DeviceDefault.Light.RatingBar.Small"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ratingBar2"
android:animationResolution="3"
android:rating="2.5"
android:stepSize=".05"
android:layout_marginLeft="20dp"/>
<Button
android:layout_width="fill_parent"
android:layout_height="25dp"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="@string/add_fav_list"
android:id="@+id/button_add_fav_list"
android:background="#cf000f"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:textColor="#ffffff"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:padding="2dp"
android:textSize="10dp"/>
<Button
android:layout_width="fill_parent"
android:layout_height="25dp"
android:focusable="false"
android:text="@string/show_details"
android:id="@+id/button_detsils"
android:background="#21313f"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:textColor="#ffffff"
android:layout_marginBottom="5dp"
android:padding="2dp"
android:textSize="10dp"/>
</LinearLayout>
</LinearLayout>
also these are my onitemclicklistener codes from mainactivity:
Inside_Factory_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Inside_Factory_ListView.setVisibility(View.GONE);
TextView f= (TextView) view.findViewById(R.id.id);
Log.i("GETVIEW",view.getId()+"\n"+f.getText().toString());
setInfoInFactory(Integer.parseInt(f.getText().toString()));
Inside_Factory_Layout.setVisibility(View.VISIBLE);
final TranslateAnimation animation1 = new TranslateAnimation(0.0f, 0.0f,
3000.0f, 0.0f); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
animation1.setDuration(1500); // animation duration
animation1.setRepeatCount(0); // animation repeat count
animation1.setRepeatMode(0); // repeat animation (left to right, right to left )
//animation.setFillAfter(true);
Handler mHandler = new Handler();
Frist_Tab.setVisibility(View.VISIBLE);
Frist_Tab.startAnimation(animation1);
mHandler.postDelayed(new Runnable() {
public void run() {
Second_Tab.setVisibility(View.VISIBLE);
Second_Tab.startAnimation(animation1);
}
}, 1000);
mHandler.postDelayed(new Runnable() {
public void run() {
Third_Tab.setVisibility(View.VISIBLE);
Third_Tab.startAnimation(animation1);
}
}, 1000);
// Log.i("CLICK","CLICKED="+position);
if(L[1]){
L[2]=true;
}
}
});
I want, when I click on the button(into the listview) happens like i click on that item of list view.
Can anyone please help me?
回答1:
You can use an interface for handling click event of those Buttons
and TextView
. You have to set all focusable items'(like Button, RatingBar, EditText, Spinner etc.) focus to false.
I am giving you a code snippet
Adapter code
public class PropertyListAdapter extends ArrayAdapter<PropertyInfo> {
private Context _context;
private int layoutId;
private List<PropertyInfo> dataList;
private ListItemBtnClickInterface _interface;
public PropertyListAdapter(Context context, int resource,
List<PropertyInfo> objects, ListItemBtnClickInterface _interface) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
_context = context;
layoutId = resource;
dataList = objects;
this._interface = _interface;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder viewHolder;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) _context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(layoutId, parent, false);
viewHolder = new ViewHolder();
viewHolder.propertyImgView = (ImageView) convertView.
findViewById(R.id.propertyImgView);
viewHolder.editImgView = (ImageView) convertView.
findViewById(R.id.editImgView);
viewHolder.deleteImgView = (ImageView) convertView.
findViewById(R.id.deleteImgView);
viewHolder.propertyNameTxtView = (TextView) convertView.
findViewById(R.id.propertyNameTxtView);
viewHolder.ownerTypeTxtView = (TextView) convertView.
findViewById(R.id.ownerTypeTxtView);
viewHolder.checkBox1 = (CheckBox) convertView.
findViewById(R.id.checkBox1);
viewHolder.propertyNameTxtView.setTypeface(Utils.getRegularTypeface(_context));
viewHolder.ownerTypeTxtView.setTypeface(Utils.getRegularTypeface(_context));
viewHolder.checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
PropertyInfo element = (PropertyInfo) viewHolder.checkBox1
.getTag();
element.setSelected(buttonView.isChecked());
}
});
convertView.setTag(viewHolder);
viewHolder.checkBox1.setTag(dataList.get(position));
} else {
viewHolder = (ViewHolder)convertView.getTag();
((ViewHolder) convertView.getTag()).checkBox1.setTag(dataList.get(position));
}
ViewHolder holder = (ViewHolder) convertView.getTag();
holder.checkBox1.setChecked(dataList.get(position).isSelected());
int height = viewHolder.propertyImgView.getLayoutParams().height;
int width = viewHolder.propertyImgView.getLayoutParams().width;
viewHolder.deleteImgView.setTag(position);
viewHolder.editImgView.setTag(position);
viewHolder.deleteImgView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Object data = v.getTag();
if(data != null) {
_interface.listItemBtnClickListener(data, v.getId());
}
}
});
viewHolder.editImgView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Object data = v.getTag();
if(data != null) {
_interface.listItemBtnClickListener(data, v.getId());
}
}
});
PropertyInfo addPropertyInfo = dataList.get(position);
String propertyName = "";
String ownerType = "";
String imgPath = "";
propertyName = addPropertyInfo.getPropertyName();
ownerType = addPropertyInfo.getOwnerTypeName();
imgPath = addPropertyInfo.getImagePath();
viewHolder.propertyNameTxtView.setText(propertyName);
viewHolder.ownerTypeTxtView.setText(ownerType);
if(imgPath != null && !imgPath.equalsIgnoreCase("")) {
Uri uri = Uri.parse(imgPath);
Picasso.with(_context).load(uri)
.resize(width, height).centerCrop().into(viewHolder.propertyImgView);
} else {
viewHolder.propertyImgView.setImageResource(R.drawable.no_img);
}
return convertView;
}
private class ViewHolder {
ImageView propertyImgView;
ImageView editImgView;
ImageView deleteImgView;
TextView propertyNameTxtView;
TextView ownerTypeTxtView;
CheckBox checkBox1;
}
}
Interface for handling click event
public interface ListItemBtnClickInterface {
public void listItemBtnClickListener(Object obj, int viewId);
}
setting adapter and click event of child views
propertyList = dbHelper.getAllProperties();
dbHelper.closeDB();
if(propertyList != null && propertyList.size() > 0) {
noPropertyTxtView.setVisibility(View.GONE);
adapter = new PropertyListAdapter(
PropertyListActivity.this,
R.layout.row_property, propertyList, new ListItemBtnClickInterface() {
private int clickedPosition;
@Override
public void listItemBtnClickListener(Object obj, int viewId) {
// TODO Auto-generated method stub
clickedPosition = Integer.parseInt(obj.toString());
PropertyInfo addPropertyInfo = propertyList.get(clickedPosition);
final long propertyId = addPropertyInfo.getId();
switch (viewId) {
case R.id.editImgView:
Intent intent = new Intent(PropertyListActivity.this,
AddPropertyActivity.class);
intent.putExtra(PortfolioManagementApplication.KEY_PROPERTY_ID,
propertyId);
startActivity(intent);
break;
case R.id.deleteImgView:
new AlertDialog.Builder(PropertyListActivity.this)
.setTitle(getResources().getString(R.string.delete_property_title))
.setIcon(R.drawable.delete_icon)
.setMessage(R.string.delete_property_msg).setCancelable(true)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dbHelper.deleteProperty(propertyId);
dbHelper.closeDB();
propertyList.remove(clickedPosition);
adapter.notifyDataSetChanged();
if (propertyList.size() == 0)
noPropertyTxtView.setVisibility(View.VISIBLE);
else
noPropertyTxtView.setVisibility(View.GONE);
}
}).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
break;
default:
break;
}
}
});
propertyListView.setAdapter(adapter);
propertyListView.setOnItemClickListener(this);
} else {
noPropertyTxtView.setVisibility(View.VISIBLE);
}
OnItemClick of ListView
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
PropertyInfo addPropertyInfo = propertyList.get(position);
long propertyId = addPropertyInfo.getId();
Intent intent = new Intent(PropertyListActivity.this,
AddPropertyActivity.class);
intent.putExtra(PortfolioManagementApplication.KEY_PROPERTY_ID,
propertyId);
startActivity(intent);
}
add these attributes in focusable
item
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
来源:https://stackoverflow.com/questions/31666797/how-can-i-set-button-clicklistener-in-to-list-view-to-work-like-onitemclickliste