gpt4 book ai didi

java - RecyclerView 添加了额外项目?

转载 作者:太空宇宙 更新时间:2023-11-04 10:37:37 26 4
gpt4 key购买 nike

我的回收器 View 上有一个巨大的空间,就像每当我添加一个项目时它就会添加一个额外的项目,它看起来像这样 Extra item? 。告诉我你需要什么代码来解决这个问题,我很乐意提供几乎我可以提供的任何代码,至少从给你 main 和 myadapter 类开始。

主要 Activity :

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

我的适配器:

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 主线:

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

关于java - RecyclerView 添加了额外项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49307289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com