我的RecyclerView有两个问题:
第一个问题是,当我完成其活动并按下后退按钮时,它会再次创建相同的活动,但在删除一些行(大部分是从结尾处)之后,我不知道是否问题是它是在多个阶段创建视图还是什么。
第二个问题是当我按下某个按钮(例如按钮)时,每次按下时都会使用新结果重新创建活动。
这是我的适配器
package com.example.agh.grad.Adapters;
import android.Manifest; import android.app.Activity; import
android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.support.v4.app.ActivityCompat; import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import com.example.agh.grad.ItemClickListner; import com.example.agh.grad.Models.Services; import com.example.agh.grad.R; import com.example.agh.grad.SearchServices; import com.example.agh.grad.ServiceDetail; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import org.parceler.Parcels; import java.util.ArrayList; import butterknife.BindView; import butterknife.ButterKnife; /** * Created by cz on 17/06/17. */ public class recylerServiceAdapter extends RecyclerView.Adapter<recylerServiceAdapter.ViewHolder> { ArrayList<Services> service; Context context; ItemClickListner clickListner; public recylerServiceAdapter(ArrayList<Services> service, Context context) { this.service = service; this.context = context; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_recyler_services, parent, false); ViewHolder viewHolder = new ViewHolder(v); context= parent.getContext(); return viewHolder; }
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.tvServiceName.setText(service.get(position).getName());
holder.tvServiceDisrption.setText(service.get(position).getServiceShortDesrption());
holder.tvServiceProvider.setText(service.get(position).getServiceProvider());
holder.tvLikeCounter.setText(String.valueOf(service.get(position).getLikes()));
holder.tvDislikeCounter.setText(String.valueOf(service.get(position).getDislikes()));
holder.tvLocationDescrption.setText(service.get(position).getLocationAddress());
holder.tvServiceName.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/* Intent myIntent= new Intent(context,SearchServices.class);
context.startActivity(myIntent);*/
}
});
holder.btnLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final FirebaseDatabase firedatabase = FirebaseDatabase.getInstance();
DatabaseReference ref = firedatabase.getReference("Services");
DatabaseReference wantedService = ref.child(String.valueOf(service.get(holder.getAdapterPosition()).getName()));
final Long uplikes= service.get(holder.getAdapterPosition()).getLikes()+1;
wantedService.child("likes").setValue(uplikes, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
holder.tvLikeCounter.setText(String.valueOf(uplikes));
}
});
// TODO: 11/10/17 update on the database
//// TODO: 05/12/17 Check state of buttons and act upon it
holder.btnLike.setClickable(false);//// TODO: 05/12/17 set a clicked icon
holder.btnDislike.setClickable(false);
}
});
holder.btnDislike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final FirebaseDatabase firedatabase = FirebaseDatabase.getInstance();
DatabaseReference ref = firedatabase.getReference("Services");
DatabaseReference wantedService = ref.child(String.valueOf(service.get(holder.getAdapterPosition()).getName()));
final Long dislikes= service.get(holder.getAdapterPosition()).getLikes()-1;
wantedService.child("likes").setValue(dislikes, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
holder.tvLikeCounter.setText(String.valueOf(dislikes));
}
});
}
});
holder.btnCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + service.get(holder.getAdapterPosition()).getPhoneNumber()));
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Ask for permission
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
context.startActivity(callIntent);
}
});
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent myIntent= new Intent(context,ServiceDetail.class);
myIntent.putExtra(SearchServices.PARCELER_TAG, Parcels.wrap(service.get(holder.getAdapterPosition()) ));
context.startActivity(myIntent);
((Activity)context).finish();
}
});
}
@Override
public int getItemCount() {
return service.size();
}
public void setClickListener(ItemClickListner itemClickListener) {
this.clickListner = itemClickListener;
}
public class ViewHolder extends RecyclerView.ViewHolder {
@BindView (R.id.serviceName) TextView tvServiceName;
@BindView (R.id.serviceProvider)TextView tvServiceProvider;
@BindView (R.id.serviceShortDescrption)TextView tvServiceDisrption;
@BindView (R.id.tvLikeCounter)TextView tvLikeCounter;
@BindView (R.id.tvDislikeCounter)TextView tvDislikeCounter ;
@BindView (R.id.locationDescrption)TextView tvLocationDescrption;
@BindView (R.id.btnLike) Button btnLike;
@BindView (R.id.btnDislike) Button btnDislike;
@BindView (R.id.btnCall) Button btnCall;
@BindView (R.id.serviceCard) CardView cardView;
public ViewHolder(View itemView) {
super(itemView);
itemView.setTag(itemView);
ButterKnife.bind(this, itemView);
}
}
}
这是我的活动
public class MainActivity extends AppCompatActivity {
List<String> arrayServices = new ArrayList<>();
RecyclerView recyclerViewServices;
recylerServiceAdapter recylerServiceAdapter;
ArrayList<Services> newServices;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = this.getIntent();
// arrayServices= intent.getStringArrayListExtra("newServices");
// System.out.println(arrayServices.size());
newServices = Parcels.unwrap(intent.getParcelableExtra(SearchServices.PARCELER_TAG)) ;
Log.e( "onCreate: ",newServices.toString() );
recyclerViewServices = (RecyclerView) findViewById(R.id.recylerViewServiceDetail);
recyclerViewServices.setLayoutManager(new LinearLayoutManager(this));
recylerServiceAdapter = new recylerServiceAdapter(newServices, getApplicationContext());
recyclerViewServices.setAdapter(recylerServiceAdapter);
}
}
和我的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:background="@color/lightPrimary"
tools:context="com.example.agh.grad.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recylerViewServiceDetail"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="5dp"
tools:layout_editor_absoluteY="5dp" />
</RelativeLayout>
答案 0 :(得分:0)
而不是使用此
holder.tvServiceName.setText(service.get(position).getName());
使用
holder.tvServiceName.setText(service.get(getAdapterPosition())
.getName());
答案 1 :(得分:0)
而不是在onClick中使用它
holder.getAdapterPosition()
使用
public void onBindViewHolder(final ViewHolder holder,final int position)
position
答案 2 :(得分:0)
将活动启动模式更改为android:launchMode =&#34; singleTask&#34;解决了这个问题