Android programming realizes the function of adding click event in item partial area of ListView

  • 2021-08-12 03:42:31
  • OfStack

This article describes the Android programming implementation of ListView item partial region add click event function. Share it for your reference, as follows:

Requirements such as title: Click events are added to some areas of item in Android listview, and listview is displayed in one interface, but the displayed content is divided into upper and lower parts, which are white background and blue background respectively. Now you need to only click on the blue background to jump to other interfaces. The solution is as follows:

1 I began to wonder if I could add one to the upper layout in the layout of list item:


android:clickable="false"
android:focusable="false"

It is forbidden to click, but it has no effect after trying. Later, the master reminded me that the adapter used by my listview is BaseAdapter, which can get the lower blue Relativelayout in the adapter, and then add click events to this layout, which can realize some regional response events. Facts have proved that it is very feasible. The main codes posted are as follows:


//  Warehouse receipt adapter 
public class OrderAdapter extends BaseAdapter {
 public OrderAdapter(Context myContext) {
 }
 public OrderAdapter(OrderFragment orderFragment) {
 }
 @Override
 public int getCount() {
  if (dataMap == null) {
   return orderVec.length;
  }
  return dataMap.size();
 }
 @Override
 public Object getItem(int position) {
  return position;
 }
 @Override
 public long getItemId(int position) {
  return position;
 }
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder viewHolder = null;
  if (convertView == null) {
   viewHolder = new ViewHolder();
   convertView = getActivity().getLayoutInflater().inflate(
     R.layout.activity_order_item, null);
   initViewHolder(convertView, viewHolder, position);
// Key code to get the layout of the lower blue layer id
   } else {
    viewHolder = (ViewHolder) convertView.getTag();
   }
   refreshViewHolder(position, viewHolder);
   return convertView;
  }
  private void refreshViewHolder(int position, ViewHolder viewHolder) {
   // . . . . . . . . 
  }
  private void initViewHolder(View convertView, ViewHolder viewHolder,
    final int position) {
   viewHolder.modeifyorder = (RelativeLayout) convertView
     .findViewById(R.id.modeifyorder);
   viewHolder.modeifyorder.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
     Map<String, Object> map = dataMap.get(position);
     long orderId = Long.parseLong(map.get(Orderid).toString());
     String instStr = map.get(Instrument).toString();
     DocCaptain.getInstance().setOrderTrade2Modify(instStr,
       orderId);
     getSelfActivity().showOrHideOrderTradeFragment();
    }
   });
   convertView.setTag(viewHolder);
  }
  private class ViewHolder {
   TextView orderid;
   TextView iFDStopPrice;
   RelativeLayout modeifyorder;
  }
}

Successful completion, record 1.

More readers interested in Android can check the topics of this site: "Summary of Android Control Usage", "Introduction and Advanced Tutorial of Android Development", "Summary of View Skills in Android View", "Summary of activity Operation Skills in Android Programming", "Summary of Android Database Operation Skills" and "Summary of Android Resource Operation Skills"

I hope this article is helpful to everyone's Android programming.


Related articles: