Bottomsheet ListView仅显示1项

时间:2017-08-29 10:38:48

标签: android listview android-cursoradapter bottom-sheet

我创建了一个带底页的应用程序,该应用程序使用带有列表视图的底页。

以前,我使用recyclerView来显示底部工作表内的数组列表中的列表项。现在,我通过从游标中检索数据,将应用程序更改为使用listView

现在,底部工作表只显示一个项目:

Emulator Image

即使在Android工作室预览中,我也只能看到1个列表项:

我创建了一个带有单个列表视图的测试活动,并且显示了所有数据,除了每个第3项都是"扭曲"

Emulator Image

以下是布局活动的代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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/coordinateLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    tools:context=".MainActivity"

    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:text="EventList Button"
            android:id="@+id/eventlistbutton"
            android:layout_alignParentBottom="true"
            />
        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/contactsbutton"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_margin="12dp"
            android:id="@+id/contactsbutton"
            android:clickable="true"
            app:backgroundTint="@color/cardbackground2"
            app:fabSize="normal"
            />
        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/zoomunpressed"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_margin="12dp"
            android:id="@+id/zoomOut"
            android:clickable="true"
            app:backgroundTint="@color/cardbackground2"
            app:fabSize="normal"
            app:borderWidth="-120000dp"
            />
        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/zoom2unpressed"
            android:id="@+id/zoomIn"
            android:clickable="true"
            app:backgroundTint="@color/cardbackground2"
            app:fabSize="normal"
            app:borderWidth="-120000dp"
            app:layout_anchor="@id/zoomOut"
            android:layout_marginLeft="12dp"
            android:layout_marginRight="12dp"
            android:layout_below="@+id/zoomOut"
            />
        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/messageunpressed"
            android:id="@+id/chatbutton"
            android:clickable="true"
            app:backgroundTint="@color/cardbackground2"
            app:fabSize="normal"
            android:layout_marginRight="12dp"
            android:layout_below="@+id/contactsbutton"
            android:layout_alignStart="@+id/contactsbutton" />
    </RelativeLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/bottom_sheet_behavior"
    android:clipToPadding="true"
    android:orientation="vertical"
    android:id="@+id/bottomsheet"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/cardbackground"
        android:gravity="center"
        android:text="Event List"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:textSize="18sp"
        />
    <android.support.v4.widget.NestedScrollView

        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:background="@color/recyclerview_background"
        >
            <ListView
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:id="@+id/eventlistview"
                android:paddingLeft="@dimen/recyclerviewhorizontalpadding"
                android:paddingRight="@dimen/recyclerviewhorizontalpadding"
                >
            </ListView>
    </android.support.v4.widget.NestedScrollView>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>

以下是Listview项目的代码:

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/CardViewItem"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardBackgroundColor="@color/cardbackground2"
    card_view:cardCornerRadius="@dimen/cardviewcorner"
    card_view:cardElevation="8dp"
    >
    <!--android:layout_marginTop="8dp"-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/cardbackground"
            android:orientation="horizontal"
            >
            <TextView
                android:id="@+id/event_name"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:padding="@dimen/cardmargin"
                android:textColor="@color/white"
                android:textSize="18sp"
                android:textStyle="bold"
                tools:text="Museum Tour"
                />
            <TextView
                android:id="@+id/event_date"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:padding="@dimen/cardmargin"
                android:textAlignment="viewEnd"
                android:textColor="@color/white"
                android:textSize="18dp"
                android:textStyle="bold"
                tools:text="8/24"
                />

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/card_view_height"
            android:background="@color/cardbackground2"
            android:orientation="vertical"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal"
                >
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:paddingLeft="@dimen/cardmargin"
                    android:textColor="@color/white"
                    android:textSize="18sp"
                    android:text="Time:"
                    android:textStyle="bold"
                    />
                <TextView
                    android:id="@+id/event_time"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_marginRight="18dp"
                    android:layout_weight="3"
                    android:gravity="center_vertical"
                    android:paddingLeft="@dimen/cardmargin"
                    android:textAlignment="textEnd"
                    android:textColor="@color/white"
                    android:textSize="18sp"
                    tools:text="11:50"
                    />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"

                    android:clickable="true"
                    android:gravity="center_vertical"
                    android:paddingLeft="@dimen/cardmargin"
                    android:textColor="@color/white"
                    android:textSize="18sp"
                    android:text="Summary:"
                    android:textStyle="bold"
                    />
                <TextView
                    android:id="@+id/event_summary"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:clickable="true"
                    android:ellipsize="end"
                    android:gravity="center_vertical"
                    android:maxLength="130"
                    android:maxLines="1"
                    android:paddingLeft="@dimen/cardmargin"
                    android:layout_marginRight="18dp"
                    android:text="Once upon a time  a few missadfsafasdfasfdf"
                    android:textColor="@color/white"
                    android:textSize="18sp"
                    />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:clickable="true"
                    android:gravity="center_vertical"
                    android:paddingLeft="@dimen/cardmargin"
                    android:textColor="@color/white"
                    android:textSize="18sp"
                    android:text="Participants:"
                    android:textStyle="bold"
                    />
                <TextView
                    android:id="@+id/event_participantNum"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="2"
                    android:clickable="true"
                    android:gravity="center_vertical"
                    android:paddingLeft="@dimen/cardmargin"
                    android:layout_marginRight="18dp"
                    android:text="80"
                    android:textAlignment="viewEnd"
                    android:textColor="@color/white"
                    android:textSize="18sp"
                    />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:clickable="true"
                    android:gravity="center_vertical"
                    android:paddingLeft="@dimen/cardmargin"
                    android:textColor="@color/white"
                    android:textSize="18sp"
                    android:text="Location:"
                    android:textStyle="bold"
                    />
                <TextView
                    android:id="@+id/event_location"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="2"
                    android:clickable="true"
                    android:gravity="center_vertical"
                    android:paddingLeft="@dimen/cardmargin"
                    android:layout_marginRight="18dp"
                    android:text="Rio de Janeiro"
                    android:textAlignment="viewEnd"
                    android:textColor="@color/white"
                    android:textSize="18sp"
                    />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

最后,这是主要活动的代码:

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

    Button eventListButton;
    public static BottomSheetBehavior mBottomsheetbehvior;
    RecyclerView rv;
    LinearLayoutManager llm;
    Context context;
    public static GoogleMap mMap;
    FloatingActionButton contactsbutton;
    FloatingActionButton testButton;
    Intent contactsintent;
    Intent testIntent;

    DatabaseHelper eventsDBHelper;
    EventListCursorAdapter eventListCursorAdapter;
    Cursor eventCursor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDataHolder dataHolder = new myDataHolder();
        context = this;

        eventsDBHelper = new DatabaseHelper(context);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        LinearLayout bottomsheet =
                findViewById(R.id.bottomsheet);
        mBottomsheetbehvior = BottomSheetBehavior.from(bottomsheet);
        mBottomsheetbehvior.setHideable(true);
        mBottomsheetbehvior.setPeekHeight(400);
        mBottomsheetbehvior.setState(BottomSheetBehavior.STATE_HIDDEN);

        eventListButton = findViewById(R.id.eventlistbutton);
        eventListButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mBottomsheetbehvior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
                    mBottomsheetbehvior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                    eventListButton.setText("Hide");
                }
                else if(mBottomsheetbehvior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
                    mBottomsheetbehvior.setState(BottomSheetBehavior.STATE_HIDDEN);
                    eventListButton.setText("Event List Button");
                }
                else if(mBottomsheetbehvior.getState() == BottomSheetBehavior.STATE_HIDDEN) {
                    mBottomsheetbehvior.setState(BottomSheetBehavior.STATE_EXPANDED);
                    eventListButton.setText("Peek");
                }
            }
        });

        mBottomsheetbehvior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_EXPANDED) {
                    eventListButton.setText("Peek");
                }
                else if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
                    eventListButton.setText("Hide");
                }
                else if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                    eventListButton.setText("Event List Button");
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        }); 
        final ListView eventListView =  (ListView) findViewById(R.id.eventlistview);
        final DatabaseHelper eventManager = new DatabaseHelper(context);

        Thread thread = new Thread(new Runnable(){
            @Override
            public void run(){
                eventCursor = eventManager.getAllEvents();
                eventListCursorAdapter = new EventListCursorAdapter(
                        MainActivity.this,
                        eventCursor,
                        0);

                eventListView.setAdapter(eventListCursorAdapter);
            }
        });
        thread.start();   
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        //Stuff
    }
    public void moveCamera(LatLng location){

        final CameraPosition movelocation  = CameraPosition.builder().
                target(location).zoom(14).build();
        MainActivity.mMap.animateCamera(CameraUpdateFactory.newCameraPosition(movelocation));
    }

}

如果有帮助,这是光标适配器的代码:

public class EventListCursorAdapter extends CursorAdapter {

    private LayoutInflater cursorInflater;
    Calculations calculations = new Calculations();
    Gson gson = new Gson();
    Context AppContext;

    public EventListCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
        cursorInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        AppContext = context.getApplicationContext();
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return cursorInflater.inflate(R.layout.card_view, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView timeText = (TextView) view.findViewById(R.id.event_time);
        TextView nameText = (TextView) view.findViewById(R.id.event_name);
        TextView dateText = (TextView) view.findViewById(R.id.event_date);
        TextView summaryText = (TextView) view.findViewById(R.id.event_summary);
        TextView participantsText = (TextView) view.findViewById(R.id.event_participantNum);
        TextView locationText = (TextView) view.findViewById(R.id.event_location);

        final Cursor mCursor = cursor;

        String date = calculations.UnixTimeConverter(
                mCursor.getLong(mCursor.getColumnIndex(DatabaseHelper.COL_UNIXTIME)
        ))[0];
        String time = calculations.UnixTimeConverter(
                mCursor.getLong(mCursor.getColumnIndex(DatabaseHelper.COL_UNIXTIME))
        )[1];
        final LatLng location = gson.fromJson(mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_LOCATION)),LatLng.class);

        nameText.setText(mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_NAME)));
        dateText.setText(date);
        timeText.setText(time);
        participantsText.setText(mCursor.getInt(mCursor.getColumnIndex(DatabaseHelper.COL_PARTICIPANTS))+"");
        summaryText.setText(mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_SUMMARY)));
        locationText.setText(mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_LOCATIONNAME)));

        locationText.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                final CameraPosition camLocation  = CameraPosition.builder().
                        target(location).zoom(18).build();
                MainActivity.mMap.animateCamera(CameraUpdateFactory.newCameraPosition(camLocation));
            }
        });

        summaryText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LayoutInflater mLayoutInflator;
                mLayoutInflator = LayoutInflater.from(AppContext);
                final AlertDialog.Builder mBuilder = new AlertDialog.Builder(mContext);
                View mView = mLayoutInflator.inflate(R.layout.summarydialog,null);
                TextView textView = mView.findViewById(R.id.mainText);
                textView.setText(
                        mCursor.getString(mCursor.getColumnIndex(DatabaseHelper.COL_SUMMARY))
                );

                textView.setMovementMethod(new ScrollingMovementMethod());

                mBuilder.setView(mView);
                final AlertDialog dialog = mBuilder.create();
                dialog.show();
            }
        });

    }
}

如果您能告诉如何让底部工作表listview显示光标中的所有数据,而不让某些列表项被“扭曲”,那将会很有帮助。比如第二张图片

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 只需删除嵌套的ScrollView标记即可。 显然,您不应该将ListView放在scrollview

它不会对底部工作表造成任何形式的改变:)