How to make android listview scrollable?

后端 未结 9 1346
悲&欢浪女
悲&欢浪女 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

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/listEmployeeDetails"
            android:layout_height="match_parent" 
            android:layout_width="match_parent"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_gravity="center"
            android:background="#ffffff">
    
            <TextView android:id="@+id/tvEmpId"
                          android:layout_height="wrap_content"
                          android:textSize="12sp"
                          android:padding="2dp"
                          android:layout_width="0dp"
                          android:layout_weight="0.3"/>
                <TextView android:id="@+id/tvNameEmp"
                          android:layout_height="wrap_content"
                          android:textSize="12sp"                     
                          android:padding="2dp"
                          android:layout_width="0dp"
                          android:layout_weight="0.5"/>
                 <TextView
                        android:layout_height="wrap_content"
                        android:id="@+id/tvStatusEmp"
                        android:textSize="12sp"
                        android:padding="2dp"
                        android:layout_width="0dp"
                        android:layout_weight="0.2"/>               
    </LinearLayout> 
    

    details.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/listEmployeeDetails"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/page_bg"
        android:orientation="vertical" >
        <LinearLayout
            android:id="@+id/lLayoutGrid"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/page_bg"
            android:orientation="vertical" >
    
            ................... others components here............................
    
            <ListView
                android:id="@+id/listView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:alwaysDrawnWithCache="true"
                android:dividerHeight="1dp"
                android:horizontalSpacing="3dp"
                android:scrollingCache="true"
                android:smoothScrollbar="true"
                android:stretchMode="columnWidth"
                android:verticalSpacing="3dp" 
                android:layout_marginBottom="30dp">
            </ListView>
        </LinearLayout>
    </RelativeLayout>
    

    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<EmployeeBean> employeeList; 
    
        publicListViewAdapter(Context context, List<EmployeeBean> 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<EmployeeBean> EMPLOYEE_LIST = new ArrayList<EmployeeBean>();
    
    //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));
    
    0 讨论(0)
  • 2021-01-03 20:08

    I know this question is 4-5 years old, but still, this might be useful:

    Sometimes, if you have only a few elements that "exit the screen", the list might not scroll. That's because the operating system doesn't view it as actually exceeding the screen.

    I'm saying this because I ran into this problem today - I only had 2 or 3 elements that were exceeding the screen limits, and my list wasn't scrollable. And it was a real mystery. As soon as I added a few more, it started to scroll.

    So you have to make sure it's not a design problem at first, like the list appearing to go beyond the borders of the screen but in reality, "it doesn't", and adjust its dimensions and margin values and see if it's starting to "become scrollable". It did, for me.

    0 讨论(0)
  • 2021-01-03 20:12

    I found a tricky solution... which works only in a RelativeLayout. We only need to put a View above a ListView and set clickable 'true' on View and false for the ListView

     <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listview
        android:clickable="false" />
    
    <View
        android:layout_width="match_parent"
        android:background="@drawable/gradient_white"
        android:layout_height="match_parent"
        android:clickable="true"
        android:layout_centerHorizontal="true"
        android:layout_alignTop="@+id/listview" />
    
    0 讨论(0)
提交回复
热议问题