DrawerLayout中的ListView问题

时间:2015-10-06 09:32:36

标签: android xml listview android-listview navigation-drawer

我试着解决了几天令人沮丧的问题。 我的主要布局是Scrollview,我知道你无法在逻辑上滚动到Scrollview中的Listview。

但是我的Listview在我的NavigationView中,所以它应该?我有单独的问题:

  • 如果我把" fill_parent"或" match_parent"在我的" menu_slide.xml"的第一个RelativeLayout里面;我的Listview只显示第一个元素,无论我放入多少内容。如果我给他们一个可能1000dp的修复高度,我的listview可以显示其中的每个新对象。
  • 其次,无论我的Listview中有多少元素,我都无法滚动它们。 (NavigationView是一个scrollview?)
  • 最后一个,如果我输入我的搜索栏来过滤我的Listview,它会显示正确的元素数量,但它显示了例如,如果有两个匹配的元素,我的Listview的前两个元素不是两个匹配元素。一切都错了。

我想要一个Listview,它可以保存我所放置的每个对象。与我的屏幕具有相同高度的NavigationDrawer拥有该列表视图,希望可滚动。

以下是我的代码:

activity_main.xml:(里面的NavigationDrawer)

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <ScrollView
        android:id="@+id/vscroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:context=".MainActivity">

            <RelativeLayout...>

            <LinearLayout...>

            <RelativeLayout...>

</RelativeLayout>
    </ScrollView>

    <android.support.design.widget.NavigationView
        android:id="@+id/list_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        app:headerLayout="@layout/menu_slide"
        />

</android.support.v4.widget.DrawerLayout>

menu_slide.xml:(里面有ListView和我的搜索栏)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_list"
    android:orientation="vertical"
    android:layout_width="300dp"
    android:layout_height="1000dp" <!-- If I put "match_- or fill_parent inside, my ListView shows only one object.-->
    android:gravity="top|center">

    <FrameLayout
        android:id="@+id/search"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/searchfield"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:singleLine="true" >
            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/searchBtn"
            android:layout_width="25dp"
            android:layout_height="25dp" />
    </FrameLayout>

    <ListView
        android:id="@+id/alist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:choiceMode="singleChoice"
        android:layout_below="@+id/search"
        android:background="@color/whitePrimary">
    </ListView>

    <TextView
        android:id="@+id/listText"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/circle"/>
</RelativeLayout>

此按钮将元素放入我的列表中:(在我的activity_main.xml中)

        bMyList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(myActivities.contains(thisActivity)){
                    ...Toast
                } else {
                    myActivities.add(thisActivity);
                    String[] row = new String[myActivities.size()];
                    for (int i = 0; i < row.length; i++) {
                        row[i] = myActivities.get(i).getShortTitle();
                    }
                    adapter = new CustomList(MainActivity.this, myActivities, row);
                    vList.setAdapter(adapter);
                }
            }
        });

这是我的搜索栏监听器:(在我的NavigationDrawer中)

searchfield.addTextChangedListener(new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    MainActivity.this.adapter.getFilter().filter(s);
                }
            });

这是我的CustomAdapter:(将我的对象放入Listview)

public class CustomList extends ArrayAdapter<String>{

    private final Activity context;
    private final ArrayList<NextActivity> listActivities;

    public CustomList(Activity context, ArrayList<NextActivity> allactivities, String[] test) {
        super(context, R.layout.list_layout, test);
        this.context = context;
        this.listActivities = allactivities;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_layout, null, true);

        TextView title = (TextView) rowView.findViewById(R.id.title);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.list_activity_icon);


        title.setText(listActivities.get(position).getShortTitle());
        Picasso.with(context).load(listActivities.get(position).getIconSmallURL())
                .error(R.drawable.activity_icon_small_placeholder)
                .placeholder(R.drawable.activity_icon_small_placeholder)
                .into(imageView);

        return rowView;
    }

}

这是我想找的: https://www.dropbox.com/s/mup13ww79aq1e4j/Screenshot_2015-10-03-19-45-58.png?dl=0

0 个答案:

没有答案