Android - Listview没有显示最后一项

时间:2014-03-17 15:31:44

标签: android android-layout listview android-listview

我的问题是列表视图中的最后一项没有显示,即使它已被添加到适配器以及列表中,因为我可以看到适配器中的项目数量正常。这是布局的问题吗?这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/golive_bg"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/Room"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus android:layout_width="wrap_content" />

    </EditText>

    <Button
        android:id="@+id/Search1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Search" />

    <ImageButton
        android:id="@+id/CreateRoomimageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/closed_doorplus" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
        <ListView
            android:id="@+id/listRooms"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fastScrollEnabled="true"
            android:transcriptMode="alwaysScroll">
        </ListView>
</LinearLayout>

</LinearLayout>

这是我的课堂活动

public class chatRoomsListActivity extends Activity implements OnScrollListener  {

private static String Password;
private ListView lv;
private static Collection<HostedRoom> rooms;
private static ArrayList<String> Roomlist = new ArrayList<String>();
private static Handler mHandler = new Handler();
private Button SearchButton;
private ImageButton CreateButton;
private EditText EditTextSearch;
private String Search;
private String RoomName;
private static ChatRoomListAdapter adapter;
private static String Nickname="";

@Override 
public void onCreate(Bundle savedInstanceState)
        {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.chat_rooms);

            SearchButton=(Button)findViewById(R.id.Search1);
            CreateButton=(ImageButton)findViewById(R.id.CreateRoomimageButton);
            lv = (ListView) findViewById(R.id.listRooms);        

            adapter = new ChatRoomListAdapter(getApplicationContext(),  
R.layout.chat_rooms_wrapper);
            lv.setAdapter(adapter);
            //ScrollView.measureChildWithMargins();

            try {
            populate_RoomList();
        } catch (XMPPException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }     
          addListenerOnButton();


/*   
             arrayAdapter = new ArrayAdapter<String>(
                     this,android.R.layout.simple_list_item_1,Roomlist);            
             lv.setAdapter(arrayAdapter);   
             */


             lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parnet,    
android.view.View view,
                            int position, long id) {

                    setRoomName(ChatRoomListAdapter.getItemName(position));
                    showOptionDialog();

                    }
                });      
            addListenerOnButton();

        }

public void addListenerOnButton() {      
            final Context context = this;
                CreateButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    createRoomDialog();                 
                }

            }); 

            SearchButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {

                     EditTextSearch=(EditText) findViewById(R.id.Room);
                     Search=EditTextSearch.getText().toString();
                    //Search=Search.concat("*");

                         try {
                            // populate_RoomList();
                             getSearchlist();
                            // updateList();
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                }

            });
        }

public void getSearchlist() throws XMPPException{

        Roomlist.clear();
        adapter.clear();
        ChatRoomListAdapter.getChatRoom().clear();

        rooms=getHostedRooms(MyService.getConnection(),"conference."+  
MyService.getDomain());

              System.out.println("in getsearch");
            for (HostedRoom h : rooms)
            {
                System.out.println(h.getName().toString()+" contain " +Search);
                if(h.getName().toString().contains(Search)){
                System.out.println("roomlist= " +h.getJid().toString());

                Roomlist.add(h.getName());


System.out.println("ChatRooms.IsRoomPassProtected(h.getName())==   
"+ChatRooms.IsRoomPassProtected(h.getName()));

                if(ChatRooms.IsRoomPassProtected(h.getName())==true){
                    adapter.add(new ChatRoom(h.getName(),true));
                }
                else if(ChatRooms.IsRoomPassProtected(h.getName())==false){
                    adapter.add(new ChatRoom(h.getName(),false));
                 }

                  }
            }
            System.out.println("Adapter count="+adapter.getCount());
        }


private void populate_RoomList() throws XMPPException {

            //chatRoomsList.getRooms();"
            Roomlist.clear();
            adapter.clear();
            //rooms.clear();
            ChatRoomListAdapter.getChatRoom().clear();

            rooms=getHostedRooms(MyService.getConnection(),"conference."+  
MyService.getDomain());
            //System.out.println(rooms.iterator().next().getName());

            for (HostedRoom h : rooms)
            {
                System.out.println("roomlist= " +h.getName().toString());
                Roomlist.add(h.getName());

System.out.println("ChatRooms.IsRoomPassProtected(h.getName())==   
"+ChatRooms.IsRoomPassProtected(h.getName()));

                if(ChatRooms.IsRoomPassProtected(h.getName())==true){
                    adapter.add(new ChatRoom(h.getName(),true));
                }
                else if(ChatRooms.IsRoomPassProtected(h.getName())==false){
                    adapter.add(new ChatRoom(h.getName(),false));
                }

            }

            System.out.println("Adapter count="+adapter.getCount());

        }



public void setRoomName(String roomName) {
            RoomName = roomName;
        }

public static Collection<HostedRoom> getHostedRooms(Connection connection, String serviceName)
                throws XMPPException {
            List<HostedRoom> answer = new ArrayList<HostedRoom>();
            ServiceDiscoveryManager discoManager = new       
ServiceDiscoveryManager(connection);
            DiscoverItems items = discoManager.discoverItems(serviceName);
            for (Iterator<DiscoverItems.Item> it = items.getItems(); it.hasNext();) {
                answer.add(new HostedRoom(it.next()));
            }
            return answer;
        }   

}

这是我的自定义适配器

public class ChatRoomListAdapter extends ArrayAdapter<ChatRoom> {

private TextView name;
private static List<ChatRoom>  ChatRoom = new ArrayList<ChatRoom>();
private LinearLayout wrapper;
private ImageView lock;
public ChatRoomListAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);
}


public static List<ChatRoom> getChatRoom() {
    return ChatRoom;
}


public static void setChatRoom(List<ChatRoom> chatRoom) {
    ChatRoom = chatRoom;
}


public ChatRoom getItem(int index) {
    return ChatRoomListAdapter.ChatRoom.get(index);
}

public static String getItemName(int index) {
    return ChatRoomListAdapter.ChatRoom.get(index).name;
}


public static void setfriends(List<Friends> friends) {
    FriendListAdapter.setFriends(friends);
}

public void add(ChatRoom object) {
System.out.println("IN Chat room list adapter "+" addded");
ChatRoomListAdapter.ChatRoom.add(object);
    super.add(object);

}

public void remove(ChatRoom object) {
    ChatRoom.remove(object);
    super.remove(object);
}

public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    if (row == null) {
        LayoutInflater inflater = (LayoutInflater)     
this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.chat_rooms_wrapper, parent, false);
    }

    wrapper = (LinearLayout) row.findViewById(R.id.C_R_wrapper);

    name = (TextView) row.findViewById(R.id.C_R_RoomName);
    lock=(ImageView)row.findViewById(R.id.C_R_Lock);

    try {
         Log.d("Chat room adapter", "heere");
         ChatRoom CR= getItem(position);
         System.out.println("name= "+CR.name);
         name.setText(CR.name);

            if(CR.lock==true)
            {
                lock.setImageResource(R.drawable.lock_lock_icon);
            }else{
                lock.setImageResource(R.drawable.lock_unlock_icon);
            }


    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   



return row;

}


public Bitmap decodeToBitmap(byte[] decodedByte) {
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}



}

提前致谢。

5 个答案:

答案 0 :(得分:3)

将此行添加到您的Viewpager

android:layout_marginBottom="?attr/actionBarSize"

因为您底部没有的尺寸与工具栏的尺寸完全相同

使用RecyclerView的示例

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view_trips"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:layout_marginBottom="?attr/actionBarSize"/>

Viewpager的示例

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager_trip_history"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="?attr/actionBarSize" />

答案 1 :(得分:2)

试试这个你可以解决你的问题:

您只需要更改您的xml文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/golive_bg"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="6" >

    <EditText
        android:id="@+id/Room"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10" 
        android:layout_weight="4">

        <requestFocus android:layout_width="wrap_content" />

    </EditText>

    <Button
        android:id="@+id/Search1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Search"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/CreateRoomimageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/closed_doorplus"
        android:layout_weight="1" />
</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >
        <ListView
            android:id="@+id/listRooms"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fastScrollEnabled="true"
            android:transcriptMode="alwaysScroll">
        </ListView>
</RelativeLayout>

</LinearLayout>

答案 2 :(得分:0)

尝试设置第二个LinearLayout保持Listviewmatch_parent高度......看起来LinearLayout正在与ListView增长wrap_content {1}})离开屏幕空间

答案 3 :(得分:0)

您可以获取适配器的getView内的最后一项,并通过setLayoutParams更改布局参数高度。但减号是您必须更正其他项目的高度,因为向上/向下滚动列表时高度会发生变化

答案 4 :(得分:0)

也许很简单。尝试将xml文件中listview的layout_height设置为wrap_content。

android:layout_height="wrap_content"