【问题标题】:RecyclerView's height 'match_parent' not working as expected whereas fixed height (1000dp,2000dp) is workingRecyclerView 的高度“match_parent”没有按预期工作,而固定高度(1000dp,2000dp)正在工作
【发布时间】:2017-11-02 23:40:17
【问题描述】:

我正在努力制作这样一个很酷的布局:

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

我尝试使用 cardview 和 recyclerview。

问题:

当我将 RecyclerView 的高度设置为 match_parent (android: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>

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>

我的适配器类是这样的:

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

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

模型类是这样的:

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

抱歉,代码太长了。

谢谢!

【问题讨论】:

  • 您没有使用CollapsingToolbarLayout 内置的视差支持和overlap your AppBarLayout 的内置功能是否有原因?
  • 我对 CollapsingToolbarLayout 一无所知,所以在网上冲浪时,我发现这个库有点酷,所以我选择了它。
  • @ianhanniballake 折叠工具栏是不是类似这样?我在这个库上花了很多时间,所以切换到 CollapsingToolbar 将这个 Parallax 库放在一边是个好主意。请告诉我。谢谢。

标签: java android xml android-layout android-xml


【解决方案1】:

您在可滚动布局中有一个 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>

【讨论】:

  • 感谢您的快速回复。你的意思是用&lt;android.support.v4.widget.NestedScrollView替换这个&lt;com.github.florent37.parallax.ScrollView
  • 没问题!是的,将 com.github.florent37.parallax.ScrollView 替换为 android.support.v4.widget.NestedScrollView
  • 我试过了,但也没有任何运气。我认为 CollapsingToolbarLayout 会按照@ianhanniballake 的建议进行类似的工作。
【解决方案2】:

用 NestedScrollView 替换您的 ScrollView 后,您可以将此行添加到 NestedScrollView

android:fillViewport="true"

【讨论】:

    【解决方案3】:

    我在 NestedScrollView 中遇到了同样的问题。

    只需在 NestedScrollView 中添加一行,我的问题就解决了。

    android:fillViewport="true"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      • 1970-01-01
      • 2010-10-23
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 2021-03-25
      相关资源
      最近更新 更多