How to make android listview scrollable?

后端 未结 9 1395
悲&欢浪女
悲&欢浪女 2021-01-03 19:12

I have two listviews, but they don\'t scroll. How do I correct this?

Here is my layout.xml

 

        
9条回答
  •  既然无缘
    2021-01-03 20:01

    This is my working code. you may try with this.

    row.xml

    
    
    
            
                
                                
     
    

    details.xml

    
        
    
            ................... others components here............................
    
            
            
        
    
    

    Adapter class :

    import java.util.List;
    
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Color;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    
    public class ListViewAdapter extends BaseAdapter {
        private Context context;
        private List employeeList; 
    
        publicListViewAdapter(Context context, List employeeList) {
                this.context = context;
                this.employeeList = employeeList;
            }
    
        public View getView(int position, View convertView, ViewGroup parent) {
                View row = convertView;
                EmployeeBeanHolder holder = null;
                LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                row = inflater.inflate(R.layout.row, parent, false);
    
                holder = new EmployeeBeanHolder();
                holder.employeeBean = employeeList.get(position);
                holder.tvEmpId = (TextView) row.findViewById(R.id.tvEmpId);
                holder.tvName = (TextView) row.findViewById(R.id.tvNameEmp);
                holder.tvStatus = (TextView) row.findViewById(R.id.tvStatusEmp);
    
                row.setTag(holder);
                holder.tvEmpId.setText(holder.employeeBean.getEmpId());
                holder.tvName.setText(holder.employeeBean.getName());
                holder.tvStatus.setText(holder.employeeBean.getStatus());
    
                 if (position % 2 == 0) {
                        row.setBackgroundColor(Color.rgb(213, 229, 241));
                    } else {                    
                        row.setBackgroundColor(Color.rgb(255, 255, 255));
                    }        
    
                return row;
            }
    
       public static class EmployeeBeanHolder {
            EmployeeBean employeeBean;
            TextView tvEmpId;
            TextView tvName;
            TextView tvStatus;
        }
    
        @Override
        public int getCount() {
                return employeeList.size();
            }
    
        @Override
        public Object getItem(int position) {
                return null;
            }
    
        @Override
        public long getItemId(int position) {
                return 0;
        }
    }
    

    employee bean class:

    public class EmployeeBean {
        private String empId;
        private String name;
        private String status;
    
        public EmployeeBean(){      
        }
    
        public EmployeeBean(String empId, String name, String status) {
            this.empId= empId;
            this.name = name;
            this.status = status;
        }
    
        public String getEmpId() {
            return empId;
        }
    
        public void setEmpId(String empId) {
            this.empId= empId;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getStatus() {
            return status;
        }
    
        public void setStatus(String status) {
            this.status =status;
        }
    }
    

    in Activity class:

    onCreate method:

    public static List EMPLOYEE_LIST = new ArrayList();
    
    //create emplyee data
    for(int i=0;i<=10;i++) {
      EmployeeBean emplyee = new EmployeeBean("EmpId"+i,"Name "+i, "Active");
      EMPLOYEE_LIST .add(emplyee );
    }
    
    ListView listView;
    listView = (ListView) findViewById(R.id.listView);
    listView.setAdapter(new ListViewAdapter(this, EMPLOYEE_LIST));
    

提交回复
热议问题