RecyclerView的高度“ match_parent”未按预期运行,而固定高度(1000dp,2000dp)正在运行

Kis*_*Oli 1 java xml android android-layout android-xml

我正在努力制作一个很酷的布局,如下所示:

为此,我正在使用AndroidParallax l

为此,我正在使用Github的 Android Parallax库。该库从xml本身创建所有视图(如图所示)。但是我想创建自己的recyclerview,创建adpater,模型类并使用cardview显示它们。

我尝试使用cardview和recyclerview。

问题:

当我将RecyclerView的高度设置为match_parentandroid:layout_height =“ match_parent”)时,它给出如下的UI:

在此处输入图片说明

仅显示第一张卡片视图,仅显示一半。其他卡片视图以某种方式重叠。

但是,当我使用固定高度(例如1000dp,2000dp)给出其高度时,它会按预期显示UI(如第一个图所示)。我认为将其高度固定是一个不好的解决方案,因为数据项可能会有所不同。

我不明白我的代码有什么问题。请为此提出一些解决方案。

以下是我对代码的不同看法。

activity_main.xml

<com.github.florent37.parallax.ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:scaleType="centerCrop"
            android:src="@drawable/background"
            android:tag="parallax=0.3" />

        <TextView
            style="@style/MyTitle"
            android:layout_width="match_parent"
            android:layout_height="160dp"
            android:gravity="center"
            android:tag="parallax=0.5"
            android:text="My awesome title" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="150dp"
            android:orientation="vertical">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/my_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical" />
        </LinearLayout>
    </FrameLayout>
</com.github.florent37.parallax.ScrollView>
Run Code Online (Sandbox Code Playgroud)

card_view.xml

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

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="10dp">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:text="Hello"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textView"
                android:layout_marginTop="10dp"
                android:text="world" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我的适配器类是这样的:

public class MyRecyclerViewAdapter extends RecyclerView
        .Adapter<MyRecyclerViewAdapter
        .DataObjectHolder> {

    private ArrayList<ContactsModel> mDataset;
    private static MyClickListener myClickListener;

    public static class DataObjectHolder extends RecyclerView.ViewHolder
            implements View
            .OnClickListener {
        TextView label;
        TextView dateTime;

        public DataObjectHolder(View itemView) {
            super(itemView);
            label = (TextView) itemView.findViewById(R.id.textView);
            dateTime = (TextView) itemView.findViewById(R.id.textView2);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            //myClickListener.onItemClick(getAdapterPosition(), v);
        }
    }

    public void setOnItemClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public MyRecyclerViewAdapter(ArrayList<ContactsModel> myDataset) {
        mDataset = myDataset;
    }

    @Override
    public DataObjectHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_view, parent, false);

        DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
        return dataObjectHolder;
    }

    @Override
    public void onBindViewHolder(DataObjectHolder holder, int position) {
        holder.label.setText(mDataset.get(position).getmText1());
        holder.dateTime.setText(mDataset.get(position).getmText2());
    }

    public void addItem(ContactsModel dataObj, int index) {
        mDataset.add(index, dataObj);
        notifyItemInserted(index);
    }

    public void deleteItem(int index) {
        mDataset.remove(index);
        notifyItemRemoved(index);
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    public interface MyClickListener {
        public void onItemClick(int position, View v);
    }
}
Run Code Online (Sandbox Code Playgroud)

MainActivity中,我编写了这样的代码:

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new MyRecyclerViewAdapter(getDataSet());
        mRecyclerView.setAdapter(mAdapter);



        Fabric.with(this, new Crashlytics());
    }
    private ArrayList<ContactsModel> getDataSet() {
        ArrayList results = new ArrayList<ContactsModel>();
        for (int index = 0; index < 20; index++) {
            ContactsModel obj = new ContactsModel("Some Primary Text " + index,
                    "Secondary " + index);
            results.add(index, obj);
        }
        return results;
    }
}
Run Code Online (Sandbox Code Playgroud)

模型类如下:

public class ContactsModel {
    private String mText1;
    private String mText2;

    ContactsModel (String text1, String text2){
        mText1 = text1;
        mText2 = text2;
    }

    public String getmText1() {
        return mText1;
    }

    public void setmText1(String mText1) {
        this.mText1 = mText1;
    }

    public String getmText2() {
        return mText2;
    }

    public void setmText2(String mText2) {
        this.mText2 = mText2;
    }
}
Run Code Online (Sandbox Code Playgroud)

很长的代码很抱歉。

谢谢!

Jos*_*ith 6

您在可滚动布局中有一个RecyclerView(可滚动)。

将您的ScrollView标记替换为“ android.support.v4.widget.NestedScrollView”。这将使RecyclerView正确展开。

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

...

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

</android.support.v4.widget.NestedScrollView>
Run Code Online (Sandbox Code Playgroud)


Ala*_* sa 5

将 ScrollView 替换为 NestedScrollView 后,您只需将此行添加到 NestedScrollView 中即可

android:fillViewport="true"
Run Code Online (Sandbox Code Playgroud)