我试着解决了几天令人沮丧的问题。 我的主要布局是Scrollview,我知道你无法在逻辑上滚动到Scrollview中的Listview。
但是我的Listview在我的NavigationView中,所以它应该?我有单独的问题:
我想要一个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