【问题标题】:Android - Listview not showing the last itemAndroid - Listview 不显示最后一项
【发布时间】:2014-03-17 15:31:44
【问题描述】:

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

<?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);
}



}

提前致谢。

【问题讨论】:

  • 没有代码很难说。可能你没有调用 adapter.notifyDataSetChanged()。
  • 是的,我添加了代码。查看我的编辑@smittey

标签: android android-layout listview android-listview


【解决方案1】:

将此行添加到您的 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" />

【讨论】:

    【解决方案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>
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        也许它非常简单。尝试将 xml 文件中列表视图的 layout_height 设置为 wrap_content。

        android:layout_height="wrap_content"
        

        【讨论】:

          【解决方案5】:

          尝试将第二个LinearLayout 保持Listview 设置为match_parent 高度...看起来您的LinearLayout 正在随着ListView (wrap_content) 超出您的屏幕空间而增长

          【讨论】:

          • 如果您只删除列表视图周围的线性布局会更好。没用。
          • @yekretsaM 我尝试将其更改为 match_parent 仍然没有显示一项。
          • @Barışcan Kayaoğlu 我还尝试删除列表视图周围的衬里布局。但还是没用。
          • 我设法按特定高度显示所有项目,但它会与所有 android 设备兼容吗?我设置了 android:layout_height="391dp" 及其工作
          猜你喜欢
          • 2019-10-31
          • 1970-01-01
          • 1970-01-01
          • 2017-01-25
          • 2017-10-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多