簡體   English   中英

RecyclerView是否添加了其他項目?

[英]RecyclerView Extra item added?

我在回收者視圖中獲得了很大的空間,就像每次添加項目時都會添加一個額外的項目,並且看起來像是這個額外的項目一樣? 告訴我您需要解決什么代碼,我很高興為您提供幾乎可以在給您main和myadapter類的基礎上提供的任何代碼。

主要活動:

package com.example.jenso.paperseller;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {
    CustomerDatabase database;
    FloatingActionButton fab;
    private CustomerViewModel mCustomerViewModel;

    public static final int NEW_CUSTOMER_ACTIVITY_REQUEST_CODE = 1;

    private static final  String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        RecyclerView recyclerView = findViewById(R.id.recycler);
        final PapperRecyclerAdapter adapter = new PapperRecyclerAdapter(this);

        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        fab = findViewById(R.id.fab);

        mCustomerViewModel = ViewModelProviders.of(this).get(CustomerViewModel.class);

        mCustomerViewModel.getmAllCustomers().observe(this, new Observer<List<Customer>>() {
            @Override
            public void onChanged(@Nullable List<Customer> customers) {
                adapter.setCustomer(customers);
            }
        });
        recyclerView.setAdapter(adapter);
        fab.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, CreateCustomer.class);
                startActivityForResult(intent, NEW_CUSTOMER_ACTIVITY_REQUEST_CODE);

            }

        });
    }

    public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == NEW_CUSTOMER_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
            String[] mCustomerSave = data.getStringArrayExtra(CreateCustomer.EXTRA_REPLY);
            Customer customer = new Customer(mCustomerSave[0],mCustomerSave[1],mCustomerSave[2],mCustomerSave[3]);
            mCustomerViewModel.insert(customer);
        } else {
            Toast.makeText(
                    getApplicationContext(),
                    R.string.empty_not_saved,
                    Toast.LENGTH_LONG).show();
        }
    }
}

MyAdapter:

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class PapperRecyclerAdapter extends RecyclerView.Adapter<PapperRecyclerAdapter.CustomerViewHolder> {

    class CustomerViewHolder extends  RecyclerView.ViewHolder{
        private TextView textViewName;
        private TextView textViewAddress;
        private TextView textViewPhoneNumber;
        private TextView textViewEmail;

        private CustomerViewHolder(View itemView){
            super(itemView);
            textViewName = itemView.findViewById(R.id.nameTxt);
            textViewAddress = itemView.findViewById(R.id.addressTxt);
            textViewPhoneNumber = itemView.findViewById(R.id.PhoneNumberTxt);
            textViewEmail = itemView.findViewById(R.id.emailTxt);

        }

    }
    private List<Customer> mCustomers;
    private Context context;

    private final LayoutInflater mInflater;

    public PapperRecyclerAdapter(Context context) {
          mInflater = LayoutInflater.from(context);
    }

    @Override
    public CustomerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.list_item, parent, false);

        return new CustomerViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(CustomerViewHolder holder, int position) {
        if(mCustomers != null) {
            Customer current = mCustomers.get(position);
            holder.textViewName.setText(current.getFullName());
            holder.textViewAddress.setText(current.getAddress());
            holder.textViewPhoneNumber.setText(current.getPhonenumber());
            holder.textViewEmail.setText(current.getEmail());

        } else {
            holder.textViewName.setText("Full name");
            holder.textViewAddress.setText("Address");
            holder.textViewPhoneNumber.setText("PhoneNumber");
            holder.textViewEmail.setText("Email");

        }
    }

    void setCustomer(List<Customer> customers) {
        mCustomers = customers;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if(mCustomers != null){
            return mCustomers.size();
        } else {
            return 0;
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
        }
    }
}

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <TextView
                android:id="@+id/nameTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Name"


                />

            <TextView
                android:id="@+id/addressTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Address"


                />

            <TextView
                android:id="@+id/PhoneNumberTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Phonenumber"


                />

            <TextView
                android:id="@+id/emailTxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Email"


                />

        </LinearLayout>


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


</android.support.constraint.ConstraintLayout>

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:layout_gravity="bottom|end"
        android:foreground="@mipmap/ic_launcher_round" />

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

Content_main:

<?xml version="1.0" encoding="utf-8"?>
<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">


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




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





</android.support.constraint.ConstraintLayout>

這里的問題很可能在您的list_item中,我建議您做的是

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM