recyclerview不显示所有数据

时间:2018-03-27 07:10:19

标签: android android-recyclerview

我尝试调整recyclelerview来显示对象列表。但实际上我有一些奇怪的错误。我的对象有要显示的ID和名称数据,但在列表中,首先,我只能看到Id,当我向下滚动时,只有完全显示出屏幕的行才会完全显示...

这是我的ListPresentActivity。

public class ListPresentActivity extends AppCompatActivity implements ViewInterface {

private static final String EXTRA_PRESENT_ID = "EXTRA_PRESENT_ID";
private static final String EXTRA_PRESENT_NAME = "EXTRA_PRESENT_NAME";

private List<PresentItem> listOfPresent;

private LayoutInflater layoutInflater;
private RecyclerView recyclerView;
private CustomAdapter adapter;

private PresentController presentController;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_present);


    recyclerView = (RecyclerView) findViewById(R.id.rec_list_activity);
    recyclerView.setHasFixedSize(true);

    layoutInflater = getLayoutInflater();

    presentController = new PresentController(this, new FakePresentModel());
}

@Override
public void startDetailActivity(int id, String name, String info, String target, String advice, String price) {
//public void startDetailActivity(int id) {
    Intent i = new Intent(this,PresentDetailActivity.class);
    i.putExtra(EXTRA_PRESENT_ID, id);
    i.putExtra(EXTRA_PRESENT_NAME, name);

    startActivity(i);
}

@Override
public void setUpAdapterAndView(List<PresentItem> listOfPresent) {
    this.listOfPresent = listOfPresent;

    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    adapter = new CustomAdapter();
    recyclerView.setAdapter(adapter);
}

private class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomViewHolder>{


    @Override
    public CustomAdapter.CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = layoutInflater.inflate(R.layout.item_present, parent, false);

        return new CustomViewHolder(v);
    }

    @Override
    public void onBindViewHolder(CustomAdapter.CustomViewHolder holder, int position) {

        PresentItem currentPresent = listOfPresent.get(position);

        holder.id.setText(
                Integer.toString(currentPresent.getId())
        );

        holder.name.setText(
                currentPresent.getName()
        );
    }

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

    class CustomViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private TextView id;
        private TextView name;
        private ViewGroup container;

        public CustomViewHolder(View itemView){
            super(itemView);

            this.id = (TextView) itemView.findViewById(R.id.texv_item_id);
            this.name = (TextView) itemView.findViewById(R.id.texv_item_name);

            this.container = (ViewGroup) itemView.findViewById(R.id.root_list_present);

            this.container.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            PresentItem presentItem = listOfPresent.get(
                    this.getAdapterPosition()
            );

            presentController.onListItemClick(presentItem);
        }
    }

}
}

布局item_present.xml

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="88dp"
android:id="@+id/root_list_present"
xmlns:tools="http://schemas.android.com/tools">

<TextView
    android:id="@+id/texv_item_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:textSize="48sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="#id" />

<TextView
    android:id="@+id/texv_item_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="84dp"
    android:layout_marginTop="8dp"
    android:ellipsize="end"
    android:maxLines="1"
    android:textSize="20sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="collier" />

</android.support.constraint.ConstraintLayout>

我的列表布局

<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.lubbee.bruh.view.ListPresentActivity">

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

</android.support.constraint.ConstraintLayout>

我现在已经搜索了很长时间,并且我有fount的唯一结果似乎是用recyclerView.setHasFixedSize(true);修复的,这不是我的情况......

谢谢你的时间!

PS:如果需要丢失一些代码部分,只需说出这个词! :

enter image description here

enter image description here

第一次加载后。

一次滚动到底部并返回到屏幕顶部 enter image description here

1 个答案:

答案 0 :(得分:1)

<LinearLayout android:layout_width="match_parent"
    android:layout_height="88dp"
    android:weightSum="1"
    xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:layout_gravity="center_vertical"
    android:layout_weight="0.2"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

<TextView
    android:layout_gravity="center_vertical"
    android:layout_weight="0.8"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />

</LinearLayout>

尝试这个简单的LinearLayout应该可行 您还可以添加其他属性,但现在可以尝试