Recycler视图和Card View无法正常工作

时间:2017-01-19 11:45:59

标签: android android-layout android-recyclerview android-cardview recycler-adapter

查看主要活动.xml 我正在做的是在按钮下面显示回收器视图并编辑文本.. 编辑文本的值被制作成卡但问题是按钮不起作用我不知道为什么plz帮助

我是协调员观点的新手,所以如果我做了一些noob错误,请原谅我

AdapterEx.java

import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class AdapterEx extends RecyclerView.Adapter<AdapterEx.ViewHolder>{

    private List<String> nos;


    public AdapterEx(List<String> nos){
        this.nos = nos;
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        public TextView number;

        public ViewHolder(View itemView) {
            super(itemView);
            number  =
                    (TextView)itemView.findViewById(R.id.nos);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View v) {
                    int position = getAdapterPosition();

                    Snackbar.make(v, "Click detected on item " + position,
                            Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();

                }
            });
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.card, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        viewHolder.number.setText(nos.get(i));
    }

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

初始化回收商视图

recycler = (RecyclerView) findViewById(R.id.rv);
        LayoutManager manager = new LinearLayoutManager(this);
        recycler.setLayoutManager(manager);
        nos = new ArrayList<>();
        nos.add("sadasd");
        nos.add("sdada");
        adapter = new AdapterEx(nos);
        recycler.setAdapter(adapter);
        recycler.setHasFixedSize(true);
        recycler.setItemAnimator(new DefaultItemAnimator());

主要活动。 XML

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

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.dev99.mathbuddy.prime"
    android:id="@+id/prime"
    android:background="@drawable/prime">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:inputType="number|numberDecimal|numberSigned"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="FACTORISE IT!"
        android:id="@+id/button5"
        android:onClick="checker"
        android:nestedScrollingEnabled="false"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/editText2"
        android:layout_below="@+id/button5"
        android:textAlignment="center"
        android:textSize="30dp"
        android:textColor="#000000"
        android:layout_alignParentStart="false"
        android:layout_alignParentLeft="false" />

</RelativeLayout>

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

    </android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>

card view.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_view"
    android:layout_gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_margin="10dp"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="4dp"
    card_view:cardBackgroundColor="@color/bluemat"
    card_view:cardElevation="10dp"
    card_view:contentPadding="4dp"
    android:clickable="true">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/nos"
        android:textSize="40dp"
        android:textStyle="bold"
        android:textAlignment="center"
        android:layout_gravity="center"
        android:textColor="@color/red"/>

</android.support.v7.widget.CardView>

1 个答案:

答案 0 :(得分:2)

问题是RecyclerView wrap_content。 出于某种原因,RecyclerView不能很好地显示最后一项或最后几项。我遇到的最常见问题是最后一项被切断了。