我在我的回收者视图上获得了一个巨大的空间,就像我在添加项目时添加额外的项目一样,它看起来像Extra item?。告诉我你需要修改哪些代码我很乐意提供任何代码,我几乎可以开始给你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>
答案 0 :(得分:0)
这里的问题很可能在你的list_item中,我建议你做的是
<LinearLayout>
<android.support.v7.widget.CardView>
<TextView/>
</android.support.v7.widget.CardView>
</LinearLayout>