在RecyclerView中搜寻后出现的下一个项目

时间:2016-07-15 11:45:03

标签: android android-recyclerview recycler-adapter

我创建了RecyclerView。当它在屏幕上呈现时,每个屏幕只显示一个项目。当我向下滚动RecyclerView时,我可以看到下一个项目。这是我的代码

    public class LeaveStatusViewAdapter extends RecyclerView
        .Adapter<LeaveStatusViewAdapter
        .DataObjectHolder> {
    private static String LOG_TAG = "MyRecyclerViewAdapter";
    private ArrayList<Employee> mDataset;
    private static MyClickListener myClickListener;

    public static class DataObjectHolder extends RecyclerView.ViewHolder
            implements View
            .OnClickListener {
        TextView startDate;
        TextView endDate;
        TextView leaveType;
        TextView leaveDays;
        TextView leaveReason;
        TextView leaveStatus;

        public DataObjectHolder(View itemView) {
            super(itemView);
            startDate = (TextView) itemView.findViewById(R.id.textView_StartDate);
            endDate = (TextView) itemView.findViewById(R.id.textView_EndDate);
            leaveType = (TextView) itemView.findViewById(R.id.textView_LeaveType);
            leaveDays = (TextView) itemView.findViewById(R.id.textView_LeaveDays);
            leaveReason = (TextView) itemView.findViewById(R.id.textView_LeaveReason);
            leaveStatus = (TextView) itemView.findViewById(R.id.textView_LeaveStatus);
            Log.i(LOG_TAG, "Adding Listener");
            itemView.setOnClickListener(this);
        }

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

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

    public LeaveStatusViewAdapter(ArrayList<Employee> myDataset) {
        mDataset = myDataset;
    }

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

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

    @Override
    public void onBindViewHolder(DataObjectHolder holder, int position) {
        holder.startDate.setText(mDataset.get(position).getLeaveStartDate());
        holder.endDate.setText(mDataset.get(position).getLeavesEndDate());
        holder.leaveType.setText(mDataset.get(position).getLeaveType());
        holder.leaveDays.setText(mDataset.get(position).getLeaveDays());
        holder.leaveReason.setText(mDataset.get(position).getLeaveReason());
        holder.leaveStatus.setText(mDataset.get(position).getLeaveStatus());
    }

    public void addItem(Employee 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);
    }
}

以下是活动类

public class LeaveStatusActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private static String LOG_TAG = "CardViewActivity";

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

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

    @Override
    protected void onResume() {
        super.onResume();
        ((LeaveStatusViewAdapter) mAdapter).setOnItemClickListener(new LeaveStatusViewAdapter
                .MyClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                Log.i(LOG_TAG, " Clicked on Item " + position);
            }
        });
    }

    private ArrayList<Employee> getDataSet() {
        ArrayList results = new ArrayList<Employee>();
        for (int index = 0; index < 3; index++) {
            Employee obj = new Employee(" : 15/07/2016",
                    " : 20/07/2016"," : EL: Earned Leave"," : 5"," : Not feeeling well"," : Pending");
            results.add(index, obj);
        }
        return results;
    }
}

以下是布局文件

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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"
    tools:context="com.leave.mcgm.mcgmleavemanagement.LeaveStatusActivity">

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

在RecyclerView中为每个项目提供自定义视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">

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

            <TextView
                android:id="@+id/lbl_StartDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:layout_alignParentTop="true"
                android:text="Start Date"/>

            <TextView
                android:id="@+id/textView_StartDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_toEndOf="@+id/lbl_StartDate" />
            <TextView
                android:id="@+id/lbl_EndDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="End Date"
                android:textStyle="bold"
                android:layout_marginStart="24dp"
                android:layout_alignParentTop="true"
                android:layout_toEndOf="@+id/textView_LeaveReason" />
            <TextView
                android:id="@+id/textView_EndDate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/lbl_EndDate"
                android:layout_alignBottom="@+id/lbl_EndDate"
                android:layout_toEndOf="@+id/lbl_EndDate" />
            <TextView
                android:id="@+id/lbl_LeaveType"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:text="LeaveType"
                android:layout_below="@+id/lbl_EndDate"
                android:layout_alignParentStart="true"
                android:textStyle="bold"/>
            <TextView
                android:id="@+id/textView_LeaveType"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:text=""
                android:layout_alignBaseline="@+id/lbl_LeaveType"
                android:layout_alignBottom="@+id/lbl_LeaveType"
                android:layout_toEndOf="@+id/lbl_LeaveType" />
            <TextView
                android:id="@+id/lbl_LeaveDays"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Leave Days"
                android:singleLine="true"
                android:layout_below="@+id/lbl_EndDate"
                android:layout_alignStart="@+id/lbl_EndDate"
                android:textStyle="bold"/>
            <TextView
                android:id="@+id/textView_LeaveDays"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:text=""
                android:layout_alignBaseline="@+id/lbl_LeaveDays"
                android:layout_alignBottom="@+id/lbl_LeaveDays"
                android:layout_toEndOf="@+id/lbl_LeaveDays" />
            <TextView
                android:id="@+id/lbl_LeaveReason"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Leave Reason"
                android:singleLine="true"
                android:layout_alignTop="@+id/textView_LeaveReason"
                android:layout_alignParentStart="true"
                android:textStyle="bold"/>
            <TextView
                android:id="@+id/textView_LeaveReason"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:text=""
                android:layout_below="@+id/textView_LeaveDays"
                android:layout_toEndOf="@+id/lbl_LeaveReason" />
            <TextView
                android:id="@+id/lbl_LeaveStatus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Leave Status"
                android:singleLine="true"
                android:layout_alignBaseline="@+id/textView_LeaveStatus"
                android:layout_alignBottom="@+id/textView_LeaveStatus"
                android:layout_alignParentStart="true"
                android:textStyle="bold"/>
            <TextView
                android:id="@+id/textView_LeaveStatus"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:text=""
                android:layout_below="@+id/lbl_LeaveReason"
                android:layout_toEndOf="@+id/lbl_LeaveReason"
                android:textAllCaps="true"/>
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:1)

您应该将列表中每个元素的视图更改为android:layout_height="wrap_content"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
<android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">
.....
</LinearLayout>

当您使用android:layout_height="match_parent"时,列表中的每个项目都会尝试使用屏幕中可能的最大空间